From c69c0725bb3fab25e48e57b8de299364c159d056 Mon Sep 17 00:00:00 2001 From: Peter Hsu Date: Wed, 31 May 2017 12:33:29 -0700 Subject: [PATCH] A new entry point for calling the cred helpers that allow passing environment variables Signed-off-by: Peter Hsu --- client/command.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/client/command.go b/client/command.go index a144d5a..8da3343 100644 --- a/client/command.go +++ b/client/command.go @@ -1,6 +1,7 @@ package client import ( + "fmt" "io" "os" "os/exec" @@ -17,15 +18,26 @@ type ProgramFunc func(args ...string) Program // NewShellProgramFunc creates programs that are executed in a Shell. func NewShellProgramFunc(name string) ProgramFunc { + return NewShellProgramFuncWithEnv(name, nil) +} + +// NewShellProgramFuncWithEnv creates programs that are executed in a Shell with environment variables +func NewShellProgramFuncWithEnv(name string, env *map[string]string) ProgramFunc { return func(args ...string) Program { - return &Shell{cmd: newCmdRedirectErr(name, args)} + return &Shell{cmd: createProgramCmdRedirectErr(name, args, env)} } } -func newCmdRedirectErr(name string, args []string) *exec.Cmd { - newCmd := exec.Command(name, args...) - newCmd.Stderr = os.Stderr - return newCmd +func createProgramCmdRedirectErr(commandName string, args []string, env *map[string]string) *exec.Cmd { + programCmd := exec.Command(commandName, args...) + programCmd.Env = os.Environ() + if env != nil { + for k, v := range *env { + programCmd.Env = append(programCmd.Env, fmt.Sprintf("%s=%s", k, v)) + } + } + programCmd.Stderr = os.Stderr + return programCmd } // Shell invokes shell commands to talk with a remote credentials helper.