Skip to content

Commit 6338c06

Browse files
authored
Merge pull request #69 from shhsu/command_env
A new entry point for calling the cred helpers that allow passing environment variables
2 parents a8de4f6 + c69c072 commit 6338c06

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

client/command.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
"fmt"
45
"io"
56
"os"
67
"os/exec"
@@ -17,15 +18,26 @@ type ProgramFunc func(args ...string) Program
1718

1819
// NewShellProgramFunc creates programs that are executed in a Shell.
1920
func NewShellProgramFunc(name string) ProgramFunc {
21+
return NewShellProgramFuncWithEnv(name, nil)
22+
}
23+
24+
// NewShellProgramFuncWithEnv creates programs that are executed in a Shell with environment variables
25+
func NewShellProgramFuncWithEnv(name string, env *map[string]string) ProgramFunc {
2026
return func(args ...string) Program {
21-
return &Shell{cmd: newCmdRedirectErr(name, args)}
27+
return &Shell{cmd: createProgramCmdRedirectErr(name, args, env)}
2228
}
2329
}
2430

25-
func newCmdRedirectErr(name string, args []string) *exec.Cmd {
26-
newCmd := exec.Command(name, args...)
27-
newCmd.Stderr = os.Stderr
28-
return newCmd
31+
func createProgramCmdRedirectErr(commandName string, args []string, env *map[string]string) *exec.Cmd {
32+
programCmd := exec.Command(commandName, args...)
33+
programCmd.Env = os.Environ()
34+
if env != nil {
35+
for k, v := range *env {
36+
programCmd.Env = append(programCmd.Env, fmt.Sprintf("%s=%s", k, v))
37+
}
38+
}
39+
programCmd.Stderr = os.Stderr
40+
return programCmd
2941
}
3042

3143
// Shell invokes shell commands to talk with a remote credentials helper.

0 commit comments

Comments
 (0)