Skip to content

[breaking] Add env variable to let tools know the cli version and the gRPC client version #1640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 31, 2022
Prev Previous commit
Next Next commit
Add support for global env variable set over arduino-cli
  • Loading branch information
cmaglie committed Jan 27, 2022
commit 98776d5a48e7dd858dfd9187b2d07b7a02d96a7a
2 changes: 1 addition & 1 deletion arduino/cores/packagemanager/install_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (pm *PackageManager) RunPostInstallScript(platformRelease *cores.PlatformRe
}
postInstall := platformRelease.InstallDir.Join(postInstallFilename)
if postInstall.Exist() && postInstall.IsNotDir() {
cmd, err := executils.NewProcessFromPath(postInstall)
cmd, err := executils.NewProcessFromPath(nil, postInstall)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (disc *PluggableDiscovery) sendCommand(command string) error {

func (disc *PluggableDiscovery) runProcess() error {
logrus.Infof("starting discovery %s process", disc.id)
proc, err := executils.NewProcess(disc.processArgs...)
proc, err := executils.NewProcess(nil, disc.processArgs...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion arduino/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func TestDiscoveryStdioHandling(t *testing.T) {
// Build `cat` helper inside testdata/cat
builder, err := executils.NewProcess("go", "build")
builder, err := executils.NewProcess(nil, "go", "build")
require.NoError(t, err)
builder.SetDir("testdata/cat")
require.NoError(t, builder.Run())
Expand Down
2 changes: 1 addition & 1 deletion arduino/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (mon *PluggableMonitor) sendCommand(command string) error {

func (mon *PluggableMonitor) runProcess() error {
mon.log.Infof("Starting monitor process")
proc, err := executils.NewProcess(mon.processArgs...)
proc, err := executils.NewProcess(nil, mon.processArgs...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion arduino/monitor/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestDummyMonitor(t *testing.T) {
// Build `dummy-monitor` helper inside testdata/dummy-monitor
testDataDir, err := paths.New("testdata").Abs()
require.NoError(t, err)
builder, err := executils.NewProcess("go", "install", "github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor@main")
builder, err := executils.NewProcess(nil, "go", "install", "github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor@main")
fmt.Println(testDataDir.String())
env := os.Environ()
env = append(env, "GOBIN="+testDataDir.String())
Expand Down
2 changes: 1 addition & 1 deletion commands/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Debug(ctx context.Context, req *dbg.DebugConfigRequest, inStream io.Reader,
}
entry.Debug("Executing debugger")

cmd, err := executils.NewProcess(commandLine...)
cmd, err := executils.NewProcess(nil, commandLine...)
if err != nil {
return nil, &arduino.FailedDebugError{Message: tr("Cannot execute debug tool"), Cause: err}
}
Expand Down
2 changes: 1 addition & 1 deletion commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func runTool(recipeID string, props *properties.Map, outStream, errStream io.Wri
if dryRun {
return nil
}
cmd, err := executils.NewProcess(cmdArgs...)
cmd, err := executils.NewProcess(nil, cmdArgs...)
if err != nil {
return fmt.Errorf(tr("cannot execute upload tool: %s"), err)
}
Expand Down
23 changes: 14 additions & 9 deletions executils/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ type Process struct {
cmd *exec.Cmd
}

// NewProcess creates a command with the provided command line arguments.
// The first argument is the path to the executable, the remainder are the
// arguments to the command.
func NewProcess(args ...string) (*Process, error) {
// NewProcess creates a command with the provided command line arguments
// and environment variables (that will be added to the parent os.Environ).
// The first argument args[0] is the path to the executable, the remainder
// are the arguments to the command.
func NewProcess(extraEnv []string, args ...string) (*Process, error) {
if len(args) == 0 {
return nil, errors.New(tr("no executable specified"))
}
p := &Process{
cmd: exec.Command(args[0], args[1:]...),
}
p.cmd.Env = append(p.cmd.Env, os.Environ()...)
p.cmd.Env = append(p.cmd.Env, extraEnv...)
TellCommandNotToSpawnShell(p.cmd)

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

// NewProcessFromPath creates a command from the provided executable path and
// command line arguments.
func NewProcessFromPath(executable *paths.Path, args ...string) (*Process, error) {
// NewProcessFromPath creates a command from the provided executable path,
// additional environemnt vars and command line arguments.
func NewProcessFromPath(extraEnv []string, executable *paths.Path, args ...string) (*Process, error) {
processArgs := []string{executable.String()}
processArgs = append(processArgs, args...)
return NewProcess(processArgs...)
return NewProcess(extraEnv, processArgs...)
}

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

// SetEnvironment set the enviroment for the running process. Each entry is of the form "key=value".
func (p *Process) SetEnvironment(values []string) {
p.cmd.Env = values
p.cmd.Env = nil
p.cmd.Env = append(p.cmd.Env, os.Environ()...)
p.cmd.Env = append(p.cmd.Env, values...)
}

// RunWithinContext starts the specified command and waits for it to complete. If the given context
Expand Down
4 changes: 2 additions & 2 deletions executils/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import (

func TestProcessWithinContext(t *testing.T) {
// Build `delay` helper inside testdata/delay
builder, err := NewProcess("go", "build")
builder, err := NewProcess(nil, "go", "build")
require.NoError(t, err)
builder.SetDir("testdata/delay")
require.NoError(t, builder.Run())

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