From 1bcdc124ab50c9efdef1dc9c610923c5da3ec029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 5 Feb 2022 22:27:18 +0200 Subject: [PATCH] [common] add ability to pass env to invoker commands via context --- internal/common/common.go | 10 ++++++++++ 1 file changed, 10 insertions(+) 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