-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
199 lines (175 loc) · 4.41 KB
/
common.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package commands
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/jaypipes/procession/proto"
)
const (
permissionsHelpExtended = `
NOTE: To find out what permissions may be applied to a role, use
the p7n permissions command.
`
)
const (
rolesHelpExtended = `
NOTE: To find out what roles a user may be added to, use
the p7n role list command.
`
)
const (
errUnsetUser = `Error: unable to find the authenticating user.
Please set the PROCESSION_USER environment variable or supply a value
for the --user CLI option.
`
errConnect = `Error: unable to connect to the Procession server.
Please check the PROCESSION_HOST and PROCESSION_PORT environment
variables or --host and --port CLI options.
`
errForbidden = `Error: you are not authorized to perform that action.`
errBadVisibility = `Error: incorrect value for visibility.
Valid values are PUBLIC or PRIVATE.
`
)
const (
msgNoRecords = "No records found matching search criteria."
)
// Some commonly-used CLI options
const (
defaultListLimit = 50
defaultListSort = "uuid:asc"
)
var (
listLimit int
listMarker string
listSort string
)
// Checks the given string to ensure it matches an appropriate value for a
// visibility setting and returns the matching integer value or exits with a
// usage message
func checkVisibility(cmd *cobra.Command, value string) pb.Visibility {
ival, found := pb.Visibility_value[strings.ToUpper(value)]
if !found {
fmt.Println(errBadVisibility)
cmd.Usage()
os.Exit(1)
}
return pb.Visibility(ival)
}
func exitIfConnectErr(err error) {
if err != nil {
fmt.Println(errConnect)
os.Exit(1)
}
}
func exitIfForbidden(err error) {
if s, ok := status.FromError(err); ok {
if s.Code() == codes.PermissionDenied {
fmt.Println(errForbidden)
os.Exit(int(s.Code()))
}
}
}
// Writes a generic error to output and exits if supplied error is an error
func exitIfError(err error) {
if s, ok := status.FromError(err); ok {
if s.Code() != codes.OK {
fmt.Printf("Error: %s\n", s.Message())
os.Exit(int(s.Code()))
}
}
}
func exitNoRecords() {
if !quiet {
fmt.Println(msgNoRecords)
}
os.Exit(0)
}
func checkAuthUser(cmd *cobra.Command) {
if authUser == "" && !cmd.Flags().Changed("user") {
fmt.Println(errUnsetUser)
cmd.Usage()
os.Exit(1)
}
}
func connect() *grpc.ClientConn {
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
connectAddress := fmt.Sprintf("%s:%d", connectHost, connectPort)
conn, err := grpc.Dial(connectAddress, opts...)
if err != nil {
fmt.Println(errConnect)
os.Exit(1)
return nil
}
return conn
}
func printIf(b bool, msg string, args ...interface{}) {
if b {
fmt.Printf(msg, args...)
}
}
// Appends the standard listing CLI options to the supplied command struct
func addListOptions(cmd *cobra.Command) {
cmd.Flags().IntVarP(
&listLimit,
"limit", "",
defaultListLimit,
"Number of records to limit results to.",
)
cmd.Flags().StringVarP(
&listMarker,
"marker", "",
"",
"Identifier of the last record on the previous page of results.",
)
cmd.Flags().StringVarP(
&listSort,
"sort", "",
defaultListSort,
"Comma-delimited list of 'field:direction' indicators for sorting results",
)
}
// Examines the supplied search listing options and returns a constructed
// pb.SearchOptions message struct for use in the request to the Procession
// service
func buildSearchOptions(cmd *cobra.Command) *pb.SearchOptions {
sortIndicators := strings.Split(listSort, ",")
sortFields := make([]*pb.SortField, len(sortIndicators))
for x, indicator := range sortIndicators {
parts := strings.Split(indicator, ":")
direction := pb.SortDirection_ASC
if len(parts) == 2 {
askedDir := strings.ToUpper(parts[1])
if val, ok := pb.SortDirection_value[askedDir]; !ok {
validVals := make([]string, 0)
for _, validVal := range pb.SortDirection_name {
validVals = append(validVals, validVal)
}
fmt.Printf(
"Error: unknown sort direction '%s'. Valid values: %v\n",
askedDir,
validVals,
)
os.Exit(1)
} else {
direction = pb.SortDirection(val)
}
}
sortField := &pb.SortField{
Field: parts[0],
Direction: direction,
}
sortFields[x] = sortField
}
res := &pb.SearchOptions{
Limit: uint32(listLimit),
Marker: listMarker,
SortFields: sortFields,
}
return res
}