forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_table.go
102 lines (82 loc) · 2.24 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
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{
Header: []boshtbl.Header{
boshtbl.NewHeader("ID"),
boshtbl.NewHeader("Time"),
},
Rows: [][]boshtbl.Value{
{
boshtbl.NewValueString(id),
boshtbl.NewValueTime(t.Event.Timestamp()),
},
},
Transpose: true,
}
if len(t.Event.User()) > 0 {
table = table.AddColumn("User", []boshtbl.Value{
boshtbl.NewValueString(t.Event.User()),
})
}
table = table.AddColumn("Action", []boshtbl.Value{
boshtbl.NewValueString(t.Event.Action()),
})
table = table.AddColumn("Object Type", []boshtbl.Value{
boshtbl.NewValueString(t.Event.ObjectType()),
})
if len(t.Event.ObjectName()) > 0 {
table = table.AddColumn("Object Name", []boshtbl.Value{
boshtbl.NewValueString(t.Event.ObjectName()),
})
}
if len(t.Event.TaskID()) > 0 {
table = table.AddColumn("Task ID", []boshtbl.Value{
boshtbl.NewValueString(t.Event.TaskID()),
})
}
if len(t.Event.DeploymentName()) > 0 {
table = table.AddColumn("Deployment", []boshtbl.Value{
boshtbl.NewValueString(t.Event.DeploymentName()),
})
}
if len(t.Event.Instance()) > 0 {
table = table.AddColumn("Instance", []boshtbl.Value{
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 = table.AddColumn("Context", []boshtbl.Value{
boshtbl.NewValueStrings(desc),
})
}
if len(t.Event.Error()) > 0 {
table = table.AddColumn("Error", []boshtbl.Value{
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] }