diff --git a/task.go b/task.go index 692331e4..5fae5c4e 100644 --- a/task.go +++ b/task.go @@ -77,20 +77,20 @@ func Run() { } for _, a := range args { - if err = RunTask(a); err != nil { + if err = RunTask(context.Background(), a); err != nil { log.Fatal(err) } } } // RunTask runs a task by its name -func RunTask(name string) error { +func RunTask(ctx context.Context, name string) error { t, ok := Tasks[name] if !ok { return &taskNotFoundError{name} } - if err := t.runDeps(); err != nil { + if err := t.runDeps(ctx); err != nil { return err } @@ -100,20 +100,20 @@ func RunTask(name string) error { } for i := range t.Cmds { - if err := t.runCommand(i); err != nil { + if err := t.runCommand(ctx, i); err != nil { return &taskRunError{name, err} } } return nil } -func (t *Task) runDeps() error { +func (t *Task) runDeps(ctx context.Context) error { vars, err := t.handleVariables() if err != nil { return err } - var g errgroup.Group + g, ctx := errgroup.WithContext(ctx) for _, d := range t.Deps { dep := d @@ -124,7 +124,7 @@ func (t *Task) runDeps() error { return err } - if err = RunTask(dep); err != nil { + if err = RunTask(ctx, dep); err != nil { return err } return nil @@ -155,7 +155,7 @@ func (t *Task) isUpToDate() bool { return generatesMinTime.After(sourcesMaxTime) } -func (t *Task) runCommand(i int) error { +func (t *Task) runCommand(ctx context.Context, i int) error { vars, err := t.handleVariables() if err != nil { return err @@ -167,7 +167,7 @@ func (t *Task) runCommand(i int) error { if strings.HasPrefix(c, "^") { c = strings.TrimPrefix(c, "^") - if err = RunTask(c); err != nil { + if err = RunTask(ctx, c); err != nil { return err } return nil @@ -177,7 +177,7 @@ func (t *Task) runCommand(i int) error { if err != nil { return err } - cmd := execext.NewCommand(context.Background(), c) + cmd := execext.NewCommand(ctx, c) if dir != "" { cmd.Dir = dir } diff --git a/watch.go b/watch.go index 8117276c..0e9d5181 100644 --- a/watch.go +++ b/watch.go @@ -1,6 +1,7 @@ package task import ( + "context" "fmt" "log" "strings" @@ -16,7 +17,7 @@ func WatchTasks(args []string) error { // run tasks on init for _, a := range args { - if err := RunTask(a); err != nil { + if err := RunTask(context.Background(), a); err != nil { fmt.Println(err) break } @@ -42,7 +43,7 @@ loop: select { case <-watcher.Events: for _, a := range args { - if err := RunTask(a); err != nil { + if err := RunTask(context.Background(), a); err != nil { fmt.Println(err) continue loop }