-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
exec.go
151 lines (134 loc) · 3.48 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package runn
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
"github.com/cli/safeexec"
"github.com/k1LoW/donegroup"
"github.com/k1LoW/exec"
"github.com/mattn/go-shellwords"
)
const execRunnerKey = "exec"
const (
execStoreStdoutKey = "stdout"
execStoreStderrKey = "stderr"
execStoreExitCodeKey = "exit_code"
)
const execDefaultShell = "bash -e -c {0}"
const execSh = "sh -e -c {0}"
const execBash = "bash --noprofile --norc -eo pipefail -c {0}"
type execRunner struct{}
type execCommand struct {
command string
shell string
stdin string
background bool
liveOutput bool
}
func newExecRunner() *execRunner {
return &execRunner{}
}
func (rnr *execRunner) Run(ctx context.Context, s *step) error {
globalScopes.mu.RLock()
if !globalScopes.runExec {
globalScopes.mu.RUnlock()
return errors.New("scope error: exec runner is not allowed. 'run:exec' scope is required")
}
globalScopes.mu.RUnlock()
o := s.parent
e, err := o.expandBeforeRecord(s.execCommand, s)
if err != nil {
return err
}
cmd, ok := e.(map[string]any)
if !ok {
return fmt.Errorf("invalid exec command: %v", e)
}
command, err := parseExecCommand(cmd)
if err != nil {
return fmt.Errorf("invalid exec command: %w", err)
}
if err := rnr.run(ctx, command, s); err != nil {
return err
}
return nil
}
func (rnr *execRunner) run(ctx context.Context, c *execCommand, s *step) error {
o := s.parent
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
switch c.shell {
case "":
c.shell = execDefaultShell
case "sh":
c.shell = execSh
case "bash":
c.shell = execBash
}
o.capturers.captureExecCommand(c.command, c.shell, c.background)
if !strings.Contains(c.shell, "{0}") {
return fmt.Errorf("invalid shell setting. custom shell option requires `{0}`.: %q", c.shell)
}
shWithOpts, err := shellwords.Parse(c.shell)
if err != nil {
return nil
}
for i := range shWithOpts {
shWithOpts[i] = strings.Replace(shWithOpts[i], "{0}", c.command, 1)
}
sh, err := safeexec.LookPath(shWithOpts[0])
if err != nil {
if c.shell != "" {
return err
}
// fallback to sh
fallback, errr := safeexec.LookPath("sh")
if errr != nil {
return err
}
sh = fallback
}
cmd := exec.CommandContext(ctx, sh, shWithOpts[1:]...)
if strings.Trim(c.stdin, " \n") != "" {
cmd.Stdin = strings.NewReader(c.stdin)
o.capturers.captureExecStdin(c.stdin)
}
if c.liveOutput {
cmd.Stdout = io.MultiWriter(stdout, o.maskRule.NewWriter(o.stdout))
cmd.Stderr = io.MultiWriter(stderr, o.maskRule.NewWriter(o.stderr))
} else {
cmd.Stdout = stdout
cmd.Stderr = stderr
}
if c.background {
// run in background
if err := cmd.Start(); err != nil {
o.capturers.captureExecStdout(stdout.String())
o.capturers.captureExecStderr(stderr.String())
o.record(s.idx, map[string]any{
string(execStoreStdoutKey): stdout.String(),
string(execStoreStderrKey): stderr.String(),
string(execStoreExitCodeKey): cmd.ProcessState.ExitCode(),
})
return nil
}
donegroup.Go(ctx, func() error {
_ = cmd.Wait() // WHY: Because it is only necessary to wait. For example, SIGNAL KILL is also normal.
return nil
})
o.record(s.idx, map[string]any{})
return nil
}
_ = cmd.Run()
o.capturers.captureExecStdout(stdout.String())
o.capturers.captureExecStderr(stderr.String())
o.record(s.idx, map[string]any{
string(execStoreStdoutKey): stdout.String(),
string(execStoreStderrKey): stderr.String(),
string(execStoreExitCodeKey): cmd.ProcessState.ExitCode(),
})
return nil
}