diff --git a/internal/compiler/v1/compiler_v1.go b/internal/compiler/v1/compiler_v1.go index 7c56c57e..4213a4b3 100644 --- a/internal/compiler/v1/compiler_v1.go +++ b/internal/compiler/v1/compiler_v1.go @@ -2,6 +2,7 @@ package v1 import ( "bytes" + "context" "fmt" "strings" "sync" @@ -121,7 +122,7 @@ func (c *CompilerV1) HandleDynamicVar(v taskfile.Var) (string, error) { Stdout: &stdout, Stderr: c.Logger.Stderr, } - if err := execext.RunCommand(opts); err != nil { + if err := execext.RunCommand(context.Background(), opts); err != nil { return "", fmt.Errorf(`task: Command "%s" in taskvars file failed: %s`, opts.Command, err) } diff --git a/internal/compiler/v2/compiler_v2.go b/internal/compiler/v2/compiler_v2.go index 1251ba32..7cad6c90 100644 --- a/internal/compiler/v2/compiler_v2.go +++ b/internal/compiler/v2/compiler_v2.go @@ -2,6 +2,7 @@ package v2 import ( "bytes" + "context" "fmt" "strings" "sync" @@ -93,7 +94,7 @@ func (c *CompilerV2) HandleDynamicVar(v taskfile.Var) (string, error) { Stdout: &stdout, Stderr: c.Logger.Stderr, } - if err := execext.RunCommand(opts); err != nil { + if err := execext.RunCommand(context.Background(), opts); err != nil { return "", fmt.Errorf(`task: Command "%s" in taskvars file failed: %s`, opts.Command, err) } diff --git a/internal/execext/exec.go b/internal/execext/exec.go index f86003bb..935d4d1c 100644 --- a/internal/execext/exec.go +++ b/internal/execext/exec.go @@ -13,7 +13,6 @@ import ( // RunCommandOptions is the options for the RunCommand func type RunCommandOptions struct { - Context context.Context Command string Dir string Env []string @@ -28,7 +27,7 @@ var ( ) // RunCommand runs a shell command -func RunCommand(opts *RunCommandOptions) error { +func RunCommand(ctx context.Context, opts *RunCommandOptions) error { if opts == nil { return ErrNilOptions } @@ -47,20 +46,27 @@ func RunCommand(opts *RunCommandOptions) error { return err } - r := interp.Runner{ - Context: opts.Context, - Dir: opts.Dir, - Env: env, + r, err := interp.New( + interp.Dir(opts.Dir), + interp.Env(env), - Exec: interp.DefaultExec, - Open: interp.OpenDevImpls(interp.DefaultOpen), + interp.Module(interp.DefaultExec), + interp.Module(interp.OpenDevImpls(interp.DefaultOpen)), - Stdin: opts.Stdin, - Stdout: opts.Stdout, - Stderr: opts.Stderr, - } - if err = r.Reset(); err != nil { + interp.StdIO(opts.Stdin, opts.Stdout, opts.Stderr), + ) + if err != nil { return err } - return r.Run(p) + return r.Run(ctx, p) +} + +// IsExitError returns true the given error is an exis status error +func IsExitError(err error) bool { + switch err.(type) { + case interp.ExitStatus, interp.ShellExitStatus: + return true + default: + return false + } } diff --git a/status.go b/status.go index d2dabd37..853ae246 100644 --- a/status.go +++ b/status.go @@ -71,8 +71,7 @@ func getStatusChecker(t *taskfile.Task) (status.Checker, error) { func isTaskUpToDateStatus(ctx context.Context, t *taskfile.Task) (bool, error) { for _, s := range t.Status { - err := execext.RunCommand(&execext.RunCommandOptions{ - Context: ctx, + err := execext.RunCommand(ctx, &execext.RunCommandOptions{ Command: s, Dir: t.Dir, Env: getEnviron(t), diff --git a/task.go b/task.go index ce4d2d1d..e9e29045 100644 --- a/task.go +++ b/task.go @@ -19,7 +19,6 @@ import ( "github.com/Masterminds/semver" "golang.org/x/sync/errgroup" - "mvdan.cc/sh/interp" ) const ( @@ -198,7 +197,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error { e.Logger.VerboseErrf("task: error cleaning status on error: %v", err2) } - if _, ok := err.(interp.ExitCode); ok && t.IgnoreError { + if execext.IsExitError(err) && t.IgnoreError { e.Logger.VerboseErrf("task: task error ignored: %v", err) continue } @@ -243,8 +242,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi defer stdOut.Close() defer stdErr.Close() - err := execext.RunCommand(&execext.RunCommandOptions{ - Context: ctx, + err := execext.RunCommand(ctx, &execext.RunCommandOptions{ Command: cmd.Cmd, Dir: t.Dir, Env: getEnviron(t), @@ -252,7 +250,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi Stdout: stdOut, Stderr: stdErr, }) - if _, ok := err.(interp.ExitCode); ok && cmd.IgnoreError { + if execext.IsExitError(err) && cmd.IgnoreError { e.Logger.VerboseErrf("task: command error ignored: %v", err) return nil }