1
0
mirror of https://github.com/go-task/task.git synced 2025-02-03 13:22:11 +02:00

print logs to stderr instead of stdout

also, don't print up-to-date status when the --silent flag was given

closes #68
This commit is contained in:
Andrey Nering 2017-09-30 14:56:35 -03:00
parent 14676dc3f8
commit abb19dfbf8
5 changed files with 29 additions and 21 deletions

View File

@ -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)

26
log.go
View File

@ -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...)
}
}

View File

@ -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{

View File

@ -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
}

View File

@ -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)
}