-
Notifications
You must be signed in to change notification settings - Fork 190
/
runner.go
318 lines (263 loc) · 5.74 KB
/
runner.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package cmdr
import (
"fmt"
"strings"
"github.com/gookit/color"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/cliutil/cmdline"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/strutil/textutil"
)
// Task struct
type Task struct {
err error
index int
// ID for task
ID string
Cmd *Cmd
// BeforeRun hook
BeforeRun func(t *Task)
PrevCond func(prev *Task) bool
}
// NewTask instance
func NewTask(cmd *Cmd) *Task {
return &Task{
Cmd: cmd,
}
}
// get task id by cmd.Name
func (t *Task) ensureID(idx int) {
t.index = idx
if t.ID != "" {
return
}
id := t.Cmd.IDString()
if t.Cmd.Name == "" {
id += mathutil.String(idx)
}
t.ID = id
}
var rpl = textutil.NewVarReplacer("$").DisableFlatten()
// RunWith command
func (t *Task) RunWith(ctx maputil.Data) error {
cmdVars := ctx.StringMap("cmdVars")
if len(cmdVars) > 0 {
// rpl := strutil.NewReplacer(cmdVars)
for i, val := range t.Cmd.Args {
if strings.ContainsRune(val, '$') {
t.Cmd.Args[i] = rpl.RenderSimple(val, cmdVars)
}
}
}
return t.Run()
}
// Run command
func (t *Task) Run() error {
if t.BeforeRun != nil {
t.BeforeRun(t)
}
t.err = t.Cmd.Run()
return t.err
}
// Err get
func (t *Task) Err() error {
return t.err
}
// Index get
func (t *Task) Index() int {
return t.index
}
// Cmdline get
func (t *Task) Cmdline() string {
return t.Cmd.Cmdline()
}
// IsSuccess of task
func (t *Task) IsSuccess() bool {
return t.err == nil
}
// RunnerHookFn func
type RunnerHookFn func(r *Runner, t *Task) bool
// Runner use for batch run multi task commands
type Runner struct {
prev *Task
// task name to index
idMap map[string]int
tasks []*Task
// Errs on run tasks, key is Task.ID
Errs errorx.ErrMap
// TODO Concurrent run
// Workdir common workdir
Workdir string
// EnvMap will append to task.Cmd on run
EnvMap map[string]string
// Params for add custom params
Params maputil.Map
// DryRun dry run all commands
DryRun bool
// OutToStd stdout and stderr
OutToStd bool
// IgnoreErr continue on error
IgnoreErr bool
// BeforeRun hooks on each task. return false to skip current task.
BeforeRun func(r *Runner, t *Task) bool
// AfterRun hook on each task. return false to stop running.
AfterRun func(r *Runner, t *Task) bool
}
// NewRunner instance with config func
func NewRunner(fns ...func(rr *Runner)) *Runner {
rr := &Runner{
idMap: make(map[string]int, 0),
tasks: make([]*Task, 0),
Errs: make(errorx.ErrMap),
Params: make(maputil.Map),
}
rr.OutToStd = true
for _, fn := range fns {
fn(rr)
}
return rr
}
// WithOutToStd set
func (r *Runner) WithOutToStd() *Runner {
r.OutToStd = true
return r
}
// Add multitask at once
func (r *Runner) Add(tasks ...*Task) *Runner {
for _, task := range tasks {
r.AddTask(task)
}
return r
}
// AddTask add one task
func (r *Runner) AddTask(task *Task) *Runner {
if task.Cmd == nil {
panic("task command cannot be empty")
}
idx := len(r.tasks)
task.ensureID(idx)
// TODO check id repeat
r.idMap[task.ID] = idx
r.tasks = append(r.tasks, task)
return r
}
// AddCmd commands
func (r *Runner) AddCmd(cmds ...*Cmd) *Runner {
for _, cmd := range cmds {
r.AddTask(&Task{Cmd: cmd})
}
return r
}
// GitCmd quick a git command task
func (r *Runner) GitCmd(subCmd string, args ...string) *Runner {
return r.AddTask(&Task{
Cmd: NewGitCmd(subCmd, args...),
})
}
// CmdWithArgs a command task
func (r *Runner) CmdWithArgs(cmdName string, args ...string) *Runner {
return r.AddTask(&Task{
Cmd: NewCmd(cmdName, args...),
})
}
// CmdWithAnys a command task
func (r *Runner) CmdWithAnys(cmdName string, args ...any) *Runner {
return r.AddTask(&Task{
Cmd: NewCmd(cmdName, arrutil.SliceToStrings(args)...),
})
}
// AddCmdline as a command task
func (r *Runner) AddCmdline(line string) *Runner {
bin, args := cmdline.NewParser(line).BinAndArgs()
return r.AddTask(&Task{
Cmd: NewCmd(bin, args...),
})
}
// Run all tasks
func (r *Runner) Run() error {
// do run tasks
for i, task := range r.tasks {
if r.BeforeRun != nil && !r.BeforeRun(r, task) {
continue
}
if r.prev != nil && task.PrevCond != nil && !task.PrevCond(r.prev) {
continue
}
if r.DryRun {
color.Infof("DRY-RUN: task#%d execute completed\n\n", i+1)
continue
}
if !r.RunTask(task) {
break
}
fmt.Println() // with newline.
}
if len(r.Errs) == 0 {
return nil
}
return r.Errs
}
// StepRun one command
func (r *Runner) StepRun() error {
return nil // TODO
}
// RunTask command
func (r *Runner) RunTask(task *Task) (goon bool) {
if len(r.EnvMap) > 0 {
task.Cmd.AppendEnv(r.EnvMap)
}
if r.OutToStd && !task.Cmd.HasStdout() {
task.Cmd.ToOSStdoutStderr()
}
// common workdir
if r.Workdir != "" && task.Cmd.Dir == "" {
task.Cmd.WithWorkDir(r.Workdir)
}
// do running
if err := task.RunWith(r.Params); err != nil {
r.Errs[task.ID] = err
color.Errorf("Task#%d run error: %s\n", task.Index()+1, err)
// not ignore error, stop.
if !r.IgnoreErr {
return false
}
}
if r.AfterRun != nil && !r.AfterRun(r, task) {
return false
}
// store prev
r.prev = task
return true
}
// Len of tasks
func (r *Runner) Len() int {
return len(r.tasks)
}
// Reset instance
func (r *Runner) Reset() *Runner {
r.prev = nil
r.tasks = make([]*Task, 0)
r.idMap = make(map[string]int, 0)
return r
}
// TaskIDs get
func (r *Runner) TaskIDs() []string {
ss := make([]string, 0, len(r.idMap))
for id := range r.idMap {
ss = append(ss, id)
}
return ss
}
// Prev task instance after running
func (r *Runner) Prev() *Task {
return r.prev
}
// Task get by id name
func (r *Runner) Task(id string) (*Task, error) {
if idx, ok := r.idMap[id]; ok {
return r.tasks[idx], nil
}
return nil, fmt.Errorf("task %q is not exists", id)
}