-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathreporter.go
327 lines (252 loc) · 6.73 KB
/
reporter.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
package tachyon
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"
)
type Reporter interface {
StartTasks(r *Runner)
FinishTasks(r *Runner)
StartHandlers(r *Runner)
FinishHandlers(r *Runner)
StartTask(task *Task, name, args string, vars Vars)
FinishTask(task *Task, res *Result)
FinishAsyncTask(act *AsyncAction)
Progress(str string)
JSONProgress(data []byte) error
}
type ProgressReporter interface {
Progress(string)
JSONProgress(data []byte) error
}
type CLIReporter struct {
out io.Writer
Start time.Time
}
var sCLIReporter *CLIReporter = &CLIReporter{out: os.Stdout}
func (c *CLIReporter) StartTasks(r *Runner) {
c.Start = time.Now()
fmt.Fprintf(c.out, "== tasks @ %v\n", r.Start)
}
func (c *CLIReporter) FinishTasks(r *Runner) {
dur := time.Since(c.Start)
fmt.Fprintf(c.out, "%7.3f ! Waiting on all tasks to finish...\n", dur.Seconds())
}
func (c *CLIReporter) StartHandlers(r *Runner) {
dur := time.Since(c.Start)
fmt.Fprintf(c.out, "%7.3f ! Running any handlers\n", dur.Seconds())
}
func (c *CLIReporter) FinishHandlers(r *Runner) {}
func (c *CLIReporter) StartTask(task *Task, name, args string, vars Vars) {
dur := time.Since(c.Start)
if task.Async() {
fmt.Fprintf(c.out, "%7.3f - %s &\n", dur.Seconds(), name)
} else {
fmt.Fprintf(c.out, "%7.3f - %s\n", dur.Seconds(), name)
}
fmt.Fprintf(c.out, "%7.3f %s: %s\n", dur.Seconds(), task.Command(), inlineVars(vars))
}
func (c *CLIReporter) Progress(str string) {
dur := time.Since(c.Start)
lines := strings.Split(str, "\n")
out := strings.Join(lines, fmt.Sprintf("\n%7.3f + ", dur.Seconds()))
fmt.Fprintf(c.out, "%7.3f + %s\n", dur.Seconds(), out)
}
func (c *CLIReporter) JSONProgress(data []byte) error {
cr := JsonChunkReconstitute{c}
return cr.Input(data)
}
func (c *CLIReporter) FinishTask(task *Task, res *Result) {
if res == nil {
return
}
dur := time.Since(c.Start)
indent := fmt.Sprintf("%7.3f ", dur.Seconds())
label := "result"
if res.Changed == false {
label = "check"
} else if res.Failed == true {
label = "failed"
}
if render, ok := res.Get("_result"); ok {
out, ok := render.Read().(string)
if ok {
out = strings.TrimSpace(out)
if out != "" {
lines := strings.Split(out, "\n")
indented := strings.Join(lines, indent+"\n")
fmt.Fprintf(c.out, "%7.3f * %s:\n", dur.Seconds(), label)
fmt.Fprintf(c.out, "%7.3f %s\n", dur.Seconds(), indented)
}
return
}
}
if len(res.Data) > 0 {
fmt.Fprintf(c.out, "%7.3f * %s:\n", dur.Seconds(), label)
fmt.Fprintf(c.out, "%s\n", indentedVars(Vars(res.Data), indent))
}
}
func (c *CLIReporter) FinishAsyncTask(act *AsyncAction) {
dur := time.Since(c.Start)
if act.Error == nil {
fmt.Fprintf(c.out, "%7.3f * %s (async success)\n", dur.Seconds(), act.Task.Name())
} else {
fmt.Fprintf(c.out, "%7.3f * %s (async error:%s)\n", dur.Seconds(), act.Task.Name(), act.Error)
}
}
type AdhocProgress struct {
out io.Writer
Start time.Time
}
func (a *AdhocProgress) Progress(str string) {
dur := time.Since(a.Start)
lines := strings.Split(str, "\n")
out := strings.Join(lines, fmt.Sprintf("\n%7.3f ", dur.Seconds()))
fmt.Fprintf(a.out, "%7.3f %s\n", dur.Seconds(), out)
}
func (a *AdhocProgress) JSONProgress(data []byte) error {
cr := JsonChunkReconstitute{a}
return cr.Input(data)
}
type JsonChunkReporter struct {
out io.Writer
Start time.Time
}
func (c *JsonChunkReporter) send(args ...interface{}) {
b := ijson(args...)
fmt.Fprintf(c.out, "%d\n%s\n", len(b), string(b))
}
var sJsonChunkReporter *JsonChunkReporter = &JsonChunkReporter{out: os.Stdout}
func (c *JsonChunkReporter) StartTasks(r *Runner) {
c.Start = r.Start
c.send("phase", "start", "time", r.Start.String())
}
func (c *JsonChunkReporter) FinishTasks(r *Runner) {
c.send("phase", "finish")
}
func (c *JsonChunkReporter) StartHandlers(r *Runner) {
c.send("phase", "start_handlers")
}
func (c *JsonChunkReporter) FinishHandlers(r *Runner) {
c.send("phase", "finish_handlers")
}
func (c *JsonChunkReporter) StartTask(task *Task, name, args string, vars Vars) {
dur := time.Since(c.Start).Seconds()
typ := "sync"
if task.Async() {
typ = "async"
}
c.send(
"phase", "start_task",
"type", typ,
"name", name,
"command", task.Command(),
"args", args,
"vars", vars,
"delta", dur)
}
func (c *JsonChunkReporter) Progress(str string) {
dur := time.Since(c.Start).Seconds()
c.send(
"phase", "progress",
"delta", dur,
"progress", str)
}
func (c *JsonChunkReporter) JSONProgress(data []byte) error {
dur := time.Since(c.Start).Seconds()
raw := json.RawMessage(data)
c.send(
"phase", "json_progress",
"delta", dur,
"progress", &raw)
return nil
}
func (c *JsonChunkReporter) FinishTask(task *Task, res *Result) {
if res == nil {
return
}
dur := time.Since(c.Start).Seconds()
c.send(
"phase", "finish_task",
"delta", dur,
"result", res)
}
func (c *JsonChunkReporter) FinishAsyncTask(act *AsyncAction) {
dur := time.Since(c.Start).Seconds()
if act.Error == nil {
c.send(
"phase", "finish_task",
"delta", dur,
"result", act.Result)
} else {
c.send(
"phase", "finish_task",
"delta", dur,
"error", act.Error)
}
}
type JsonChunkReconstitute struct {
report ProgressReporter
}
func (j *JsonChunkReconstitute) Input(data []byte) error {
m := make(map[string]interface{})
err := json.Unmarshal(data, &m)
if err != nil {
return err
}
return j.InputMap(m, 0)
}
func (j *JsonChunkReconstitute) InputMap(m map[string]interface{}, depth int) error {
phase, ok := m["phase"]
if !ok {
return fmt.Errorf("No phase specified")
}
var prefix string
if depth > 0 {
prefix = fmt.Sprintf("[%d] ", depth)
}
switch phase {
case "start":
time, ok := m["time"]
if !ok {
time = "(unknown)"
}
j.report.Progress(fmt.Sprintf("%sremote tasks @ %s", prefix, time))
case "start_task":
j.report.Progress(fmt.Sprintf("%s- %s", prefix, m["name"]))
mv := m["vars"].(map[string]interface{})
j.report.Progress(fmt.Sprintf("%s %s: %s", prefix, m["command"], inlineMap(mv)))
case "finish_task":
res := m["result"].(map[string]interface{})
data := res["data"].(map[string]interface{})
label := "result"
if res["changed"].(bool) == false {
label = "check"
} else if res["failed"].(bool) == true {
label = "failed"
}
reported := false
if v, ok := data["_result"]; ok {
if str, ok := v.(string); ok {
if str != "" {
j.report.Progress(fmt.Sprintf("%s* %s:", prefix, label))
j.report.Progress(prefix + " " + str)
}
reported = true
}
}
if !reported {
if len(data) > 0 {
j.report.Progress(fmt.Sprintf("%s* %s:", prefix, label))
j.report.Progress(indentedMap(data, prefix+" "))
}
}
case "json_progress":
ds := m["progress"].(map[string]interface{})
j.InputMap(ds, depth+1)
}
return nil
}