-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtable.go
125 lines (99 loc) · 3.61 KB
/
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// SPDX-License-Identifier: Apache-2.0
package hook
import (
"sort"
"time"
"github.com/dustin/go-humanize"
"github.com/gosuri/uitable"
"github.com/sirupsen/logrus"
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/types/library"
)
// table is a helper function to output the
// provided hooks in a table format with
// a specific set of fields displayed.
func table(hooks *[]library.Hook) error {
logrus.Debug("creating table for list of hooks")
// create a new table
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#New
table := uitable.New()
// set column width for table to 50
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table
table.MaxColWidth = 50
// ensure the table is always wrapped
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table
table.Wrap = true
logrus.Trace("adding headers to hook table")
// set of hook fields we display in a table
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow("NUMBER", "STATUS", "EVENT", "BRANCH", "CREATED")
// iterate through all hooks in the list
for _, h := range reverse(*hooks) {
logrus.Tracef("adding hook %d to hook table", h.GetNumber())
// calculate created timestamp in human readable form
//
// https://pkg.go.dev/github.com/dustin/go-humanize?tab=doc#Time
c := humanize.Time(time.Unix(h.GetCreated(), 0))
// add a row to the table with the specified values
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow(h.GetNumber(), h.GetStatus(), h.GetEvent(), h.GetBranch(), c)
}
// output the table in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(table)
}
// wideTable is a helper function to output the
// provided hooks in a wide table format with
// a specific set of fields displayed.
func wideTable(hooks *[]library.Hook) error {
logrus.Debug("creating wide table for list of hooks")
// create new wide table
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#New
table := uitable.New()
// set column width for wide table to 200
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table
table.MaxColWidth = 200
// ensure the wide table is always wrapped
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table
table.Wrap = true
logrus.Trace("adding headers to wide hook table")
// set of hook fields we display in a wide table
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow("NUMBER", "SOURCE", "STATUS", "HOST", "EVENT", "BRANCH", "CREATED")
// iterate through all hooks in the list
for _, h := range reverse(*hooks) {
logrus.Tracef("adding hook %d to wide hook table", h.GetNumber())
// calculate created timestamp in human readable form
//
// https://pkg.go.dev/github.com/dustin/go-humanize?tab=doc#Time
c := humanize.Time(time.Unix(h.GetCreated(), 0))
// add a row to the table with the specified values
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow(h.GetNumber(), h.GetSourceID(), h.GetStatus(), h.GetHost(), h.GetEvent(), h.GetBranch(), c)
}
// output the wide table in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(table)
}
// reverse is a helper function to sort the hooks
// based off the hook number and then flip the
// order they get displayed in.
func reverse(h []library.Hook) []library.Hook {
// sort the list of hooks based off the hook number
sort.SliceStable(h, func(i, j int) bool {
return h[i].GetNumber() < h[j].GetNumber()
})
return h
}