@@ -20,6 +20,7 @@ import (
20
20
"io"
21
21
"os"
22
22
"os/exec"
23
+ "sync"
23
24
24
25
"github.com/arduino/go-paths-helper"
25
26
"github.com/pkg/errors"
@@ -30,6 +31,28 @@ type Process struct {
30
31
cmd * exec.Cmd
31
32
}
32
33
34
+ var globalEnv struct {
35
+ env []string
36
+ lock sync.Mutex
37
+ }
38
+
39
+ // SetProcessGlobalEnv set the environment variables that must be set for
40
+ // all the processes started via executils
41
+ func SetProcessGlobalEnv (env []string ) {
42
+ globalEnv .lock .Lock ()
43
+ globalEnv .env = env
44
+ globalEnv .lock .Unlock ()
45
+ }
46
+
47
+ // GetProcessGlobalEnv return the environment variables that are set for
48
+ // all the processes started via executils (it can be changed with executils.SetProcessGlobalEnv)
49
+ func GetProcessGlobalEnv () []string {
50
+ globalEnv .lock .Lock ()
51
+ defer globalEnv .lock .Unlock ()
52
+ var res []string
53
+ return append (res , globalEnv .env ... ) // copy array
54
+ }
55
+
33
56
// NewProcess creates a command with the provided command line arguments.
34
57
// The first argument is the path to the executable, the remainder are the
35
58
// arguments to the command.
@@ -40,6 +63,7 @@ func NewProcess(args ...string) (*Process, error) {
40
63
p := & Process {
41
64
cmd : exec .Command (args [0 ], args [1 :]... ),
42
65
}
66
+ p .cmd .Env = GetProcessGlobalEnv ()
43
67
TellCommandNotToSpawnShell (p .cmd )
44
68
45
69
// This is required because some tools detects if the program is running
@@ -140,7 +164,7 @@ func (p *Process) Run() error {
140
164
141
165
// SetEnvironment set the enviroment for the running process. Each entry is of the form "key=value".
142
166
func (p * Process ) SetEnvironment (values []string ) {
143
- p .cmd .Env = values
167
+ p .cmd .Env = append ( GetProcessGlobalEnv (), values ... )
144
168
}
145
169
146
170
// RunWithinContext starts the specified command and waits for it to complete. If the given context
0 commit comments