forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_table.go
109 lines (90 loc) · 2.52 KB
/
event_table.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
package cmd
import (
"fmt"
"sort"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshui "github.com/cloudfoundry/bosh-cli/ui"
boshtbl "github.com/cloudfoundry/bosh-cli/ui/table"
)
type EventTable struct {
Event boshdir.Event
UI boshui.UI
}
func (t EventTable) Print() {
id := t.Event.ID()
if t.Event.ParentID() != "" {
id += " <- " + t.Event.ParentID()
}
table := boshtbl.Table{
Rows: [][]boshtbl.Value{
{
boshtbl.NewValueString("ID"),
boshtbl.NewValueString(id),
},
{
boshtbl.NewValueString("Time"),
boshtbl.NewValueTime(t.Event.Timestamp()),
},
},
}
if len(t.Event.User()) > 0 {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("User"),
boshtbl.NewValueString(t.Event.User()),
})
}
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("Action"),
boshtbl.NewValueString(t.Event.Action()),
})
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("Object Type"),
boshtbl.NewValueString(t.Event.ObjectType()),
})
if len(t.Event.ObjectName()) > 0 {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("Object ID"),
boshtbl.NewValueString(t.Event.ObjectName()),
})
}
if len(t.Event.TaskID()) > 0 {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("Task ID"),
boshtbl.NewValueString(t.Event.TaskID()),
})
}
if len(t.Event.DeploymentName()) > 0 {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("Deployment"),
boshtbl.NewValueString(t.Event.DeploymentName()),
})
}
if len(t.Event.Instance()) > 0 {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("Instance"),
boshtbl.NewValueString(t.Event.Instance()),
})
}
if len(t.Event.Context()) > 0 {
desc := []string{}
for name, value := range t.Event.Context() {
desc = append(desc, fmt.Sprintf("%s: %s", name, value))
}
sort.Sort(EventContextSorting(desc))
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("Context"),
boshtbl.NewValueStrings(desc),
})
}
if len(t.Event.Error()) > 0 {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString("Error"),
boshtbl.NewValueString(t.Event.Error()),
})
}
t.UI.PrintTable(table)
}
type EventContextSorting []string
func (s EventContextSorting) Len() int { return len(s) }
func (s EventContextSorting) Less(i, j int) bool { return s[i] < s[j] }
func (s EventContextSorting) Swap(i, j int) { s[i], s[j] = s[j], s[i] }