-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
146 lines (124 loc) · 3.2 KB
/
format.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/olekukonko/tablewriter"
"github.com/buger/goterm"
"github.com/mgutz/ansi"
"os"
"strings"
"github.com/dustin/go-humanize"
//"time"
)
var red = ansi.ColorCode("red+b")
var green = ansi.ColorCode("green+h:black")
var yellow = ansi.ColorCode("yellow")
var reset = ansi.ColorCode("reset")
var writer = os.Stdout
var eventmap map[string]*cloudformation.StackEvent = make(map[string]*cloudformation.StackEvent)
var stackname string
var stackevent *cloudformation.StackEvent
var table = tablewriter.NewWriter(goterm.Output)
func PrintEventsAsTable(events []*cloudformation.StackEvent) {
if len(events) == 0 {
return
}
updateEventMap(events)
printEventTable()
}
func getTableData() [][]string {
data := make([][]string, len(eventmap))
i := 0
for id, event := range eventmap {
if id == stackname {
stackevent = event
} else {
data[i] = []string{id, getTimestamp(event),
getStatusString(*event.ResourceStatus),
getReasonString(event.ResourceStatusReason)}
i++
}
}
return data
}
func printEventTable() {
// TODO: fix terminal formatting
goterm.Clear()
goterm.MoveCursor(1, 1)
table.SetHeader([]string{"Resource", "Time", "Status", "Reason"})
if stackevent != nil {
table.SetFooter([]string{stackname,
getTimestamp(stackevent),
getStatusString(*stackevent.ResourceStatus), ""})
}
table.SetBorder(false)
table.SetAutoFormatHeaders(false)
table.ClearRows()
table.AppendBulk(getTableData())
table.Render()
goterm.Flush()
}
func updateEventMap(events []*cloudformation.StackEvent) {
stackname = *events[0].StackName
for _, event := range events {
if event != nil {
eventmap[*event.LogicalResourceId] = event
}
}
}
func getReasonString(reason *string) string {
if reason != nil {
return *reason
} else {
return ""
}
}
func getStatusString(status string) string {
return fmt.Sprintf("%s%s%s", getColor(status), status, reset)
}
func getTimestamp(event *cloudformation.StackEvent) string {
if event == nil || event.Timestamp == nil {
return ""
}
return humanize.Time(*event.Timestamp)
//return event.Timestamp.Local().Format(time.UnixDate)
}
func PrintEventsAsLog(events []*cloudformation.StackEvent) {
for _, event := range events {
printEventLine(*event)
}
}
func printEventLine(event cloudformation.StackEvent) {
status := *event.ResourceStatus
printEventColor(status)
printEvent(event)
printReset()
}
func printEvent(event cloudformation.StackEvent) {
timestamp := getTimestamp(&event)
fmt.Fprintf(writer, "%s - %s - %s", *event.LogicalResourceId, timestamp, *event.ResourceStatus)
if event.ResourceStatusReason != nil {
fmt.Fprintf(writer, " - %s", *event.ResourceStatusReason)
}
}
func printReset() (int, error) {
return fmt.Fprintf(writer, "%s\n", reset)
}
func printEventColor(status string) {
color := getColor(status)
fmt.Fprintf(writer, "%s", color)
}
func getColor(status string) string {
var color string
if isInProgress(status) {
color = yellow
} else if isFailed(status) {
color = red
} else {
color = green
}
return color
}
func isFailed(status string) bool {
return strings.Contains(status, "ROLLBACK") || strings.Contains(status, "FAIL")
}