diff --git a/internal/common/common.go b/internal/common/common.go index 4f6df9a..9c21adf 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -26,6 +26,11 @@ import ( "time" ) +const ( + // InvokerCtxKeyEnv is a Invoker context value key for setting the command environment, a slice of strings + InvokerCtxKeyEnv = "invoker-env" +) + var ( Timeout = 3 * time.Second ErrTimeout = errors.New("command timed out") @@ -47,6 +52,11 @@ func (i Invoke) Command(name string, arg ...string) ([]byte, error) { func (i Invoke) CommandWithContext(ctx context.Context, name string, arg ...string) ([]byte, error) { cmd := exec.CommandContext(ctx, name, arg...) + env := ctx.Value(InvokerCtxKeyEnv) + if cmdEnv, ok := env.([]string); ok { + cmd.Env = cmdEnv + } + var buf bytes.Buffer cmd.Stdout = &buf cmd.Stderr = &buf