diff --git a/internal/shell/coreutils.go b/internal/shell/coreutils.go new file mode 100644 index 0000000000000000000000000000000000000000..ad5401d5f98b2f51dcc8b38da357a70e085703c6 --- /dev/null +++ b/internal/shell/coreutils.go @@ -0,0 +1,19 @@ +package shell + +import ( + "os" + "runtime" + "strconv" +) + +var useGoCoreUtils bool + +func init() { + // If CRUSH_CORE_UTILS is set to either true or false, respect that. + // By default, enable on Windows only. + if v, err := strconv.ParseBool(os.Getenv("CRUSH_CORE_UTILS")); err == nil { + useGoCoreUtils = v + } else { + useGoCoreUtils = runtime.GOOS == "windows" + } +} diff --git a/internal/shell/shell.go b/internal/shell/shell.go index 5a10be9537714162e4d5ed25360b42690395793f..ea2ab1c219fc69362602acdc83cc83db79212388 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -241,7 +241,7 @@ func (s *Shell) execPOSIX(ctx context.Context, command string) (string, string, interp.Interactive(false), interp.Env(expand.ListEnviron(s.env...)), interp.Dir(s.cwd), - interp.ExecHandlers(s.blockHandler(), coreutils.ExecHandler), + interp.ExecHandlers(s.execHandlers()...), ) if err != nil { return "", "", fmt.Errorf("could not run command: %w", err) @@ -257,6 +257,16 @@ func (s *Shell) execPOSIX(ctx context.Context, command string) (string, string, return stdout.String(), stderr.String(), err } +func (s *Shell) execHandlers() []func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc { + handlers := []func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc{ + s.blockHandler(), + } + if useGoCoreUtils { + handlers = append(handlers, coreutils.ExecHandler) + } + return handlers +} + // IsInterrupt checks if an error is due to interruption func IsInterrupt(err error) bool { return errors.Is(err, context.Canceled) ||