Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
add *exec.Cmd support
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Jul 28, 2019
1 parent 711e16f commit 86162eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
28 changes: 25 additions & 3 deletions executor.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package runn

import (
"bytes"
"fmt"
"os/exec"
)

// Executor do the code statement execution
type Executor struct {
StopWhenError bool
Expand All @@ -19,10 +25,26 @@ func (e Executor) Execute(stmts ...interface{}) (err error) {
case Runner:
runner := stmt.(Runner)
runErr := runner.Run()
if e.StopWhenError {
return runErr
if runErr != nil {
if e.StopWhenError {
return runErr
}
errs.Add(runErr)
}
case *exec.Cmd:
execCmd := stmt.(*exec.Cmd)
buf := bytes.Buffer{}
execCmd.Stdout = &buf
execCmd.Stderr = &buf

runErr := execCmd.Run()
if err != nil {
runErr = fmt.Errorf("%s: %s", err.Error(), buf.String())
if e.StopWhenError {
return runErr
}
errs.Add(runErr)
}
errs.Add(runErr)
}
}
if len(errs) > 0 {
Expand Down
9 changes: 6 additions & 3 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runn_test

import (
"errors"
"os/exec"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -29,8 +30,9 @@ func TestExecutor_All(t *testing.T) {
RunnerImplementationWithError{},
errors.New("error1"),
errors.New("error2"),
exec.Command("wrong-command", "bad-argument"),
},
errors.New("some-error; error1; error2"),
errors.New("some-error; error1; error2; exec: \"wrong-command\": executable file not found in $PATH"),
},
{
true,
Expand All @@ -54,9 +56,10 @@ func TestExecutor_All(t *testing.T) {
false,
[]interface{}{
RunnerImplementationNoError{},
RunnerImplementationWithError{},
nil,
exec.Command("echo", "hello", "world"),
},
errors.New("some-error"),
nil,
},
}

Expand Down

0 comments on commit 86162eb

Please sign in to comment.