generated from golang-templates/seed
-
Notifications
You must be signed in to change notification settings - Fork 29
/
flow.go
495 lines (433 loc) · 12.8 KB
/
flow.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package goyek
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"sort"
"strings"
"text/tabwriter"
"time"
)
// Flow is the root type of the package.
// Use Register methods to register all tasks
// and Run or Main method to execute provided tasks.
type Flow struct {
output io.Writer
usage func()
logger Logger // TODO: If Helper() is implemented then it is called when A.Helper() is called.
tasks map[string]*taskSnapshot // snapshot of defined tasks
defaultTask *taskSnapshot // task to run when none is explicitly provided
middlewares []Middleware
}
// DefaultFlow is the default flow.
// The top-level functions such as Define, Main, and so on are wrappers for the methods of Flow.
var DefaultFlow = &Flow{}
// Middleware represents a task runner interceptor.
type Middleware func(Runner) Runner
// taskSnapshot is a copy of the task to make the flow usage safer.
type taskSnapshot struct {
name string
usage string
deps []*taskSnapshot
action func(a *A)
parallel bool
}
// Tasks returns all tasks sorted in lexicographical order.
func Tasks() []*DefinedTask {
return DefaultFlow.Tasks()
}
// Tasks returns all tasks sorted in lexicographical order.
func (f *Flow) Tasks() []*DefinedTask {
var tasks []*DefinedTask
for _, task := range f.tasks {
tasks = append(tasks, &DefinedTask{task, f})
}
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Name() < tasks[j].Name() })
return tasks
}
// Define registers the task. It panics in case of any error.
func Define(task Task) *DefinedTask {
return DefaultFlow.Define(task)
}
// Define registers the task. It panics in case of any error.
func (f *Flow) Define(task Task) *DefinedTask {
// validate
if task.Name == "" {
panic("task name cannot be empty")
}
if f.isDefined(task.Name, f) {
panic("task with the same name is already defined")
}
for _, dep := range task.Deps {
if !f.isDefined(dep.Name(), dep.flow) {
panic("dependency was not defined: " + dep.Name())
}
}
var deps []*taskSnapshot
for _, dep := range task.Deps {
deps = append(deps, dep.taskSnapshot)
}
taskCopy := &taskSnapshot{
name: task.Name,
usage: task.Usage,
deps: deps,
action: task.Action,
parallel: task.Parallel,
}
f.tasks[task.Name] = taskCopy
return &DefinedTask{taskCopy, f}
}
// Undefine unregisters the task. It panics in case of any error.
func Undefine(task *DefinedTask) {
DefaultFlow.Undefine(task)
}
// Undefine unregisters the task. It panics in case of any error.
func (f *Flow) Undefine(task *DefinedTask) {
snapshot := task.taskSnapshot
if !f.isDefined(snapshot.name, task.flow) {
panic("task was not defined: " + snapshot.name)
}
delete(f.tasks, snapshot.name)
for _, task := range f.tasks {
if len(task.deps) == 0 {
continue
}
var cleanDep []*taskSnapshot
for _, dep := range task.deps {
if dep == snapshot {
continue
}
cleanDep = append(cleanDep, dep)
}
task.deps = cleanDep
}
if f.defaultTask == snapshot {
f.defaultTask = nil
}
}
func (f *Flow) isDefined(name string, flow *Flow) bool {
if f.tasks == nil {
f.tasks = map[string]*taskSnapshot{}
}
if f != flow {
return false // defined in other flow
}
_, ok := f.tasks[name]
return ok
}
// Output returns the destination used for printing messages.
// [os.Stdout] is returned if output was not set or was set to nil.
func Output() io.Writer {
return DefaultFlow.Output()
}
// Output returns the destination used for printing messages.
// [os.Stdout] is returned if output was not set or was set to nil.
func (f *Flow) Output() io.Writer {
if f.output == nil {
return os.Stdout
}
return f.output
}
// SetOutput sets the output destination.
func SetOutput(out io.Writer) {
DefaultFlow.SetOutput(out)
}
// SetOutput sets the output destination.
func (f *Flow) SetOutput(out io.Writer) {
f.output = out
}
// GetLogger returns the logger used by A's logging functions
// [CodeLineLogger] is returned if logger was not set or was set to nil.
func GetLogger() Logger {
return DefaultFlow.Logger()
}
// Logger returns the logger used by A's logging functions
// [CodeLineLogger] is returned if logger was not set or was set to nil.
func (f *Flow) Logger() Logger {
if f.logger == nil {
return &CodeLineLogger{}
}
return f.logger
}
// SetLogger sets the logger used by A's logging functions.
//
// [A] uses following methods if implemented:
//
// Error(w io.Writer, args ...interface{})
// Errorf(w io.Writer, format string, args ...interface{})
// Fatal(w io.Writer, args ...interface{})
// Fatalf(w io.Writer, format string, args ...interface{})
// Skip(w io.Writer, args ...interface{})
// Skipf(w io.Writer, format string, args ...interface{})
// Helper()
func SetLogger(logger Logger) {
DefaultFlow.SetLogger(logger)
}
// SetLogger sets the logger used by A's logging functions.
//
// [A] uses following methods if implemented:
//
// Error(w io.Writer, args ...interface{})
// Errorf(w io.Writer, format string, args ...interface{})
// Fatal(w io.Writer, args ...interface{})
// Fatalf(w io.Writer, format string, args ...interface{})
// Skip(w io.Writer, args ...interface{})
// Skipf(w io.Writer, format string, args ...interface{})
// Helper()
func (f *Flow) SetLogger(logger Logger) {
f.logger = logger
}
// Usage returns a function that prints a usage message documenting the flow.
// It is called when an error occurs while parsing the flow.
// [Print] is returned if a function was not set or was set to nil.
func Usage() func() {
return DefaultFlow.Usage()
}
// Usage returns a function that prints a usage message documenting the flow.
// It is called when an error occurs while parsing the flow.
// [Flow.Print] is returned if a function was not set or was set to nil.
func (f *Flow) Usage() func() {
if f.usage == nil {
return f.Print
}
return f.usage
}
// SetUsage sets the function called when an error occurs while parsing tasks.
func SetUsage(fn func()) {
DefaultFlow.SetUsage(fn)
}
// SetUsage sets the function called when an error occurs while parsing tasks.
func (f *Flow) SetUsage(fn func()) {
f.usage = fn
}
// Default returns the default task.
// nil is returned if default was not set.
func Default() *DefinedTask {
return DefaultFlow.Default()
}
// Default returns the default task.
// nil is returned if default was not set.
func (f *Flow) Default() *DefinedTask {
if f.defaultTask == nil {
return nil
}
return &DefinedTask{f.defaultTask, f}
}
// SetDefault sets a task to run when none is explicitly provided.
// It panics in case of any error.
func SetDefault(task *DefinedTask) {
DefaultFlow.SetDefault(task)
}
// SetDefault sets a task to run when none is explicitly provided.
// Passing nil clears the default task.
// It panics in case of any error.
func (f *Flow) SetDefault(task *DefinedTask) {
if task == nil {
f.defaultTask = nil
return
}
if !f.isDefined(task.Name(), task.flow) {
panic("task was not defined: " + task.Name())
}
f.defaultTask = task.taskSnapshot
}
// Use adds task runner middlewares (interceptors).
func Use(middlewares ...Middleware) {
DefaultFlow.Use(middlewares...)
}
// Use adds task runner middlewares (interceptors).
func (f *Flow) Use(middlewares ...Middleware) {
for _, m := range middlewares {
if m == nil {
panic("middleware cannot be nil")
}
f.middlewares = append(f.middlewares, m)
}
}
// Option configures the flow execution.
type Option interface {
apply(*config)
}
type optionFunc func(*config)
func (fn optionFunc) apply(cfg *config) {
fn(cfg)
}
type config struct {
noDeps bool
skipTasks []string
}
// NoDeps is an option to skip processing of all dependencies.
func NoDeps() Option {
return optionFunc(func(c *config) {
c.noDeps = true
})
}
// Skip is an option to skip processing of given tasks.
func Skip(tasks ...string) Option {
return optionFunc(func(c *config) {
c.skipTasks = append(c.skipTasks, tasks...)
})
}
// FailError pointer is returned by [Flow.Execute] when a task failed.
type FailError struct {
Task string
}
func (err *FailError) Error() string {
return "task failed: " + err.Task
}
// Execute runs provided tasks and all their dependencies.
// Each task is executed at most once.
// Returns nil if no task has failed,
// [FailError] if a task failed,
// other errors in case of invalid input or context error.
func Execute(ctx context.Context, tasks []string, opts ...Option) error {
return DefaultFlow.Execute(ctx, tasks, opts...)
}
// Execute runs provided tasks and all their dependencies.
// Each task is executed at most once.
// Returns nil if no task has failed,
// [FailError] if a task failed,
// other errors in case of invalid input or context error.
func (f *Flow) Execute(ctx context.Context, tasks []string, opts ...Option) error {
for _, task := range tasks {
if task == "" {
return errors.New("task name cannot be empty")
}
if _, ok := f.tasks[task]; !ok {
return errors.New("task provided but not defined: " + task)
}
}
if len(tasks) == 0 && f.defaultTask != nil {
tasks = append(tasks, f.defaultTask.name)
}
if len(tasks) == 0 {
return errors.New("no task provided")
}
var middlewares []Middleware
middlewares = append(middlewares, f.middlewares...)
cfg := &config{}
for _, opt := range opts {
opt.apply(cfg)
}
for _, skippedTask := range cfg.skipTasks {
if skippedTask == "" {
return errors.New("skipped task name cannot be empty")
}
if _, ok := f.tasks[skippedTask]; !ok {
return errors.New("skipped task provided but not defined: " + skippedTask)
}
}
r := &executor{
output: &syncWriter{Writer: f.Output()},
defined: f.tasks,
logger: f.Logger(),
middlewares: middlewares,
noDeps: cfg.noDeps,
}
if ctx == nil {
ctx = context.Background()
}
return r.Execute(ctx, tasks, cfg.skipTasks)
}
const (
exitCodePass = 0
exitCodeFail = 1
exitCodeInvalid = 2
)
// Main runs provided tasks and all their dependencies.
// Each task is executed at most once.
// It exits the current program when after the run is finished
// or SIGINT interrupted the execution.
// - 0 exit code means that non of the tasks failed.
// - 1 exit code means that a task has failed or the execution was interrupted.
// - 2 exit code means that the input was invalid.
//
// Calls [Usage] when invalid args are provided.
func Main(args []string, opts ...Option) {
DefaultFlow.Main(args, opts...)
}
// Main runs provided tasks and all their dependencies.
// Each task is executed at most once.
// It exits the current program when after the run is finished
// or SIGINT interrupted the execution.
// - 0 exit code means that non of the tasks failed.
// - 1 exit code means that a task has failed or the execution was interrupted.
// - 2 exit code means that the input was invalid.
//
// Calls [Usage] when invalid args are provided.
func (f *Flow) Main(args []string, opts ...Option) {
out := f.Output()
// trap Ctrl+C and call cancel on the context
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c // first signal, cancel context
fmt.Fprintln(out, "first interrupt, graceful stop")
cancel()
<-c // second signal, hard exit
fmt.Fprintln(out, "second interrupt, exit")
os.Exit(exitCodeFail)
}()
exitCode := f.main(ctx, args, opts...)
os.Exit(exitCode)
}
func (f *Flow) main(ctx context.Context, args []string, opts ...Option) int {
out := f.Output()
from := time.Now()
err := f.Execute(ctx, args, opts...)
if _, ok := err.(*FailError); ok {
fmt.Fprintf(out, "%v\t%.3fs\n", err, time.Since(from).Seconds())
return exitCodeFail
}
if err == context.Canceled || err == context.DeadlineExceeded {
fmt.Fprintf(out, "%v\t%.3fs\n", err, time.Since(from).Seconds())
return exitCodeFail
}
if err != nil {
fmt.Fprintln(out, err.Error())
f.Usage()()
return exitCodeInvalid
}
fmt.Fprintf(out, "ok\t%.3fs\n", time.Since(from).Seconds())
return exitCodePass
}
// Print prints the information about the registered tasks.
// Tasks with empty [Task.Usage] are not printed.
func Print() {
DefaultFlow.Print()
}
// Print prints the information about the registered tasks.
// Tasks with empty [Task.Usage] are not printed.
func (f *Flow) Print() {
out := f.Output()
if f.defaultTask != nil {
fmt.Fprintf(out, "Default task: %s\n", f.defaultTask.name)
}
fmt.Fprintln(out, "Tasks:")
var (
minwidth = 5
tabwidth = 0
padding = 2
padchar byte = ' '
)
w := tabwriter.NewWriter(out, minwidth, tabwidth, padding, padchar, 0)
for _, task := range f.Tasks() {
if task.Usage() == "" {
continue
}
deps := ""
if len(task.Deps()) > 0 {
depNames := make([]string, 0, len(task.Deps()))
for _, dep := range task.Deps() {
depNames = append(depNames, dep.Name())
}
deps = " (depends on: " + strings.Join(depNames, ", ") + ")"
}
fmt.Fprintf(w, " %s\t%s\n", task.Name(), task.Usage()+deps)
}
w.Flush() //nolint:errcheck // not checking errors when writing to output
}