-
Notifications
You must be signed in to change notification settings - Fork 9
/
runner.go
166 lines (120 loc) · 2.69 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
package tools
import (
"context"
"errors"
"flag"
"fmt"
_ "log"
)
type ToolRunner struct {
Tool
tools []Tool
throttle chan bool
on_complete ToolRunnerOnCompleteFunc
}
type ToolRunnerOnCompleteFunc func(context.Context, string) error
type ToolRunnerOptions struct {
Tools []Tool
Throttle chan bool
OnCompleteFunc ToolRunnerOnCompleteFunc
}
func NewToolRunnerThrottle() (chan bool, error) {
throttle := make(chan bool)
return throttle, nil
}
func NewToolRunner(tools ...Tool) (Tool, error) {
opts := &ToolRunnerOptions{
Tools: tools,
}
return NewToolRunnerWithOptions(opts)
}
func NewSynchronousToolRunner(tools ...Tool) (Tool, error) {
throttle, err := NewToolRunnerThrottle()
if err != nil {
return nil, err
}
opts := &ToolRunnerOptions{
Tools: tools,
Throttle: throttle,
}
return NewToolRunnerWithOptions(opts)
}
func NewToolRunnerWithOptions(opts *ToolRunnerOptions) (Tool, error) {
if len(opts.Tools) == 0 {
return nil, errors.New("No tools to run!")
}
t := &ToolRunner{
tools: opts.Tools,
throttle: opts.Throttle,
on_complete: opts.OnCompleteFunc,
}
return t, nil
}
func (t *ToolRunner) Run(ctx context.Context) error {
fs := flag.NewFlagSet("combined", flag.ExitOnError)
return t.RunWithFlagSet(ctx, fs)
}
func (t *ToolRunner) RunWithFlagSet(ctx context.Context, fs *flag.FlagSet) error {
paths := fs.Args()
return t.RunWithFlagSetAndPaths(ctx, fs, paths...)
}
func (t *ToolRunner) RunWithFlagSetAndPaths(ctx context.Context, fs *flag.FlagSet, paths ...string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for _, path := range paths {
select {
case <-ctx.Done():
return nil
default:
// pass
}
done_ch := make(chan bool)
err_ch := make(chan error)
if t.throttle != nil {
go func() {
t.throttle <- true
}()
}
for _, tl := range t.tools {
go func(ctx context.Context, tl Tool, fs *flag.FlagSet) {
if t.throttle != nil {
<-t.throttle
defer func() {
t.throttle <- true
}()
}
defer func() {
done_ch <- true
}()
select {
case <-ctx.Done():
return
default:
// pass
}
err := tl.RunWithFlagSetAndPaths(ctx, fs, path)
if err != nil {
err_ch <- fmt.Errorf("Failed to run tool '%T': %w", tl, err)
}
}(ctx, tl, fs)
}
remaining := len(t.tools)
for remaining > 0 {
select {
case <-ctx.Done():
return nil
case <-done_ch:
remaining -= 1
case err := <-err_ch:
return err
}
}
if t.on_complete != nil {
err := t.on_complete(ctx, path)
if err != nil {
return fmt.Errorf("OnComplete function for '%s' failed, %w", path, err)
}
}
}
return nil
}