@@ -30,16 +30,19 @@ type Process struct {
30
30
cmd * exec.Cmd
31
31
}
32
32
33
- // NewProcess creates a command with the provided command line arguments.
34
- // The first argument is the path to the executable, the remainder are the
35
- // arguments to the command.
36
- func NewProcess (args ... string ) (* Process , error ) {
33
+ // NewProcess creates a command with the provided command line arguments
34
+ // and environment variables (that will be added to the parent os.Environ).
35
+ // The first argument args[0] is the path to the executable, the remainder
36
+ // are the arguments to the command.
37
+ func NewProcess (extraEnv []string , args ... string ) (* Process , error ) {
37
38
if len (args ) == 0 {
38
39
return nil , errors .New (tr ("no executable specified" ))
39
40
}
40
41
p := & Process {
41
42
cmd : exec .Command (args [0 ], args [1 :]... ),
42
43
}
44
+ p .cmd .Env = append (p .cmd .Env , os .Environ ()... )
45
+ p .cmd .Env = append (p .cmd .Env , extraEnv ... )
43
46
TellCommandNotToSpawnShell (p .cmd )
44
47
45
48
// This is required because some tools detects if the program is running
@@ -49,12 +52,12 @@ func NewProcess(args ...string) (*Process, error) {
49
52
return p , nil
50
53
}
51
54
52
- // NewProcessFromPath creates a command from the provided executable path and
53
- // command line arguments.
54
- func NewProcessFromPath (executable * paths.Path , args ... string ) (* Process , error ) {
55
+ // NewProcessFromPath creates a command from the provided executable path,
56
+ // additional environemnt vars and command line arguments.
57
+ func NewProcessFromPath (extraEnv [] string , executable * paths.Path , args ... string ) (* Process , error ) {
55
58
processArgs := []string {executable .String ()}
56
59
processArgs = append (processArgs , args ... )
57
- return NewProcess (processArgs ... )
60
+ return NewProcess (extraEnv , processArgs ... )
58
61
}
59
62
60
63
// RedirectStdoutTo will redirect the process' stdout to the specified
@@ -140,7 +143,9 @@ func (p *Process) Run() error {
140
143
141
144
// SetEnvironment set the enviroment for the running process. Each entry is of the form "key=value".
142
145
func (p * Process ) SetEnvironment (values []string ) {
143
- p .cmd .Env = values
146
+ p .cmd .Env = nil
147
+ p .cmd .Env = append (p .cmd .Env , os .Environ ()... )
148
+ p .cmd .Env = append (p .cmd .Env , values ... )
144
149
}
145
150
146
151
// RunWithinContext starts the specified command and waits for it to complete. If the given context
0 commit comments