-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow.go
More file actions
308 lines (250 loc) · 6.92 KB
/
workflow.go
File metadata and controls
308 lines (250 loc) · 6.92 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
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
package storm
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/samber/lo"
"gopkg.in/yaml.v3"
)
type Workflow struct{}
func (w *Workflow) Load(file string) (*WorkflowConfig, error) {
workflow := WorkflowConfig{}
fileContent, _ := os.ReadFile(file)
yaml.Unmarshal([]byte(fileContent), &workflow)
return &workflow, nil
}
func (w *Workflow) Dump(content WorkflowConfig) (*string, error) {
out, err := yaml.Marshal(&content)
outStr := string(out)
return &outStr, err
}
type State struct {
IsSuccessful bool
IsCompleted bool
Error error
}
type JobState map[string]State
const (
StepOutputTypePlain = iota + 1
StepOutputTypeStruct
StepOutputTypeJson
)
type StepOutputPlain string
type WorkflowStepOutputStruct struct {
// workflow step path
// example; `build.installing curl`, means 👇
// - name: build
// steps:
// - name: installing curl
// run: sudo apt install -y curl
Path string
Command string
Message string
}
type WorkflowRunArgs struct {
File *string
Config *WorkflowConfig
Contexts map[string]map[string]any
Directory string
Callback func(any)
StepOutputType int
}
type WorkflowRunOptions func(*WorkflowRunArgs)
func (w *Workflow) WorkflowWithFile(file string) WorkflowRunOptions {
return func(wra *WorkflowRunArgs) {
wra.File = &file
}
}
func (w *Workflow) WorkflowWithConfig(config WorkflowConfig) WorkflowRunOptions {
return func(wra *WorkflowRunArgs) {
wra.Config = &config
}
}
func (w *Workflow) WorkflowWithContexts(contexts map[string]map[string]any) WorkflowRunOptions {
return func(wra *WorkflowRunArgs) {
wra.Contexts = contexts
}
}
func (w *Workflow) WorkflowWithDirectory(directory string) WorkflowRunOptions {
return func(wra *WorkflowRunArgs) {
wra.Directory = directory
}
}
func (w *Workflow) WorkflowWithCallback(callback func(any), sot int) WorkflowRunOptions {
return func(wra *WorkflowRunArgs) {
wra.Callback = callback
wra.StepOutputType = sot
}
}
func (w *Workflow) Run(opts ...WorkflowRunOptions) error {
args := WorkflowRunArgs{
StepOutputType: StepOutputTypePlain,
Callback: func(sos any) {},
}
for _, opt := range opts {
opt(&args)
}
if args.File == nil && args.Config == nil {
return errors.New("either file or config must be specified to run a workflow")
}
if args.File != nil && args.Config == nil {
raw, err := os.ReadFile(*args.File)
if err != nil {
return fmt.Errorf("cannot read workflow file: %w", err)
}
content := string(raw)
if len(args.Contexts) > 0 {
content, err = RenderTemplate(content, args.Contexts)
if err != nil {
return fmt.Errorf("template render error: %w", err)
}
}
var config WorkflowConfig
if err := yaml.Unmarshal([]byte(content), &config); err != nil {
return fmt.Errorf("cannot parse workflow: %w", err)
}
args.Config = &config
}
if args.Directory != "" && args.Config.Directory == "" {
args.Config.Directory = args.Directory
}
jobState := make(JobState, 0)
for _, job := range args.Config.Jobs {
jobState[job.Name] = State{IsSuccessful: true, IsCompleted: true, Error: nil}
// TODO: handle error for when `job.Needs` is not found in `jobState`; aka, don't exist
if job.Needs != "" && (!jobState[job.Needs].IsCompleted || !jobState[job.Needs].IsSuccessful) {
err := fmt.Errorf("> dependencies error, %s job failed", job.Needs)
fmt.Println(err)
return jobState[job.Name].Error
}
start := time.Now()
if args.StepOutputType == StepOutputTypePlain {
fmt.Printf("[%s]\n", job.Name)
}
err := func() error {
for _, step := range job.Steps {
if args.StepOutputType == StepOutputTypePlain {
fmt.Printf("-> %s\n", step.Name)
fmt.Printf("$ %s \n", step.Run)
}
callback := func(s string) {
switch args.StepOutputType {
case StepOutputTypePlain:
fmt.Println("> ", s)
case StepOutputTypeStruct:
args.Callback(WorkflowStepOutputStruct{
Path: fmt.Sprintf("%s.%s", job.Name, step.Name),
Command: step.Run,
Message: s,
})
case StepOutputTypeJson:
payload := WorkflowStepOutputStruct{
Path: fmt.Sprintf("%s.%s", job.Name, step.Name),
Command: step.Run,
Message: s,
}
payloadString, err := json.Marshal(&payload)
if err != nil {
fmt.Println("could not marshel workflow payload to json. reason: ", err)
break
}
args.Callback(string(payloadString))
}
}
err := w.Execute(ExecuteArgs{
Directory: lo.Ternary(step.Directory != "", step.Directory, args.Config.Directory),
Shell: lo.Ternary(step.Shell != "", step.Shell, os.Getenv("SHELL")),
Command: step.Run,
OutputCallback: callback,
ErrorCallback: callback,
})
if err != nil {
return err
}
}
return nil
}()
end := time.Now()
duration := end.Sub(start)
timeToRun := duration.Seconds()
switch args.StepOutputType {
case StepOutputTypePlain:
fmt.Printf("Took %.2fs to run.\n\n", timeToRun)
case StepOutputTypeStruct:
args.Callback(WorkflowStepOutputStruct{
Path: "__builtin__.TimeTaken",
Command: "TimeTaken",
Message: fmt.Sprintf("%.2fs", timeToRun),
})
}
if err != nil {
state := jobState[job.Name]
state.IsSuccessful = false
state.IsCompleted = false
state.Error = err
jobState[job.Name] = state
}
}
return nil
}
type ExecuteArgs struct {
Directory string
Shell string
Command string
OutputCallback func(string)
ErrorCallback func(string)
}
func (w *Workflow) Execute(args ExecuteArgs) error {
// Trim any leading/trailing whitespace
command := strings.TrimSpace(args.Command)
currentDirectory, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot get current directory %w", err)
}
err = ChdirOrCreate(args.Directory)
if err != nil {
return fmt.Errorf("cannot change directory %w", err)
}
defer os.Chdir(currentDirectory)
currentCmd := exec.Command(args.Shell, "-lc", "-o", "pipefail", command)
currentCmd.Env = os.Environ()
stdoutPipe, err := currentCmd.StdoutPipe()
if err != nil {
return fmt.Errorf("error creating stdout pipe: %w", err)
}
stderrPipe, err := currentCmd.StderrPipe()
if err != nil {
return fmt.Errorf("error creating stderr pipe: %w", err)
}
// Start the command
if err := currentCmd.Start(); err != nil {
return fmt.Errorf("error starting command: %w", err)
}
// Stream stdout to the output callback
go func() {
scanner := bufio.NewScanner(stdoutPipe)
for scanner.Scan() {
args.OutputCallback(scanner.Text())
}
}()
// Stream stderr to the error callback
go func() {
scanner := bufio.NewScanner(stderrPipe)
for scanner.Scan() {
args.ErrorCallback(scanner.Text())
}
}()
// Wait for the command to finish
if err := currentCmd.Wait(); err != nil {
return fmt.Errorf("error waiting for command: %w", err)
}
return nil
}
func NewWorkflow() *Workflow {
return &Workflow{}
}