From abb19dfbf8d1e0db74f4dcd90755c5e52320f88f Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 30 Sep 2017 14:56:35 -0300 Subject: [PATCH] print logs to stderr instead of stdout also, don't print up-to-date status when the --silent flag was given closes #68 --- help.go | 2 +- log.go | 26 ++++++++++++++++---------- task.go | 8 +++++--- variables.go | 2 +- watch.go | 12 ++++++------ 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/help.go b/help.go index c5919bc9..9681d90d 100644 --- a/help.go +++ b/help.go @@ -12,7 +12,7 @@ func (e *Executor) PrintTasksHelp() { if len(tasks) == 0 { return } - e.println("Available tasks for this project:") + e.outf("Available tasks for this project:") // Format in tab-separated columns with a tab stop of 8. w := tabwriter.NewWriter(e.Stdout, 0, 8, 0, '\t', 0) diff --git a/log.go b/log.go index e8b070a1..fc168130 100644 --- a/log.go +++ b/log.go @@ -4,22 +4,28 @@ import ( "fmt" ) -func (e *Executor) println(args ...interface{}) { - fmt.Fprintln(e.Stdout, args...) +func (e *Executor) outf(s string, args ...interface{}) { + if len(args) == 0 { + s, args = "%s", []interface{}{s} + } + fmt.Fprintf(e.Stdout, s+"\n", args...) } -func (e *Executor) printfln(format string, args ...interface{}) { - fmt.Fprintf(e.Stdout, format+"\n", args...) -} - -func (e *Executor) verbosePrintln(args ...interface{}) { +func (e *Executor) verboseOutf(s string, args ...interface{}) { if e.Verbose { - e.println(args...) + e.outf(s, args...) } } -func (e *Executor) verbosePrintfln(format string, args ...interface{}) { +func (e *Executor) errf(s string, args ...interface{}) { + if len(args) == 0 { + s, args = "%s", []interface{}{s} + } + fmt.Fprintf(e.Stderr, s+"\n", args...) +} + +func (e *Executor) verboseErrf(s string, args ...interface{}) { if e.Verbose { - e.printfln(format, args...) + e.errf(s, args...) } } diff --git a/task.go b/task.go index febf1886..0446737c 100644 --- a/task.go +++ b/task.go @@ -126,7 +126,9 @@ func (e *Executor) RunTask(ctx context.Context, call Call) error { return err } if upToDate { - e.printfln(`task: Task "%s" is up to date`, t.Task) + if !e.Silent { + e.errf(`task: Task "%s" is up to date`, t.Task) + } return nil } } @@ -134,7 +136,7 @@ func (e *Executor) RunTask(ctx context.Context, call Call) error { for i := range t.Cmds { if err := e.runCommand(ctx, t, call, i); err != nil { if err2 := t.statusOnError(); err2 != nil { - e.verbosePrintfln("task: error cleaning status on error: %v", err2) + e.verboseErrf("task: error cleaning status on error: %v", err2) } return &taskRunError{t.Task, err} } @@ -164,7 +166,7 @@ func (e *Executor) runCommand(ctx context.Context, t *Task, call Call, i int) er } if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) { - e.println(cmd.Cmd) + e.errf(cmd.Cmd) } return execext.RunCommand(&execext.RunCommandOptions{ diff --git a/variables.go b/variables.go index 11840b67..0ca38d16 100644 --- a/variables.go +++ b/variables.go @@ -189,7 +189,7 @@ func (e *Executor) handleShVar(v Var) (string, error) { result := strings.TrimSuffix(stdout.String(), "\n") e.dynamicCache[v.Sh] = result - e.verbosePrintfln(`task: dynamic variable: '%s' result: '%s'`, v.Sh, result) + e.verboseErrf(`task: dynamic variable: '%s' result: '%s'`, v.Sh, result) return result, nil } diff --git a/watch.go b/watch.go index 24050882..68dc569e 100644 --- a/watch.go +++ b/watch.go @@ -20,14 +20,14 @@ func (e *Executor) watchTasks(calls ...Call) error { for i, c := range calls { tasks[i] = c.Task } - e.printfln("task: Started watching for tasks: %s", strings.Join(tasks, ", ")) + e.errf("task: Started watching for tasks: %s", strings.Join(tasks, ", ")) ctx, cancel := context.WithCancel(context.Background()) for _, c := range calls { c := c go func() { if err := e.RunTask(ctx, c); err != nil && !isContextError(err) { - e.println(err) + e.errf("%v", err) } }() } @@ -43,7 +43,7 @@ func (e *Executor) watchTasks(calls ...Call) error { for { select { case event := <-w.Event: - e.verbosePrintfln("task: received watch event: %v", event) + e.verboseErrf("task: received watch event: %v", event) cancel() ctx, cancel = context.WithCancel(context.Background()) @@ -51,7 +51,7 @@ func (e *Executor) watchTasks(calls ...Call) error { c := c go func() { if err := e.RunTask(ctx, c); err != nil && !isContextError(err) { - e.println(err) + e.errf("%v", err) } }() } @@ -62,7 +62,7 @@ func (e *Executor) watchTasks(calls ...Call) error { w.TriggerEvent(watcher.Remove, nil) }() default: - e.println(err) + e.errf("%v", err) } case <-w.Closed: return @@ -74,7 +74,7 @@ func (e *Executor) watchTasks(calls ...Call) error { // re-register each second because we can have new files for { if err := e.registerWatchedFiles(w, tasks); err != nil { - e.println(err) + e.errf("%v", err) } time.Sleep(time.Second) }