forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
154 lines (125 loc) · 3.43 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
package task
import (
"encoding/json"
"fmt"
"strings"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
type ReporterImpl struct {
ui boshui.UI
isForEvents bool
hasOutput bool
events []*Event
lastEvent *Event
outputRest string
}
func NewReporter(ui boshui.UI, isForEvents bool) *ReporterImpl {
return &ReporterImpl{ui: ui, isForEvents: isForEvents}
}
func (r ReporterImpl) TaskStarted(id int) {
r.ui.BeginLinef("Task %d", id)
}
func (r ReporterImpl) TaskFinished(id int, state string) {
if len(r.events) > 0 {
start := r.events[0].TimeAsStr()
end := r.lastEvent.TimeAsStr()
duration := r.events[0].DurationAsStr(*r.lastEvent)
r.ui.PrintLinef("\nStarted %s\nFinished %s\nDuration %s", start, end, duration)
}
if r.hasOutput {
r.ui.PrintLinef("Task %d %s", id, state)
} else {
r.ui.EndLinef(". %s", strings.Title(state))
}
}
func (r *ReporterImpl) TaskOutputChunk(id int, chunk []byte) {
if !r.hasOutput {
r.hasOutput = true
r.ui.BeginLinef("\n")
if !r.isForEvents {
r.ui.BeginLinef("\n")
}
}
if r.isForEvents {
r.outputRest += string(chunk)
for {
idx := strings.Index(r.outputRest, "\n")
if idx == -1 {
return
}
if len(r.outputRest[0:idx]) > 0 {
r.showEvent(r.outputRest[0:idx])
}
r.outputRest = r.outputRest[idx+1:]
}
} else {
r.showChunk(chunk)
}
}
func (r *ReporterImpl) showEvent(str string) {
var event Event
err := json.Unmarshal([]byte(str), &event)
if err != nil {
panic(fmt.Sprintf("unmarshal chunk '%s'", str))
}
for _, ev := range r.events {
if ev.IsSame(event) {
event.StartEvent = ev
break
}
}
if r.lastEvent != nil && r.lastEvent.IsSame(event) {
switch {
case event.State == EventStateStarted:
// does not make sense
case event.State == EventStateFinished:
r.ui.PrintBlock(fmt.Sprintf(" (%s)", event.DurationSinceStartAsStr()))
case event.State == EventStateFailed:
r.ui.PrintBlock(fmt.Sprintf(" (%s)", event.DurationSinceStartAsStr()))
r.ui.PrintErrorBlock(fmt.Sprintf(
"\n L Error: %s", event.Data.Error))
}
} else {
if r.lastEvent != nil && event.IsWorthKeeping() {
if event.Type == EventTypeDeprecation || event.Error != nil {
// Some spacing around deprecations and errors
r.ui.PrintBlock("\n")
}
}
prefix := fmt.Sprintf("\n%s | ", event.TimeAsHoursStr())
desc := event.Stage
if len(event.Tags) > 0 {
desc += " " + strings.Join(event.Tags, ", ")
}
switch {
case event.Type == EventTypeDeprecation:
r.ui.PrintBlock(prefix)
r.ui.PrintErrorBlock(fmt.Sprintf("Deprecation: %s", event.Message))
case event.State == EventStateStarted:
r.ui.PrintBlock(prefix)
r.ui.PrintBlock(fmt.Sprintf("%s: %s", desc, event.Task))
case event.State == EventStateFinished:
r.ui.PrintBlock(prefix)
r.ui.PrintBlock(fmt.Sprintf("%s: %s (%s)",
desc, event.Task, event.DurationSinceStartAsStr()))
case event.State == EventStateFailed:
r.ui.PrintBlock(prefix)
r.ui.PrintBlock(fmt.Sprintf("%s: %s (%s)",
desc, event.Task, event.DurationSinceStartAsStr()))
r.ui.PrintErrorBlock(fmt.Sprintf(
"\n L Error: %s", event.Data.Error))
case event.Error != nil:
r.ui.PrintBlock(prefix)
r.ui.PrintErrorBlock(fmt.Sprintf("Error: %s", event.Error.Message))
default:
// Skip event
}
}
if event.IsWorthKeeping() {
r.events = append(r.events, &event)
r.lastEvent = &event
}
}
func (r *ReporterImpl) showChunk(bytes []byte) {
r.ui.PrintBlock(string(bytes))
}