Skip to content

Commit

Permalink
Refactor errors in sql_runner
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzhen committed Oct 25, 2018
1 parent 6e33592 commit 5bd877a
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions sql_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func mustNewSQLRunner(quitContext context.Context, printer func(string), query s
func (sr *sqlRunner) runSQL(db database, key string) bool {
typ, ok := validSQLTypes[db.SQLType]
if !ok {
usage("Unknown sql type %v for %v", db.SQLType, db)
return maybeErrorResult(key, fmt.Sprintf("Unknown sql type %v for %v", db.SQLType, key))
}

sqlOptions := sqlTypeToOptions[typ]
Expand Down Expand Up @@ -137,19 +137,16 @@ func (sr *sqlRunner) runSQL(db database, key string) bool {

stdout, err := cmd.StdoutPipe()
if err != nil {
log.Printf("Cannot create pipe for STDOUT of running command on %v; not running. err=%v\n", key, err)
return false
return maybeErrorResult(key, fmt.Sprintf("Cannot create pipe for STDOUT of running command on %v; not running. err=%v\n", key, err))
}

stderr, err := cmd.StderrPipe()
if err != nil {
log.Printf("Cannot create pipe for STDERR of running command on %v; not running. err=%v\n", key, err)
return false
return maybeErrorResult(key, fmt.Sprintf("Cannot create pipe for STDERR of running command on %v; not running. err=%v\n", key, err))
}

if err := cmd.Start(); err != nil {
log.Printf("Cannot start command on %v; not running. err=%v\n", key, err)
return false
return maybeErrorResult(key, fmt.Sprintf("Cannot start command on %v; not running. err=%v\n", key, err))
}

scanner := bufio.NewScanner(stdout)
Expand All @@ -168,6 +165,10 @@ func (sr *sqlRunner) runSQL(db database, key string) bool {

cmd.Wait()

return maybeErrorResult(key, stderrLines...)
}

func maybeErrorResult(key string, stderrLines ...string) bool {
result := true
if len(stderrLines) > 0 {
result = false
Expand All @@ -176,6 +177,5 @@ func (sr *sqlRunner) runSQL(db database, key string) bool {
log.Println(key + " [ERROR] " + v)
}
}

return result
}

0 comments on commit 5bd877a

Please # to comment.