-
Notifications
You must be signed in to change notification settings - Fork 290
/
resourceview.go
473 lines (404 loc) · 11.4 KB
/
resourceview.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
package hud
import (
"fmt"
"strings"
"time"
"github.com/gdamore/tcell"
"github.com/windmilleng/tilt/internal/hud/view"
"github.com/windmilleng/tilt/internal/rty"
"github.com/windmilleng/tilt/pkg/model"
)
// These widths are determined experimentally, to see what shows up in a typical UX.
const DeployCellMinWidth = 8
const BuildDurCellMinWidth = 7
const BuildStatusCellMinWidth = 8
const MaxInlineErrHeight = 6
type ResourceView struct {
res view.Resource
rv view.ResourceViewState
triggerMode model.TriggerMode
selected bool
clock func() time.Time
}
func NewResourceView(res view.Resource, rv view.ResourceViewState, triggerMode model.TriggerMode,
selected bool, clock func() time.Time) *ResourceView {
return &ResourceView{
res: res,
rv: rv,
triggerMode: triggerMode,
selected: selected,
clock: clock,
}
}
func (v *ResourceView) Build() rty.Component {
layout := rty.NewConcatLayout(rty.DirVert)
layout.Add(v.resourceTitle())
if v.res.IsCollapsed(v.rv) {
return layout
}
layout.Add(v.resourceExpandedPane())
return layout
}
func (v *ResourceView) resourceTitle() rty.Component {
l := rty.NewConcatLayout(rty.DirHor)
l.Add(v.titleTextName())
l.Add(rty.TextString(" "))
l.AddDynamic(rty.Fg(rty.NewFillerString('╌'), cLightText))
l.Add(rty.TextString(" "))
if tt := v.titleText(); tt != nil {
l.Add(tt)
l.Add(middotText())
}
l.Add(v.titleTextBuild())
l.Add(middotText())
l.Add(v.titleTextDeploy())
return rty.OneLine(l)
}
func statusColor(res view.Resource) tcell.Color {
if res.IsTiltfile {
if !res.CurrentBuild.Empty() {
return cPending
} else if res.CrashLog.Empty() {
return cGood
} else {
return cBad
}
}
if !res.CurrentBuild.Empty() && !res.CurrentBuild.Reason.IsCrashOnly() {
return cPending
} else if !res.PendingBuildSince.IsZero() && !res.PendingBuildReason.IsCrashOnly() {
if res.TriggerMode == model.TriggerModeAuto {
return cPending
} else {
return cLightText
}
} else if isCrashing(res) {
return cBad
} else if res.LastBuild().Error != nil {
return cBad
} else if res.IsYAML() && !res.LastDeployTime.IsZero() {
return cGood
} else if !res.LastBuild().FinishTime.IsZero() && res.ResourceInfo.Status() == "" {
return cPending // pod status hasn't shown up yet
} else {
if res.ResourceInfo != nil {
if statusColor, ok := statusColors[res.ResourceInfo.Status()]; ok {
return statusColor
}
}
return cLightText
}
}
func (v *ResourceView) titleTextName() rty.Component {
sb := rty.NewStringBuilder()
selected := v.selected
p := " "
if selected {
p = "▼"
}
if selected && v.res.IsCollapsed(v.rv) {
p = "▶"
}
color := statusColor(v.res)
sb.Text(p)
sb.Fg(color).Textf(" ● ")
name := v.res.Name.String()
if color == cPending {
name = fmt.Sprintf("%s %s", v.res.Name, v.spinner())
}
if len(warnings(v.res)) > 0 {
name = fmt.Sprintf("%s %s", v.res.Name, "— Warning ⚠️")
}
sb.Fg(tcell.ColorDefault).Text(name)
return sb.Build()
}
func (v *ResourceView) titleText() rty.Component {
switch i := v.res.ResourceInfo.(type) {
case view.DCResourceInfo:
return titleTextDC(i)
case view.K8sResourceInfo:
return titleTextK8s(i)
default:
return nil
}
}
func titleTextK8s(k8sInfo view.K8sResourceInfo) rty.Component {
status := k8sInfo.PodStatus
if status == "" {
status = "Pending"
}
return rty.TextString(status)
}
func titleTextDC(dcInfo view.DCResourceInfo) rty.Component {
sb := rty.NewStringBuilder()
status := dcInfo.Status()
if status == "" {
status = "Pending"
}
sb.Text(status)
return sb.Build()
}
func (v *ResourceView) titleTextBuild() rty.Component {
return buildStatusCell(makeBuildStatus(v.res, v.triggerMode))
}
func (v *ResourceView) titleTextDeploy() rty.Component {
return deployTimeCell(v.res.LastDeployTime, tcell.ColorDefault)
}
func (v *ResourceView) resourceExpandedPane() rty.Component {
l := rty.NewConcatLayout(rty.DirHor)
l.Add(rty.TextString(strings.Repeat(" ", 4)))
rhs := rty.NewConcatLayout(rty.DirVert)
rhs.Add(v.resourceExpandedHistory())
rhs.Add(v.resourceExpanded())
rhs.Add(v.resourceExpandedEndpoints())
rhs.Add(v.resourceExpandedError())
l.AddDynamic(rhs)
return l
}
func (v *ResourceView) resourceExpanded() rty.Component {
switch v.res.ResourceInfo.(type) {
case view.DCResourceInfo:
return v.resourceExpandedDC()
case view.K8sResourceInfo:
return v.resourceExpandedK8s()
case view.YAMLResourceInfo:
return v.resourceExpandedYAML()
default:
return rty.EmptyLayout
}
}
func (v *ResourceView) resourceExpandedYAML() rty.Component {
yi := v.res.YAMLInfo()
if !v.res.IsYAML() || len(yi.K8sResources) == 0 {
return rty.EmptyLayout
}
l := rty.NewConcatLayout(rty.DirHor)
l.Add(rty.TextString(strings.Repeat(" ", 2)))
rhs := rty.NewConcatLayout(rty.DirVert)
rhs.Add(rty.NewStringBuilder().Fg(cLightText).Text("(Kubernetes objects that don't match a group)").Build())
rhs.Add(rty.TextString(strings.Join(yi.K8sResources, "\n")))
l.AddDynamic(rhs)
return l
}
func (v *ResourceView) resourceExpandedDC() rty.Component {
dcInfo := v.res.DCInfo()
l := rty.NewConcatLayout(rty.DirHor)
l.Add(v.resourceTextDCContainer(dcInfo))
l.Add(rty.TextString(" "))
l.AddDynamic(rty.NewFillerString(' '))
st := v.res.DockerComposeTarget().StartTime
if !st.IsZero() {
if len(v.res.Endpoints) > 0 {
v.appendEndpoints(l)
l.Add(middotText())
}
l.Add(resourceTextAge(st))
}
return rty.OneLine(l)
}
func (v *ResourceView) resourceTextDCContainer(dcInfo view.DCResourceInfo) rty.Component {
if dcInfo.ContainerID.String() == "" {
return rty.EmptyLayout
}
sb := rty.NewStringBuilder()
sb.Fg(cLightText).Text("Container ID: ")
sb.Fg(tcell.ColorDefault).Text(dcInfo.ContainerID.ShortStr())
return sb.Build()
}
func (v *ResourceView) endpointsNeedSecondLine() bool {
if len(v.res.Endpoints) > 1 {
return true
}
if v.res.IsK8s() && v.res.K8sInfo().PodRestarts > 0 && len(v.res.Endpoints) == 1 {
return true
}
return false
}
func (v *ResourceView) resourceExpandedK8s() rty.Component {
k8sInfo := v.res.K8sInfo()
if k8sInfo.PodName == "" {
return rty.EmptyLayout
}
l := rty.NewConcatLayout(rty.DirHor)
l.Add(resourceTextPodName(k8sInfo))
l.Add(rty.TextString(" "))
l.AddDynamic(rty.NewFillerString(' '))
l.Add(rty.TextString(" "))
if k8sInfo.PodRestarts > 0 {
l.Add(resourceTextPodRestarts(k8sInfo))
l.Add(middotText())
}
if len(v.res.Endpoints) > 0 && !v.endpointsNeedSecondLine() {
v.appendEndpoints(l)
l.Add(middotText())
}
l.Add(resourceTextAge(k8sInfo.PodCreationTime))
return rty.OneLine(l)
}
func resourceTextPodName(k8sInfo view.K8sResourceInfo) rty.Component {
sb := rty.NewStringBuilder()
sb.Fg(cLightText).Text("K8S POD: ")
sb.Fg(tcell.ColorDefault).Text(k8sInfo.PodName)
return sb.Build()
}
func resourceTextPodRestarts(k8sInfo view.K8sResourceInfo) rty.Component {
s := "restarts"
if k8sInfo.PodRestarts == 1 {
s = "restart"
}
return rty.NewStringBuilder().
Fg(cPending).
Textf("%d %s", k8sInfo.PodRestarts, s).
Build()
}
func resourceTextAge(t time.Time) rty.Component {
sb := rty.NewStringBuilder()
sb.Fg(cLightText).Text("AGE ")
sb.Fg(tcell.ColorDefault).Text(formatDeployAge(time.Since(t)))
return rty.NewMinLengthLayout(DeployCellMinWidth, rty.DirHor).
SetAlign(rty.AlignEnd).
Add(sb.Build())
}
func (v *ResourceView) appendEndpoints(l *rty.ConcatLayout) {
for i, endpoint := range v.res.Endpoints {
if i != 0 {
l.Add(middotText())
}
l.Add(rty.TextString(endpoint))
}
}
func (v *ResourceView) resourceExpandedEndpoints() rty.Component {
if !v.endpointsNeedSecondLine() {
return rty.NewConcatLayout(rty.DirVert)
}
l := rty.NewConcatLayout(rty.DirHor)
l.Add(resourceTextURLPrefix())
v.appendEndpoints(l)
return l
}
func resourceTextURLPrefix() rty.Component {
sb := rty.NewStringBuilder()
sb.Fg(cLightText).Text("URL: ")
return sb.Build()
}
func (v *ResourceView) resourceExpandedHistory() rty.Component {
if v.res.IsYAML() {
return rty.NewConcatLayout(rty.DirVert)
}
if v.res.CurrentBuild.Empty() && len(v.res.BuildHistory) == 0 {
return rty.NewConcatLayout(rty.DirVert)
}
l := rty.NewConcatLayout(rty.DirHor)
l.Add(rty.NewStringBuilder().Fg(cLightText).Text("HISTORY: ").Build())
rows := rty.NewConcatLayout(rty.DirVert)
rowCount := 0
if !v.res.CurrentBuild.Empty() {
rows.Add(NewEditStatusLine(buildStatus{
edits: v.res.CurrentBuild.Edits,
reason: v.res.CurrentBuild.Reason,
duration: v.res.CurrentBuild.Duration(),
status: "Building",
muted: true,
}))
rowCount++
}
for _, bStatus := range v.res.BuildHistory {
if rowCount >= 2 {
// at most 2 rows
break
}
status := "OK"
if bStatus.Error != nil {
status = "Error"
}
rows.Add(NewEditStatusLine(buildStatus{
edits: bStatus.Edits,
reason: bStatus.Reason,
duration: bStatus.Duration(),
status: status,
deployTime: bStatus.FinishTime,
}))
rowCount++
}
l.AddDynamic(rows)
return l
}
func (v *ResourceView) resourceExpandedError() rty.Component {
errPane, ok := v.resourceExpandedBuildError()
isWarnings := false
if !ok {
errPane, ok = v.resourceExpandedRuntimeError()
}
if !ok {
errPane, ok = v.resourceExpandedWarnings()
if ok {
isWarnings = true
}
}
if !ok {
return rty.NewConcatLayout(rty.DirVert)
}
l := rty.NewConcatLayout(rty.DirVert)
if isWarnings {
l.Add(rty.NewStringBuilder().Fg(cLightText).Text("WARNINGS:").Build())
} else {
l.Add(rty.NewStringBuilder().Fg(cLightText).Text("ERROR:").Build())
}
indentPane := rty.NewConcatLayout(rty.DirHor)
indentPane.Add(rty.TextString(strings.Repeat(" ", 3)))
errPane = rty.NewTailLayout(errPane)
errPane = rty.NewMaxLengthLayout(errPane, rty.DirVert, MaxInlineErrHeight)
indentPane.Add(errPane)
l.Add(indentPane)
return l
}
func (v *ResourceView) resourceExpandedRuntimeError() (rty.Component, bool) {
pane := rty.NewConcatLayout(rty.DirVert)
ok := false
if isCrashing(v.res) {
runtimeLog := v.res.CrashLog.Tail(abbreviatedLogLineCount).String()
if runtimeLog == "" {
runtimeLog = v.res.ResourceInfo.RuntimeLog().Tail(abbreviatedLogLineCount).String()
}
abbrevLog := abbreviateLog(runtimeLog)
for _, logLine := range abbrevLog {
pane.Add(rty.TextString(logLine))
ok = true
}
}
return pane, ok
}
func (v *ResourceView) resourceExpandedWarnings() (rty.Component, bool) {
pane := rty.NewConcatLayout(rty.DirVert)
ok := false
if len(warnings(v.res)) > 0 {
abbrevLog := abbreviateLog(strings.Join(warnings(v.res), "\n"))
for _, logLine := range abbrevLog {
pane.Add(rty.TextString(logLine))
ok = true
}
}
return pane, ok
}
func (v *ResourceView) resourceExpandedBuildError() (rty.Component, bool) {
pane := rty.NewConcatLayout(rty.DirVert)
ok := false
if v.res.LastBuild().Error != nil {
abbrevLog := abbreviateLog(v.res.LastBuild().Log.Tail(abbreviatedLogLineCount).String())
for _, logLine := range abbrevLog {
pane.Add(rty.TextString(logLine))
ok = true
}
// if the build log is non-empty, it will contain the error, so we don't need to show this separately
if len(abbrevLog) == 0 {
pane.Add(rty.TextString(fmt.Sprintf("Error: %s", v.res.LastBuild().Error)))
ok = true
}
}
return pane, ok
}
var spinnerChars = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
func (v *ResourceView) spinner() string {
decisecond := v.clock().Nanosecond() / int(time.Second/10)
return spinnerChars[decisecond%len(spinnerChars)] // tick spinner every 10x/second
}