@@ -190,6 +190,12 @@ func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
190
190
return cli .RunWithCustomEnv (cli .cliEnvVars , args ... )
191
191
}
192
192
193
+ // RunWithContext executes the given arduino-cli command with the given context and returns the output.
194
+ // If the context is canceled, the command is killed.
195
+ func (cli * ArduinoCLI ) RunWithContext (ctx context.Context , args ... string ) ([]byte , []byte , error ) {
196
+ return cli .RunWithCustomEnvContext (ctx , cli .cliEnvVars , args ... )
197
+ }
198
+
193
199
// GetDefaultEnv returns a copy of the default execution env used with the Run method.
194
200
func (cli * ArduinoCLI ) GetDefaultEnv () map [string ]string {
195
201
res := map [string ]string {}
@@ -324,8 +330,13 @@ func (cli *ArduinoCLI) InstallMockedAvrdude(t *testing.T) {
324
330
325
331
// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
326
332
func (cli * ArduinoCLI ) RunWithCustomEnv (env map [string ]string , args ... string ) ([]byte , []byte , error ) {
333
+ return cli .RunWithCustomEnvContext (context .Background (), env , args ... )
334
+ }
335
+
336
+ // RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
337
+ func (cli * ArduinoCLI ) RunWithCustomEnvContext (ctx context.Context , env map [string ]string , args ... string ) ([]byte , []byte , error ) {
327
338
var stdoutBuf , stderrBuf bytes.Buffer
328
- err := cli .run (& stdoutBuf , & stderrBuf , nil , env , args ... )
339
+ err := cli .run (ctx , & stdoutBuf , & stderrBuf , nil , env , args ... )
329
340
330
341
errBuf := stderrBuf .Bytes ()
331
342
cli .t .NotContains (string (errBuf ), "panic: runtime error:" , "arduino-cli panicked" )
@@ -336,15 +347,15 @@ func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) (
336
347
// RunWithCustomInput executes the given arduino-cli command pushing the given input stream and returns the output.
337
348
func (cli * ArduinoCLI ) RunWithCustomInput (in io.Reader , args ... string ) ([]byte , []byte , error ) {
338
349
var stdoutBuf , stderrBuf bytes.Buffer
339
- err := cli .run (& stdoutBuf , & stderrBuf , in , cli .cliEnvVars , args ... )
350
+ err := cli .run (context . Background (), & stdoutBuf , & stderrBuf , in , cli .cliEnvVars , args ... )
340
351
341
352
errBuf := stderrBuf .Bytes ()
342
353
cli .t .NotContains (string (errBuf ), "panic: runtime error:" , "arduino-cli panicked" )
343
354
344
355
return stdoutBuf .Bytes (), errBuf , err
345
356
}
346
357
347
- func (cli * ArduinoCLI ) run (stdoutBuff , stderrBuff io.Writer , stdinBuff io.Reader , env map [string ]string , args ... string ) error {
358
+ func (cli * ArduinoCLI ) run (ctx context. Context , stdoutBuff , stderrBuff io.Writer , stdinBuff io.Reader , env map [string ]string , args ... string ) error {
348
359
if cli .cliConfigPath != nil {
349
360
args = append ([]string {"--config-file" , cli .cliConfigPath .String ()}, args ... )
350
361
}
@@ -402,8 +413,8 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
402
413
}
403
414
}()
404
415
}
416
+ cliErr := cliProc .WaitWithinContext (ctx )
405
417
wg .Wait ()
406
- cliErr := cliProc .Wait ()
407
418
fmt .Fprintln (terminalOut , color .HiBlackString ("<<< Run completed (err = %v)" , cliErr ))
408
419
409
420
return cliErr
0 commit comments