-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.go
356 lines (309 loc) · 7.26 KB
/
workflow.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package probe
import (
"fmt"
"sync"
"time"
"github.com/fatih/color"
)
type Workflow struct {
Name string `yaml:"name",validate:"required"`
Jobs []Job `yaml:"jobs",validate:"required"`
Vars map[string]any `yaml:"vars"`
exitStatus int
env map[string]string
}
func (w *Workflow) SetExitStatus(isErr bool) {
if isErr {
w.exitStatus = 1
}
}
func (w *Workflow) Start(c Config) error {
vars, err := w.evalVars()
if err != nil {
return err
}
ctx := w.newJobContext(c, vars)
var wg sync.WaitGroup
for _, job := range w.Jobs {
// No repeat
if job.Repeat == nil {
wg.Add(1)
go func() {
defer wg.Done()
w.SetExitStatus(job.Start(ctx))
}()
continue
}
// Repeat
for i := 0; i < job.Repeat.Count; i++ {
wg.Add(1)
go func() {
defer wg.Done()
w.SetExitStatus(job.Start(ctx))
}()
time.Sleep(time.Duration(job.Repeat.Interval) * time.Second)
}
}
wg.Wait()
return nil
}
func (w *Workflow) Env() map[string]string {
if len(w.env) == 0 {
w.env = EnvMap()
}
return w.env
}
func (w *Workflow) evalVars() (map[string]any, error) {
env := StrmapToAnymap(w.Env())
vars := make(map[string]any)
expr := &Expr{}
for k, v := range w.Vars {
if mapV, ok := v.(map[string]any); ok {
vars[k] = expr.EvalTemplateMap(mapV, env)
} else if strV, ok2 := v.(string); ok2 {
output, err := expr.EvalTemplate(strV, env)
if err != nil {
return vars, err
}
vars[k] = output
}
}
return vars, nil
}
func (w *Workflow) newJobContext(c Config, vars map[string]any) JobContext {
return JobContext{
Vars: vars,
Logs: []map[string]any{},
Config: c,
}
}
type JobContext struct {
Vars map[string]any `expr:"vars"`
Logs []map[string]any `expr:"steps"`
Config
Failed bool
}
func (j *JobContext) SetFailed() {
j.Failed = true
}
type StepContext struct {
Vars map[string]any `expr:"vars"`
Logs []map[string]any `expr:"steps"`
Res map[string]any `expr:"res"`
Req map[string]any `expr:"req"`
}
type Repeat struct {
Count int `yaml:"count",validate:"required,gte=0,lt=100"`
Interval int `yaml:"interval,validate:"gte=0,lt=600"`
}
type Step struct {
Name string `yaml:"name"`
Uses string `yaml:"uses" validate:"required"`
With map[string]any `yaml:"with"`
Test string `yaml:"test"`
Echo string `yaml:"echo"`
Vars map[string]any `yaml:"vars"`
Iter []map[string]any `yaml:"iter"`
err error
ctx StepContext
idx int
expr *Expr
}
type Job struct {
Name string `yaml:"name",validate:"required"`
Steps []*Step `yaml:"steps",validate:"required"`
Repeat *Repeat `yaml:"repeat"`
Defaults any `yaml:"defaults"`
ctx *JobContext
}
func (j *Job) Start(ctx JobContext) bool {
j.ctx = &ctx
expr := &Expr{}
if j.Name == "" {
j.Name = "Unknown Job"
}
name, err := expr.EvalTemplate(j.Name, ctx)
if err != nil {
fmt.Printf("Expr error(job name): %#v\n", err)
} else {
fmt.Printf("%s\n", name)
}
var idx = 0
for _, st := range j.Steps {
st.expr = expr
if len(st.Iter) == 0 {
st.idx = idx
idx += 1
st.SetCtx(ctx, nil)
st.Do(&ctx)
continue
}
// NOTE: Split JobContext to ExprEnv
for _, vars := range st.Iter {
st.idx = idx
idx += 1
st.SetCtx(ctx, vars)
st.Do(&ctx)
}
}
return j.ctx.Failed
}
func (st *Step) Do(jCtx *JobContext) {
if st.Name == "" {
st.Name = "Unknown Step"
}
name, err := st.expr.EvalTemplate(st.Name, st.ctx)
if err != nil {
fmt.Printf("Expr error(step name): %#v\n", err)
}
expW := st.expr.EvalTemplateMap(st.With, st.ctx)
ret, err := RunActions(st.Uses, []string{}, expW, jCtx.Config.Verbose)
if err != nil {
st.err = err
jCtx.SetFailed()
return
}
// parse json and sets
req, okreq := ret["req"].(map[string]any)
res, okres := ret["res"].(map[string]any)
if okres {
body, okbody := res["body"].(string)
if okbody && isJSON(body) {
res["rawbody"] = body
res["body"] = mustMarshalJSON(body)
}
}
// set log and logs
jCtx.Logs = append(jCtx.Logs, ret)
st.updateCtx(jCtx.Logs, req, res)
if jCtx.Config.Verbose {
if !okreq || !okres {
fmt.Print("sorry, request or response is nil")
jCtx.SetFailed()
return
}
st.ShowRequestResponse(name)
if st.Test != "" {
if ok := st.DoTestWithSequentialPrint(); !ok {
jCtx.SetFailed()
}
}
if st.Echo != "" {
st.DoEchoWithSequentialPrint()
}
fmt.Println("- - -")
return
}
// Output format here:
// 1. ✔︎ Step name
num := color.HiBlackString(fmt.Sprintf("%2d.", st.idx))
output := fmt.Sprintf("%s %%s %s", num, name)
if st.Test != "" {
str, ok := st.DoTest()
if ok {
output = fmt.Sprintf(output+"\n", color.GreenString("✔︎ "))
} else {
output = fmt.Sprintf(output+"\n"+str+"\n", color.RedString("✘ "))
jCtx.SetFailed()
}
} else {
output = fmt.Sprintf(output+"\n", color.BlueString("▲ "))
}
fmt.Print(output)
if st.Echo != "" {
st.DoEcho()
}
}
func (st *Step) DoTestWithSequentialPrint() bool {
exprOut, err := st.expr.Eval(st.Test, st.ctx)
if err != nil {
fmt.Printf("%s: %s\nInput: %s\n", color.RedString("Test Error"), err, st.Test)
return false
}
boolOutput, boolOk := exprOut.(bool)
if !boolOk {
fmt.Printf("Test: `%s` = %s\n", st.Test, exprOut)
return false
}
boolResultStr := color.GreenString("Success")
if !boolOutput {
boolResultStr = color.RedString("Failure")
}
fmt.Printf("Test: %s (input: %s, env: %#v)\n", boolResultStr, st.Test, st.ctx)
return boolOk
}
func (st *Step) DoEchoWithSequentialPrint() {
exprOut, err := st.expr.Eval(st.Echo, st.ctx)
if err != nil {
fmt.Printf("%s: %#v (input: %s)\n", color.RedString("Echo Error"), err, st.Echo)
} else {
fmt.Printf("Echo: %s\n", exprOut)
}
}
func (st *Step) DoTest() (string, bool) {
exprOut, err := st.expr.Eval(st.Test, st.ctx)
if err != nil {
return fmt.Sprintf("Test\nerror: %#v\n", err), false
}
boolOutput, boolOk := exprOut.(bool)
if !boolOk {
return fmt.Sprintf("Test: `%s` = %s\n", st.Test, exprOut), false
}
if !boolOutput {
// 7 spaces
output := fmt.Sprintf(" request: %#v\n", st.ctx.Req)
output += fmt.Sprintf(" response: %#v\n", st.ctx.Res)
return output, false
}
return "", true
}
func (st *Step) DoEcho() {
exprOut, err := st.expr.Eval(st.Echo, st.ctx)
if err != nil {
fmt.Printf("Echo\nerror: %#v\n", err)
} else {
// 7 spaces
fmt.Printf(" %s\n", exprOut)
}
}
func (st *Step) SetCtx(j JobContext, override map[string]any) {
vers := MergeMaps(j.Vars, st.Vars)
if override != nil {
vers = MergeMaps(vers, override)
}
st.ctx = StepContext{
Vars: vers,
Logs: j.Logs,
}
}
func (st *Step) updateCtx(logs []map[string]any, req, res map[string]any) {
st.ctx.Logs = logs
st.ctx.Req = req
st.ctx.Res = res
}
func (st *Step) ShowRequestResponse(name string) {
fmt.Printf("--- Step %d: %s\nRequest:\n", st.idx, name)
for k, v := range st.ctx.Req {
nested, ok := v.(map[string]any)
if ok {
fmt.Printf(" %s:\n", k)
for kk, vv := range nested {
fmt.Printf(" %s: %#v\n", kk, vv)
}
} else {
fmt.Printf(" %s: %#v\n", k, v)
}
}
fmt.Printf("Response:\n")
for k, v := range st.ctx.Res {
nested, ok := v.(map[string]any)
if ok {
fmt.Printf(" %s:\n", k)
for kk, vv := range nested {
fmt.Printf(" %s: %#v\n", kk, vv)
}
} else {
fmt.Printf(" %s: %#v\n", k, v)
}
}
}