/
runner.go
221 lines (193 loc) · 4.63 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
package fab
import (
"bufio"
"context"
"fmt"
"io"
"os"
"strings"
"sync"
"github.com/bobg/errors"
)
type outcome struct {
g *gate
err error
}
func (con *Controller) incDepth() {
con.mu.Lock()
con.depth++
con.mu.Unlock()
}
func (con *Controller) decDepth() {
con.mu.Lock()
con.depth--
if con.depth < 0 {
con.depth = 0
}
con.mu.Unlock()
}
// Run runs the given targets, skipping any that have already run.
//
// A controller remembers which targets it has already run
// (whether in this call or any previous call to Run).
//
// The targets are executed concurrently.
// A separate goroutine is created for each one passed to Run.
// If the controller has never yet run the target,
// it does so, and caches the result (error or no error).
// If the target did already run, the cached error value is used.
// If another goroutine concurrently requests the same target,
// it blocks until the first one completes,
// then uses the first one's result.
//
// This function waits for all goroutines to complete.
// The return value may be an accumulation of multiple errors
// produced with [errors.Join].
func (con *Controller) Run(ctx context.Context, targets ...Target) error {
if len(targets) == 0 {
return nil
}
con.incDepth()
defer con.decDepth()
var (
verbose = GetVerbose(ctx)
errs = make([]error, len(targets))
wg sync.WaitGroup
)
for i, target := range targets {
addr, err := targetAddr(target)
if err != nil {
errs[i] = err
continue
}
i, target := i, target // Go loop-var pitfall
if d, ok := target.(*deferredResolutionTarget); ok {
// Short-circuit here to avoid some confusing extra output in verbose mode.
target, err = d.resolve(con)
if err != nil {
errs[i] = err
continue
}
}
wg.Add(1)
go func() {
defer wg.Done()
con.mu.Lock()
o, ok := con.ran[addr]
if !ok {
o = &outcome{g: newGate(false)}
con.ran[addr] = o
}
con.mu.Unlock()
if ok {
// This target was launched in a different goroutine.
// Wait for it to produce a result.
o.g.wait()
errs[i] = o.err
} else {
// This target was not previously launched,
// so run it and then open its "outcome gate."
if verbose {
con.Indentf("Running %s", con.Describe(target))
}
err := target.Run(ctx, con)
if err != nil {
err = errors.Wrapf(err, "running %s", con.Describe(target))
}
errs[i] = err
o.err = err
o.g.set(true)
}
}()
}
wg.Wait()
return errors.Join(errs...)
}
// Indentf formats and prints its arguments
// with leading indentation based on the nesting depth of the controller.
// The nesting depth increases with each call to [Controller.Run]
// and decreases at the end of the call.
//
// A newline is added to the end of the string if one is not already there.
func (con *Controller) Indentf(format string, args ...any) {
con.indentf(os.Stdout, format, args...)
}
func (con *Controller) indentf(w io.Writer, format string, args ...any) {
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
con.mu.Lock()
depth := con.depth
con.mu.Unlock()
if depth > 0 {
fmt.Fprint(w, strings.Repeat(" ", int(depth)))
}
fmt.Fprintf(w, format, args...)
}
// IndentingCopier creates an [io.Writer] that copies its data to an underlying writer,
// indenting each line according to the indentation depth of the controller.
// After indentation,
// each line additionally gets any prefix specified in `prefix`.
//
// The wrapper converts \r\n to \n, and bare \r to \n.
// A \r at the very end of the input is silently dropped.
func (con *Controller) IndentingCopier(w io.Writer, prefix string) io.Writer {
con.mu.Lock()
depth := con.depth
con.mu.Unlock()
return &indentingCopier{
w: bufio.NewWriter(w),
indent: strings.Repeat(" ", int(depth)) + prefix,
bol: true,
}
}
type indentingCopier struct {
w *bufio.Writer
indent string
bol, sawcr bool
}
func (c *indentingCopier) Write(buf []byte) (int, error) {
var n int
for _, b := range buf {
switch b {
case '\n':
if err := c.newline(); err != nil {
return n, err
}
case '\r':
if c.sawcr {
if err := c.newline(); err != nil {
return n, err
}
}
c.sawcr = true
default:
if c.sawcr {
if err := c.newline(); err != nil {
return n, err
}
}
if c.bol {
_, err := c.w.WriteString(c.indent)
if err != nil {
return n, err
}
}
c.bol = false
if err := c.w.WriteByte(b); err != nil {
return n, err
}
}
n++
}
err := c.w.Flush()
return n, err
}
func (c *indentingCopier) newline() error {
if err := c.w.WriteByte('\n'); err != nil {
return err
}
c.bol = true
c.sawcr = false
return nil
}