Skip to content

Commit

Permalink
Merge pull request #6 from rantav/split-by-newline
Browse files Browse the repository at this point in the history
fix(shell): Split the shell line by newlines and execute each separately
  • Loading branch information
rantav committed May 14, 2020
2 parents 99b62ce + aa50267 commit 3604de3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
12 changes: 9 additions & 3 deletions operations/shell-operator.go
@@ -1,8 +1,10 @@
package operations

import (
"bufio"
"bytes"
"os/exec"
"strings"

"github.com/pkg/errors"

Expand All @@ -19,9 +21,13 @@ func newShellOperator(spec OperationSpec) *shellOperation {
}

func (o *shellOperation) Operate() error {
for _, line := range o.sh {
if err := executeShell(line); err != nil {
return err
for _, command := range o.sh {
scanner := bufio.NewScanner(strings.NewReader(command))
for scanner.Scan() {
line := scanner.Text()
if err := executeShell(line); err != nil {
return err
}
}
}
return nil
Expand Down
21 changes: 21 additions & 0 deletions operations/shell-operator_test.go
Expand Up @@ -36,9 +36,30 @@ func TestShellOperatorOperate(t *testing.T) {
require.NoError(t, err)
}

func TestShellOperatorOperateMultiline(t *testing.T) {
o := newShellOperator(OperationSpec{Sh: []string{"echo hello\necho world"}})
require.NotNil(t, o)
err := o.Operate()
require.NoError(t, err)
}

func TestShellOperatorOperateAndFail(t *testing.T) {
o := newShellOperator(OperationSpec{Sh: []string{"no-such-command really"}})
require.NotNil(t, o)
err := o.Operate()
require.Error(t, err)
}

func TestShellOperatorOperateMultilineFail(t *testing.T) {
o := newShellOperator(OperationSpec{Sh: []string{"no-such-command at all\necho well"}})
require.NotNil(t, o)
err := o.Operate()
require.Error(t, err)
}

func TestShellOperatorOperateMultilineFail2(t *testing.T) {
o := newShellOperator(OperationSpec{Sh: []string{"echo ok\nno-such-command at all"}})
require.NotNil(t, o)
err := o.Operate()
require.Error(t, err)
}

0 comments on commit 3604de3

Please sign in to comment.