-
Notifications
You must be signed in to change notification settings - Fork 12
/
table.go
144 lines (111 loc) · 3.83 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: Apache-2.0
package repo
import (
"strings"
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/gosuri/uitable"
"github.com/sirupsen/logrus"
)
// table is a helper function to output the
// provided repos in a table format with
// a specific set of fields displayed.
func table(repos *[]library.Repo) error {
logrus.Debug("creating table for list of repos")
// 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 repo table")
// set of repository fields we display in a table
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow("ORG/REPO", "ACTIVE", "EVENTS", "VISIBILITY", "BRANCH")
// iterate through all repos in the list
for _, r := range *repos {
logrus.Tracef("adding repo %s to repo table", r.GetFullName())
//nolint:gosec // ignore memory aliasing
e := strings.Join(events(&r), ",")
// add a row to the table with the specified values
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow(r.GetFullName(), r.GetActive(), e, r.GetVisibility(), r.GetBranch())
}
// 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 repos in a wide table format with
// a specific set of fields displayed.
func wideTable(repos *[]library.Repo) error {
logrus.Debug("creating wide table for list of repos")
// 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 repo table")
// set of repository fields we display in a wide table
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow("ORG/REPO", "ACTIVE", "EVENTS", "VISIBILITY", "BRANCH", "REMOTE")
// iterate through all repos in the list
for _, r := range *repos {
logrus.Tracef("adding repo %s to wide repo table", r.GetFullName())
//nolint:gosec // ignore memory aliasing
e := strings.Join(events(&r), ",")
// add a row to the table with the specified values
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow(r.GetFullName(), r.GetActive(), e, r.GetVisibility(), r.GetBranch(), r.GetLink())
}
// 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)
}
// events is a helper function to output
// the event types a repo is configured
// to trigger builds off of.
func events(r *library.Repo) []string {
e := []string{}
// check if the repository allows comment events
if r.GetAllowComment() {
e = append(e, constants.EventComment)
}
// check if the repository allows deployment events
if r.GetAllowDeploy() {
e = append(e, constants.EventDeploy)
}
// check if the repository allows pull_request events
if r.GetAllowPull() {
e = append(e, constants.EventPull)
}
// check if the repository allows push events
if r.GetAllowPush() {
e = append(e, constants.EventPush)
}
// check if the repository allows tag events
if r.GetAllowTag() {
e = append(e, constants.EventTag)
}
return e
}