-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
123 lines (101 loc) · 2.44 KB
/
Copy pathexec.go
File metadata and controls
123 lines (101 loc) · 2.44 KB
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
package godexer
import (
"fmt"
"io"
"os/exec"
"strings"
"time"
"al.essio.dev/pkg/shellescape"
"github.com/go-extras/errors"
)
var ExecCommandFn = exec.Command
//nolint:gochecknoinits // init is used for automatic command registration
func init() {
RegisterCommand("", NewExecCommand) // default command
RegisterCommand("exec", NewExecCommand)
}
func escapeArgs(args []string) (result string) {
if len(args) == 0 {
return result
}
var escaped []string
for _, s := range args {
escaped = append(escaped, shellescape.Quote(s))
}
return strings.Join(escaped, " ")
}
type ExecCommand struct {
BaseCommand
Cmd []string
Variable string
AllowFail bool
Env []string
// the following parameters will allow retrying the command
Attempts int // if 0 or 1, no retry
Delay int // seconds
}
func NewExecCommand(ectx *ExecutorContext) Command {
return &ExecCommand{
BaseCommand: BaseCommand{
Ectx: ectx,
},
}
}
func (r *ExecCommand) Execute(variables map[string]any) error {
if len(r.Cmd) == 0 {
return errors.Errorf("command %q is empty", r.StepName)
}
var cmds []string
for _, v := range r.Cmd {
cmds = append(cmds, MaybeEvalValue(v, variables).(string))
}
if len(cmds) == 0 {
// should never actually happen
return errors.Errorf("command %q is empty", r.StepName)
}
r.Ectx.Logger.Info(strings.TrimSpace(fmt.Sprintf("Executing: %s %s", cmds[0], escapeArgs(cmds[1:]))))
cmd := ExecCommandFn(cmds[0], cmds[1:]...)
var buf Buffer
if r.Variable == "" {
cmd.Stdout = r.Ectx.Stdout
cmd.Stderr = r.Ectx.Stderr
} else {
cmd.Stdout = NewCombinedWriter([]io.Writer{
r.Ectx.Stdout,
&buf,
})
cmd.Stderr = NewCombinedWriter([]io.Writer{
r.Ectx.Stderr,
&buf,
})
}
cmd.Env = append(cmd.Env, r.Env...)
err := cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if r.Variable != "" {
variables[r.Variable] = buf.String()
}
if r.AllowFail {
if exitError, ok := err.(*exec.ExitError); ok {
err = nil
variables[r.StepName+"_exit_status"] = exitError.ExitCode()
} else {
variables[r.StepName+"_exit_status"] = 0
}
}
if err != nil {
r.Ectx.Logger.Infof("Got an error and attempts = %d", r.Attempts)
}
if r.Attempts > 1 {
if _, ok := err.(*exec.ExitError); ok {
r.Attempts--
r.Ectx.Logger.Infof("Got execution failure, will retry (attempts left %d)", r.Attempts)
TimeSleep(time.Duration(r.Delay) * time.Second)
err = r.Execute(variables)
}
}
return err
}