-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtable.go
134 lines (107 loc) · 3.76 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
// SPDX-License-Identifier: Apache-2.0
package secret
import (
"fmt"
"strings"
"github.com/gosuri/uitable"
"github.com/sirupsen/logrus"
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
)
// table is a helper function to output the
// provided secrets in a table format with
// a specific set of fields displayed.
func table(secrets *[]library.Secret) error {
logrus.Debug("creating table for list of secrets")
// 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 secret table")
// set of secret fields we display in a table
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow("NAME", "ORG", "TYPE", "KEY")
// iterate through all secrets in the list
for _, s := range *secrets {
logrus.Tracef("adding secret %s to secret table", s.GetName())
// calculate the key for the secret
//
//nolint:gosec // ignore memory aliasing
k := key(&s)
// add a row to the table with the specified values
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow(s.GetName(), s.GetOrg(), s.GetType(), k)
}
// 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 secrets in a wide table format with
// a specific set of fields displayed.
func wideTable(secrets *[]library.Secret) error {
logrus.Debug("creating wide table for list of secrets")
// 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 secret table")
// set of secret fields we display in a wide table
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow("NAME", "ORG", "TYPE", "KEY", "EVENTS", "IMAGES", "ALLOW COMMANDS", "ALLOW SUBSTITUTION")
// iterate through all secrets in the list
for _, s := range *secrets {
logrus.Tracef("adding secret %s to wide secret table", s.GetName())
// capture list of events for secret
e := strings.Join(s.GetAllowEvents().List(), ",")
// capture list of images for secret
i := strings.Join(s.GetImages(), ",")
// calculate the key for the secret
//
//nolint:gosec // ignore memory aliasing
k := key(&s)
// add a row to the table with the specified values
//
// https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow
table.AddRow(s.GetName(), s.GetOrg(), s.GetType(), k, e, i, s.GetAllowCommand(), s.GetAllowSubstitution())
}
// 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)
}
// key is a helper function to calculate the full
// path to a secret in the storage backend.
func key(s *library.Secret) string {
switch s.GetType() {
case constants.SecretShared:
return fmt.Sprintf("%s/%s/%s", s.GetOrg(), s.GetTeam(), s.GetName())
case constants.SecretOrg:
return fmt.Sprintf("%s/%s", s.GetOrg(), s.GetName())
case constants.SecretRepo:
fallthrough
default:
return fmt.Sprintf("%s/%s/%s", s.GetOrg(), s.GetRepo(), s.GetName())
}
}