@@ -190,6 +190,12 @@ func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
190190 return cli .RunWithCustomEnv (cli .cliEnvVars , args ... )
191191}
192192
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+
193199// GetDefaultEnv returns a copy of the default execution env used with the Run method.
194200func (cli * ArduinoCLI ) GetDefaultEnv () map [string ]string {
195201 res := map [string ]string {}
@@ -324,8 +330,13 @@ func (cli *ArduinoCLI) InstallMockedAvrdude(t *testing.T) {
324330
325331// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
326332func (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 ) {
327338 var stdoutBuf , stderrBuf bytes.Buffer
328- err := cli .run (& stdoutBuf , & stderrBuf , nil , env , args ... )
339+ err := cli .run (ctx , & stdoutBuf , & stderrBuf , nil , env , args ... )
329340
330341 errBuf := stderrBuf .Bytes ()
331342 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) (
336347// RunWithCustomInput executes the given arduino-cli command pushing the given input stream and returns the output.
337348func (cli * ArduinoCLI ) RunWithCustomInput (in io.Reader , args ... string ) ([]byte , []byte , error ) {
338349 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 ... )
340351
341352 errBuf := stderrBuf .Bytes ()
342353 cli .t .NotContains (string (errBuf ), "panic: runtime error:" , "arduino-cli panicked" )
343354
344355 return stdoutBuf .Bytes (), errBuf , err
345356}
346357
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 {
348359 if cli .cliConfigPath != nil {
349360 args = append ([]string {"--config-file" , cli .cliConfigPath .String ()}, args ... )
350361 }
@@ -402,8 +413,8 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
402413 }
403414 }()
404415 }
416+ cliErr := cliProc .WaitWithinContext (ctx )
405417 wg .Wait ()
406- cliErr := cliProc .Wait ()
407418 fmt .Fprintln (terminalOut , color .HiBlackString ("<<< Run completed (err = %v)" , cliErr ))
408419
409420 return cliErr
0 commit comments