Example #1
0
// need to run the caller always "defer log_file.Close()"
func setLogOptions(conf *config.Config) (*os.File, int) {
	// log setting
	if conf.Log.Verbose {
		log.SetLogLevelDebug()
	}
	if conf.Log.JSON {
		log.SetJSONLogFormat()
	}
	logRoot := utils.GetHomeDir()
	if conf.Log.Root != "" {
		logRoot = conf.Log.Root
	}

	logPath := path.Join(logRoot, fmt.Sprintf("%s.log", utils.GetFormatedFileDisplayName()))
	// need to run the caller always "defer log_file.Close()"
	logFile, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.Errorf("%s", err.Error())
		return nil, 1
	}

	log.Debugf("Log File: %s", logPath)

	return logFile, 0
}
Example #2
0
// ExecuteSQL is execute SQL to aws rds
func (c *Command) ExecuteSQL(args *ExecuteSQLArgs) ([]time.Duration, error) {
	driver, dsn := c.getDbOpenValues(args)

	if driver == "" {
		log.Errorf("%s", ErrDriverNotFound.Error())
		return nil, ErrDriverNotFound
	}

	db, err := sql.Open(driver, dsn)
	if err != nil {
		log.Errorf("%s", err.Error())
		return nil, err
	}
	defer db.Close()

	times := make([]time.Duration, 0, len(args.Queries))
	for _, value := range args.Queries {
		log.Debugf("query value : %s", value)

		sTime := time.Now()
		log.Infof("query start time: %s", sTime)

		result, err := db.Query(value.SQL)
		if err != nil {
			log.Errorf("%s", err.Error())
			return times, err
		}

		eTime := time.Now()
		log.Infof("query end time: %s", eTime)

		times = append(times, eTime.Sub(sTime))

		// output csv file
		cols, _ := result.Columns()
		if c.OutConfig.File && len(cols) > 0 {
			fileName := value.Name + "-" + utils.GetFormatedTime() + ".csv"
			outPath := utils.GetHomeDir()
			if c.OutConfig.Root != "" {
				outPath = c.OutConfig.Root
			}

			outState := writeCSVFile(
				&writeCSVFileArgs{
					Rows:     result,
					FileName: fileName,
					Path:     outPath,
					Bom:      c.OutConfig.Bom,
				})
			log.Debugf("out_state:%+v", outState)
		}

		result.Close()
	}

	return times, nil
}
Example #3
0
func TestGetDefaultPath(t *testing.T) {
	if path.Join(utils.GetHomeDir(), queryFile) != GetDefaultPath() {
		t.Error("default path not match")
	}
}
Example #4
0
// GetDefaultPath is return default query file path.
func GetDefaultPath() string {
	return path.Join(utils.GetHomeDir(), queryFile)
}