-
Notifications
You must be signed in to change notification settings - Fork 20
/
cmdutil.go
428 lines (332 loc) · 8.05 KB
/
cmdutil.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package cmdutil
import (
"fmt"
"strings"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/internal/build"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/AlecAivazis/survey/v2"
p "github.com/gertd/go-pluralize"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
OutputFormatWide = "wide"
OutputFormatCSV = "csv"
)
func Add(root *cobra.Command, sub *cobra.Command) *cobra.Command {
if err := bindPFlags(sub); err != nil {
panic(err)
}
root.AddCommand(sub)
return root
}
func Bind(root *cobra.Command, subs ...*cobra.Command) *cobra.Command {
if err := bindPFlags(root); err != nil {
panic(err)
}
for _, s := range subs {
Add(root, s)
}
return root
}
func bindPFlags(cmd *cobra.Command) (err error) {
pl := p.NewClient()
cmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
if err != nil {
return
}
err = bindFlag(pl, flag)
})
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
if err != nil {
return
}
err = bindFlag(pl, flag)
})
return err
}
func bindFlag(pl *p.Client, flag *pflag.Flag) error {
name := flag.Name
name = strings.ReplaceAll(name, "_", "-")
name = strings.ReplaceAll(name, ".", "-")
if err := viper.BindPFlag(name, flag); err != nil {
return fmt.Errorf("error binding flag %s to viper: %v", flag.Name, err)
}
// this is a little bit of an hack to register plural version of properties
// based on the naming conventions used by the flag type because it is not
// possible to know what is the type of a flag
flagType := strings.ToUpper(flag.Value.Type())
if strings.Contains(flagType, "SLICE") || strings.Contains(flagType, "ARRAY") {
if err := viper.BindPFlag(pl.Plural(name), flag); err != nil {
return fmt.Errorf("error binding plural flag %s to viper: %v", flag.Name, err)
}
}
return nil
}
func PromptConfirm(format string, args ...interface{}) (bool, error) {
promptConfirm := survey.Confirm{
Message: fmt.Sprintf(format, args...),
}
var confirmDelete bool
if err := survey.AskOne(&promptConfirm, &confirmDelete); err != nil {
return false, err
}
return confirmDelete, nil
}
func ValidOutputs() []string {
validVals := make([]string, 0, len(flagutil.ValidOutputFormats)+1)
validVals = append(validVals, flagutil.ValidOutputFormats...)
validVals = append(validVals, OutputFormatWide)
validVals = append(validVals, OutputFormatCSV)
return validVals
}
func ValidateOutputs(cmd *cobra.Command) error {
formats, err := cmd.Flags().GetString("output")
if err != nil {
return err
}
if formats != "" && !flagutil.IsValidInput(formats, ValidOutputs()...) {
return flagutil.InvalidValueError("output", formats, ValidOutputs()...)
}
return nil
}
func AddOutput(cmd *cobra.Command, output *string) *FlagOptions {
validVals := ValidOutputs()
name := "output"
cmd.Flags().StringVarP(
output,
name,
"o",
dump.EmptyFormat,
"Specify the output format. Choose from: "+strings.Join(validVals, ", "),
)
_ = cmd.RegisterFlagCompletionFunc(name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return validVals, cobra.ShellCompDirectiveNoSpace
})
return withFlagOptions(cmd, name)
}
func AddPage(cmd *cobra.Command, output *int) *FlagOptions {
name := "page"
cmd.Flags().IntVarP(
output,
name,
"p",
build.DefaultPageNumber,
"Page index",
)
return withFlagOptions(cmd, name)
}
func AddLimit(cmd *cobra.Command, output *int) *FlagOptions {
name := "limit"
cmd.Flags().IntVarP(
output,
name,
"l",
build.DefaultPageSize,
"Number of items in each page",
)
return withFlagOptions(cmd, name)
}
func AddAllPages(cmd *cobra.Command, output *bool) *FlagOptions {
name := "all-pages"
cmd.Flags().BoolVar(
output,
name,
false,
"Grab all pages",
)
return withFlagOptions(cmd, name)
}
func AddOrderBy(cmd *cobra.Command, output *string) *FlagOptions {
name := "order-by"
cmd.Flags().StringVar(
output,
name,
*output,
"Specifies the order by criteria",
)
return withFlagOptions(cmd, name)
}
func AddSearch(cmd *cobra.Command, output *string) *FlagOptions {
name := "search"
cmd.Flags().StringVar(
output,
name,
*output,
"Search criteria",
)
return withFlagOptions(cmd, name)
}
func AddClusterID(cmd *cobra.Command, output *string) *FlagOptions {
name := "cluster-id"
cmd.Flags().StringVarP(
output,
name,
"c",
*output,
"Cluster ID",
)
return withFlagOptions(cmd, name)
}
func AddNamespaceID(cmd *cobra.Command, output *string) *FlagOptions {
name := "namespace-id"
cmd.Flags().StringVarP(
output,
name,
"n",
*output,
"Namespace ID",
)
return withFlagOptions(cmd, name)
}
func AddID(cmd *cobra.Command, output *string) *FlagOptions {
name := "id"
cmd.Flags().StringVar(
output,
name,
*output,
"ID",
)
return withFlagOptions(cmd, name)
}
func AddTenantKind(cmd *cobra.Command, output *string) *FlagOptions {
name := "tenant-kind"
cmd.Flags().StringVar(
output,
name,
*output,
"Tenant Kind",
)
return withFlagOptions(cmd, name)
}
func AddTenantID(cmd *cobra.Command, output *string) *FlagOptions {
name := "tenant-id"
cmd.Flags().StringVar(
output,
name,
*output,
"Tenant ID",
)
return withFlagOptions(cmd, name)
}
func AddName(cmd *cobra.Command, output *string) *FlagOptions {
name := "name"
cmd.Flags().StringVar(
output,
name,
*output,
"Name",
)
return withFlagOptions(cmd, name)
}
func AddForce(cmd *cobra.Command, output *bool) *FlagOptions {
name := "force"
cmd.Flags().BoolVarP(
output,
name,
"f",
false,
"Force",
)
return withFlagOptions(cmd, name)
}
func AddUse(cmd *cobra.Command, output *bool) *FlagOptions {
name := "use"
cmd.Flags().BoolVar(
output,
name,
false,
"Use",
)
return withFlagOptions(cmd, name)
}
func AddYes(cmd *cobra.Command, yes *bool) *FlagOptions {
name := "yes"
cmd.Flags().BoolVarP(
yes,
name,
"y",
false,
"Skip confirmation of this action",
)
return withFlagOptions(cmd, name)
}
func AddChannelUpdate(cmd *cobra.Command, output *bool) *FlagOptions {
name := "channel-update"
cmd.Flags().BoolVar(
output,
name,
false,
"Filter deployment with channel updates available",
)
return withFlagOptions(cmd, name)
}
func AddOperatorUpdate(cmd *cobra.Command, output *bool) *FlagOptions {
name := "operator-update"
cmd.Flags().BoolVar(
output,
name,
false,
"Filter deployment with operator updates available",
)
return withFlagOptions(cmd, name)
}
func AddDanglingDeployments(cmd *cobra.Command, output *bool) *FlagOptions {
name := "dangling-deployments"
cmd.Flags().BoolVar(
output,
name,
false,
"Filter not deleted deployment referring to a deleted connector",
)
return withFlagOptions(cmd, name)
}
func AddRevision(cmd *cobra.Command, output *int64) *FlagOptions {
name := "revision"
cmd.Flags().Int64Var(
output,
name,
0,
"Revision to update to. At least one between this and operator-id must be specified.",
)
return withFlagOptions(cmd, name)
}
func AddOperatorId(cmd *cobra.Command, output *string) *FlagOptions {
name := "operator-id"
cmd.Flags().StringVar(
output,
name,
"",
"OperatorId to switch to. At least one between this and revision must be specified.",
)
return withFlagOptions(cmd, name)
}
func AddFile(cmd *cobra.Command, output *string) *FlagOptions {
name := "file"
cmd.Flags().StringVar(
output,
name,
"",
"file",
)
return withFlagOptions(cmd, name)
}
func withFlagOptions(cmd *cobra.Command, flagName string) *FlagOptions {
options := FlagOptions{}
options.Required = func() *FlagOptions {
_ = cmd.MarkFlagRequired(flagName)
return &options
}
options.Complete = func(f func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective)) *FlagOptions {
_ = cmd.RegisterFlagCompletionFunc(flagName, f)
return &options
}
return &options
}
// FlagOptions defines additional flag options
type FlagOptions struct {
Required func() *FlagOptions
Complete func(func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective)) *FlagOptions
}