Skip to content

Commit 98776d5

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

File tree

9 files changed

+23
-18
lines changed

9 files changed

+23
-18
lines changed

arduino/cores/packagemanager/install_uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (pm *PackageManager) RunPostInstallScript(platformRelease *cores.PlatformRe
7070
}
7171
postInstall := platformRelease.InstallDir.Join(postInstallFilename)
7272
if postInstall.Exist() && postInstall.IsNotDir() {
73-
cmd, err := executils.NewProcessFromPath(postInstall)
73+
cmd, err := executils.NewProcessFromPath(nil, postInstall)
7474
if err != nil {
7575
return err
7676
}

arduino/discovery/discovery.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (disc *PluggableDiscovery) sendCommand(command string) error {
235235

236236
func (disc *PluggableDiscovery) runProcess() error {
237237
logrus.Infof("starting discovery %s process", disc.id)
238-
proc, err := executils.NewProcess(disc.processArgs...)
238+
proc, err := executils.NewProcess(nil, disc.processArgs...)
239239
if err != nil {
240240
return err
241241
}

arduino/discovery/discovery_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func TestDiscoveryStdioHandling(t *testing.T) {
2828
// Build `cat` helper inside testdata/cat
29-
builder, err := executils.NewProcess("go", "build")
29+
builder, err := executils.NewProcess(nil, "go", "build")
3030
require.NoError(t, err)
3131
builder.SetDir("testdata/cat")
3232
require.NoError(t, builder.Run())

arduino/monitor/monitor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (mon *PluggableMonitor) sendCommand(command string) error {
171171

172172
func (mon *PluggableMonitor) runProcess() error {
173173
mon.log.Infof("Starting monitor process")
174-
proc, err := executils.NewProcess(mon.processArgs...)
174+
proc, err := executils.NewProcess(nil, mon.processArgs...)
175175
if err != nil {
176176
return err
177177
}

arduino/monitor/monitor_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestDummyMonitor(t *testing.T) {
3333
// Build `dummy-monitor` helper inside testdata/dummy-monitor
3434
testDataDir, err := paths.New("testdata").Abs()
3535
require.NoError(t, err)
36-
builder, err := executils.NewProcess("go", "install", "github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor@main")
36+
builder, err := executils.NewProcess(nil, "go", "install", "github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor@main")
3737
fmt.Println(testDataDir.String())
3838
env := os.Environ()
3939
env = append(env, "GOBIN="+testDataDir.String())

commands/debug/debug.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func Debug(ctx context.Context, req *dbg.DebugConfigRequest, inStream io.Reader,
6262
}
6363
entry.Debug("Executing debugger")
6464

65-
cmd, err := executils.NewProcess(commandLine...)
65+
cmd, err := executils.NewProcess(nil, commandLine...)
6666
if err != nil {
6767
return nil, &arduino.FailedDebugError{Message: tr("Cannot execute debug tool"), Cause: err}
6868
}

commands/upload/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ func runTool(recipeID string, props *properties.Map, outStream, errStream io.Wri
577577
if dryRun {
578578
return nil
579579
}
580-
cmd, err := executils.NewProcess(cmdArgs...)
580+
cmd, err := executils.NewProcess(nil, cmdArgs...)
581581
if err != nil {
582582
return fmt.Errorf(tr("cannot execute upload tool: %s"), err)
583583
}

executils/process.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ type Process struct {
3030
cmd *exec.Cmd
3131
}
3232

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) {
3738
if len(args) == 0 {
3839
return nil, errors.New(tr("no executable specified"))
3940
}
4041
p := &Process{
4142
cmd: exec.Command(args[0], args[1:]...),
4243
}
44+
p.cmd.Env = append(p.cmd.Env, os.Environ()...)
45+
p.cmd.Env = append(p.cmd.Env, extraEnv...)
4346
TellCommandNotToSpawnShell(p.cmd)
4447

4548
// This is required because some tools detects if the program is running
@@ -49,12 +52,12 @@ func NewProcess(args ...string) (*Process, error) {
4952
return p, nil
5053
}
5154

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) {
5558
processArgs := []string{executable.String()}
5659
processArgs = append(processArgs, args...)
57-
return NewProcess(processArgs...)
60+
return NewProcess(extraEnv, processArgs...)
5861
}
5962

6063
// RedirectStdoutTo will redirect the process' stdout to the specified
@@ -140,7 +143,9 @@ func (p *Process) Run() error {
140143

141144
// SetEnvironment set the enviroment for the running process. Each entry is of the form "key=value".
142145
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...)
144149
}
145150

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

executils/process_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import (
2525

2626
func TestProcessWithinContext(t *testing.T) {
2727
// Build `delay` helper inside testdata/delay
28-
builder, err := NewProcess("go", "build")
28+
builder, err := NewProcess(nil, "go", "build")
2929
require.NoError(t, err)
3030
builder.SetDir("testdata/delay")
3131
require.NoError(t, builder.Run())
3232

3333
// Run delay and test if the process is terminated correctly due to context
34-
process, err := NewProcess("testdata/delay/delay")
34+
process, err := NewProcess(nil, "testdata/delay/delay")
3535
require.NoError(t, err)
3636
start := time.Now()
3737
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)

0 commit comments

Comments
 (0)