forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
58 lines (46 loc) · 1.12 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package runn
import (
"bytes"
"context"
"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)
rnr.operator.capturers.captureExecCommand(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)
rnr.operator.capturers.captureExecStdin(c.stdin)
}
cmd.Stdout = stdout
cmd.Stderr = stderr
_ = cmd.Run()
rnr.operator.capturers.captureExecStdout(stdout.String())
rnr.operator.capturers.captureExecStderr(stderr.String())
rnr.operator.record(map[string]interface{}{
"stdout": stdout.String(),
"stderr": stderr.String(),
"exit_code": cmd.ProcessState.ExitCode(),
})
return nil
}