forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
93 lines (74 loc) · 1.84 KB
/
render.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
package eventstream
import (
"fmt"
"io"
"strings"
"github.com/concourse/atc/event"
"github.com/concourse/fly/ui"
"github.com/concourse/go-concourse/concourse/eventstream"
"github.com/fatih/color"
)
func Render(dst io.Writer, src eventstream.EventStream) int {
var buildConfig event.TaskConfig
exitStatus := 0
for {
ev, err := src.NextEvent()
if err != nil {
if err == io.EOF {
return exitStatus
} else {
fmt.Fprintf(dst, "failed to parse next event: %s\n", err)
return 255
}
}
switch e := ev.(type) {
case event.Log:
fmt.Fprintf(dst, "%s", e.Payload)
case event.InitializeTask:
buildConfig = e.TaskConfig
if buildConfig.Image != "" {
fmt.Fprintf(dst, "\x1b[1minitializing with %s\x1b[0m\n", buildConfig.Image)
} else {
fmt.Fprintf(dst, "\x1b[1minitializing\x1b[0m\n")
}
case event.StartTask:
argv := strings.Join(append([]string{buildConfig.Run.Path}, buildConfig.Run.Args...), " ")
fmt.Fprintf(dst, "\x1b[1mrunning %s\x1b[0m\n", argv)
case event.FinishTask:
exitStatus = e.ExitStatus
case event.Error:
errCol := ui.ErroredColor.SprintFunc()
fmt.Fprintf(dst, "%s\n", errCol(e.Message))
case event.Status:
var printColor *color.Color
switch e.Status {
case "started":
continue
case "succeeded":
printColor = ui.SucceededColor
case "failed":
printColor = ui.FailedColor
if exitStatus == 0 {
exitStatus = 1
}
case "errored":
printColor = ui.ErroredColor
if exitStatus == 0 {
exitStatus = 2
}
case "aborted":
printColor = ui.AbortedColor
if exitStatus == 0 {
exitStatus = 3
}
default:
fmt.Fprintf(dst, "unknown status: %s", e.Status)
return 255
}
printColorFunc := printColor.SprintFunc()
fmt.Fprintf(dst, "%s\n", printColorFunc(e.Status))
return exitStatus
}
}
return 255
}