@@ -66,7 +66,7 @@ type ArduinoCLI struct {
66
66
path * paths.Path
67
67
t * require.Assertions
68
68
proc * executils.Process
69
- cliEnvVars [ ]string
69
+ cliEnvVars map [ string ]string
70
70
cliConfigPath * paths.Path
71
71
stagingDir * paths.Path
72
72
dataDir * paths.Path
@@ -97,11 +97,11 @@ func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoC
97
97
cli .stagingDir = env .SharedDownloadsDir ()
98
98
}
99
99
100
- cli .cliEnvVars = [ ]string {
101
- "LANG= en" ,
102
- fmt . Sprintf ( "ARDUINO_DATA_DIR=%s" , cli .dataDir ),
103
- fmt . Sprintf ( "ARDUINO_DOWNLOADS_DIR=%s" , cli .stagingDir ),
104
- fmt . Sprintf ( "ARDUINO_SKETCHBOOK_DIR=%s" , cli .sketchbookDir ),
100
+ cli .cliEnvVars = map [ string ]string {
101
+ "LANG" : " en" ,
102
+ "ARDUINO_DATA_DIR" : cli .dataDir . String ( ),
103
+ "ARDUINO_DOWNLOADS_DIR" : cli .stagingDir . String ( ),
104
+ "ARDUINO_SKETCHBOOK_DIR" : cli .sketchbookDir . String ( ),
105
105
}
106
106
env .RegisterCleanUpCallback (cli .CleanUp )
107
107
return cli
@@ -128,11 +128,35 @@ func (cli *ArduinoCLI) SketchbookDir() *paths.Path {
128
128
129
129
// Run executes the given arduino-cli command and returns the output.
130
130
func (cli * ArduinoCLI ) Run (args ... string ) ([]byte , []byte , error ) {
131
+ return cli .RunWithCustomEnv (cli .cliEnvVars , args ... )
132
+ }
133
+
134
+ // GetDefaultEnv returns a copy of the default execution env used with the Run method.
135
+ func (cli * ArduinoCLI ) GetDefaultEnv () map [string ]string {
136
+ res := map [string ]string {}
137
+ for k , v := range cli .cliEnvVars {
138
+ res [k ] = v
139
+ }
140
+ return res
141
+ }
142
+
143
+ // convertEnvForExecutils returns a string array made of "key=value" strings
144
+ // with (key,value) pairs obtained from the given map.
145
+ func (cli * ArduinoCLI ) convertEnvForExecutils (env map [string ]string ) []string {
146
+ envVars := []string {}
147
+ for k , v := range env {
148
+ envVars = append (envVars , fmt .Sprintf ("%s=%s" , k , v ))
149
+ }
150
+ return envVars
151
+ }
152
+
153
+ // RunWithCustomEnv executes the given arduino-cli command adding the given custom env and returns the output.
154
+ func (cli * ArduinoCLI ) RunWithCustomEnv (env map [string ]string , args ... string ) ([]byte , []byte , error ) {
131
155
if cli .cliConfigPath != nil {
132
156
args = append ([]string {"--config-file" , cli .cliConfigPath .String ()}, args ... )
133
157
}
134
158
fmt .Println (color .HiBlackString (">>> Running: " ) + color .HiYellowString ("%s %s" , cli .path , strings .Join (args , " " )))
135
- cliProc , err := executils .NewProcessFromPath (cli .cliEnvVars , cli .path , args ... )
159
+ cliProc , err := executils .NewProcessFromPath (cli .convertEnvForExecutils ( env ) , cli .path , args ... )
136
160
cli .t .NoError (err )
137
161
stdout , err := cliProc .StdoutPipe ()
138
162
cli .t .NoError (err )
@@ -177,7 +201,7 @@ func (cli *ArduinoCLI) StartDaemon(verbose bool) string {
177
201
if verbose {
178
202
args = append (args , "-v" , "--log-level" , "debug" )
179
203
}
180
- cliProc , err := executils .NewProcessFromPath (cli .cliEnvVars , cli .path , args ... )
204
+ cliProc , err := executils .NewProcessFromPath (cli .convertEnvForExecutils ( cli . cliEnvVars ) , cli .path , args ... )
181
205
cli .t .NoError (err )
182
206
stdout , err := cliProc .StdoutPipe ()
183
207
cli .t .NoError (err )
0 commit comments