Skip to content

Commit d5e42d9

Browse files
committed
Add support for global env variable set over arduino-cli
1 parent 3b206ef commit d5e42d9

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Diff for: executils/process.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io"
2121
"os"
2222
"os/exec"
23+
"sync"
2324

2425
"github.com/arduino/go-paths-helper"
2526
"github.com/pkg/errors"
@@ -30,6 +31,28 @@ type Process struct {
3031
cmd *exec.Cmd
3132
}
3233

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+
3356
// NewProcess creates a command with the provided command line arguments.
3457
// The first argument is the path to the executable, the remainder are the
3558
// arguments to the command.
@@ -40,6 +63,7 @@ func NewProcess(args ...string) (*Process, error) {
4063
p := &Process{
4164
cmd: exec.Command(args[0], args[1:]...),
4265
}
66+
p.cmd.Env = GetProcessGlobalEnv()
4367
TellCommandNotToSpawnShell(p.cmd)
4468

4569
// This is required because some tools detects if the program is running
@@ -140,7 +164,7 @@ func (p *Process) Run() error {
140164

141165
// SetEnvironment set the enviroment for the running process. Each entry is of the form "key=value".
142166
func (p *Process) SetEnvironment(values []string) {
143-
p.cmd.Env = values
167+
p.cmd.Env = append(GetProcessGlobalEnv(), values...)
144168
}
145169

146170
// RunWithinContext starts the specified command and waits for it to complete. If the given context

0 commit comments

Comments
 (0)