From 27bc1ca5d17a1f401f4be0829cb5f9cb3fefc1ec Mon Sep 17 00:00:00 2001 From: Ross Hammermeister Date: Wed, 13 Nov 2019 13:50:04 -0700 Subject: [PATCH] Add flag to allow tasks provided on the command line to be run in parallel --- cmd/task/task.go | 3 +++ task.go | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index b71023ac..3ae8dcd8 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -58,6 +58,7 @@ func main() { silent bool dry bool summary bool + parallel bool dir string entrypoint string output string @@ -71,6 +72,7 @@ func main() { pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task") pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode") pflag.BoolVarP(&silent, "silent", "s", false, "disables echoing") + pflag.BoolVarP(¶llel, "parallel", "p", false, "executes tasks provided on command line in parallel") pflag.BoolVar(&dry, "dry", false, "compiles and prints tasks in the order that they would be run, without executing them") pflag.BoolVar(&summary, "summary", false, "show summary about a task") pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution") @@ -114,6 +116,7 @@ func main() { Dry: dry, Entrypoint: entrypoint, Summary: summary, + Parallel: parallel, Stdin: os.Stdin, Stdout: os.Stdout, diff --git a/task.go b/task.go index 83198192..1c3cd383 100644 --- a/task.go +++ b/task.go @@ -41,6 +41,7 @@ type Executor struct { Silent bool Dry bool Summary bool + Parallel bool Stdin io.Reader Stdout io.Writer @@ -77,12 +78,18 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error { return e.watchTasks(calls...) } + g, ctx := errgroup.WithContext(ctx) for _, c := range calls { - if err := e.RunTask(ctx, c); err != nil { - return err + c := c + if e.Parallel { + g.Go(func() error { return e.RunTask(ctx, c) }) + } else { + if err := e.RunTask(ctx, c); err != nil { + return err + } } } - return nil + return g.Wait() } // Setup setups Executor's internal state