-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from k1LoW/exec-runner
Add exec runner
- Loading branch information
Showing
9 changed files
with
257 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package runn | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/cli/safeexec" | ||
"github.com/k1LoW/exec" | ||
) | ||
|
||
const execRunnerKey = "exec" | ||
|
||
type execRunner struct { | ||
operator *operator | ||
} | ||
|
||
type execCommand struct { | ||
command string | ||
stdin string | ||
} | ||
|
||
func newExecRunner(o *operator) (*execRunner, error) { | ||
return &execRunner{ | ||
operator: o, | ||
}, nil | ||
} | ||
|
||
func (rnr *execRunner) Run(ctx context.Context, c *execCommand) error { | ||
stdout := new(bytes.Buffer) | ||
stderr := new(bytes.Buffer) | ||
if rnr.operator.debug { | ||
_, _ = fmt.Fprintf(os.Stderr, "-----START COMMAND-----\n%s\n-----END COMMAND-----\n", c.command) | ||
} | ||
sh, err := safeexec.LookPath("sh") | ||
if err != nil { | ||
return err | ||
} | ||
cmd := exec.CommandContext(ctx, sh, "-c", c.command) | ||
if strings.Trim(c.stdin, " \n") != "" { | ||
cmd.Stdin = strings.NewReader(c.stdin) | ||
if rnr.operator.debug { | ||
_, _ = fmt.Fprintf(os.Stderr, "-----START STDIN-----\n%s\n-----END STDIN-----\n", c.stdin) | ||
} | ||
} | ||
cmd.Stdout = stdout | ||
cmd.Stderr = stderr | ||
_ = cmd.Run() | ||
if rnr.operator.debug { | ||
_, _ = fmt.Fprintf(os.Stderr, "-----START STDOUT-----\n%s\n-----END STDOUT-----\n", stdout.String()) | ||
_, _ = fmt.Fprintf(os.Stderr, "-----START STDERR-----\n%s\n-----END STDERR-----\n", stderr.String()) | ||
} | ||
rnr.operator.store.steps = append(rnr.operator.store.steps, map[string]interface{}{ | ||
"stdout": stdout.String(), | ||
"stderr": stderr.String(), | ||
"exit_code": cmd.ProcessState.ExitCode(), | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package runn | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestExecRun(t *testing.T) { | ||
tests := []struct { | ||
command string | ||
stdin string | ||
want map[string]interface{} | ||
}{ | ||
{"echo hello!!", "", map[string]interface{}{ | ||
"stdout": "hello!!\n", | ||
"stderr": "", | ||
"exit_code": 0, | ||
}}, | ||
{"cat", "hello!!", map[string]interface{}{ | ||
"stdout": "hello!!", | ||
"stderr": "", | ||
"exit_code": 0, | ||
}}, | ||
} | ||
ctx := context.Background() | ||
for _, tt := range tests { | ||
o, err := New() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
r, err := newExecRunner(o) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
c := &execCommand{command: tt.command, stdin: tt.stdin} | ||
if err := r.Run(ctx, c); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
got := o.store.steps[0] | ||
if diff := cmp.Diff(got, tt.want, nil); diff != "" { | ||
t.Errorf("%s", diff) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
desc: Exec test | ||
steps: | ||
- | ||
exec: | ||
command: echo hello world!! | ||
- | ||
test: 'steps[0].stdout contains "hello"' | ||
- | ||
exec: | ||
command: cat | ||
stdin: '{{ steps[0].stdout }}' | ||
- | ||
test: 'steps[2].stdout contains "hello"' |