-
Notifications
You must be signed in to change notification settings - Fork 55
/
flags.go
303 lines (232 loc) · 8.94 KB
/
flags.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
package cli
import (
"fmt"
"github.com/spf13/cobra"
"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
)
type Flag struct {
Name string
LongForm string
ShortForm string
Help string
IsRequired bool
AlwaysPrompt bool
}
func (f Flag) GetName() string {
return f.Name
}
func (f Flag) GetLabel() string {
return inputLabel(f.Name)
}
func (f Flag) GetHelp() string {
return f.Help
}
func (f Flag) GetIsRequired() bool {
return f.IsRequired
}
func (f *Flag) IsSet(cmd *cobra.Command) bool {
return cmd.Flags().Changed(f.LongForm)
}
func (f *Flag) Ask(cmd *cobra.Command, value interface{}, defaultValue *string) error {
return askFlag(cmd, f, value, defaultValue, false)
}
func (f *Flag) AskU(cmd *cobra.Command, value interface{}, defaultValue *string) error {
return askFlag(cmd, f, value, defaultValue, true)
}
func (f *Flag) AskMany(cmd *cobra.Command, value interface{}, defaultValue *string) error {
return askManyFlag(cmd, f, value, defaultValue, false)
}
func (f *Flag) AskManyU(cmd *cobra.Command, value interface{}, defaultValue *string) error {
return askManyFlag(cmd, f, value, defaultValue, true)
}
func (f *Flag) AskBool(cmd *cobra.Command, value *bool, defaultValue *bool) error {
return askBoolFlag(cmd, f, value, defaultValue, false)
}
func (f *Flag) AskBoolU(cmd *cobra.Command, value *bool, defaultValue *bool) error {
return askBoolFlag(cmd, f, value, defaultValue, true)
}
func (f *Flag) AskInt(cmd *cobra.Command, value *int, defaultValue *string) error {
return askIntFlag(cmd, f, value, defaultValue, false)
}
func (f *Flag) AskIntU(cmd *cobra.Command, value *int, defaultValue *string) error {
return askIntFlag(cmd, f, value, defaultValue, true)
}
func (f *Flag) Select(cmd *cobra.Command, value interface{}, options []string, defaultValue *string) error {
return selectFlag(cmd, f, value, options, defaultValue, false)
}
func (f *Flag) SelectU(cmd *cobra.Command, value interface{}, options []string, defaultValue *string) error {
return selectFlag(cmd, f, value, options, defaultValue, true)
}
func (f *Flag) Pick(cmd *cobra.Command, result *string, fn pickerOptionsFunc) error {
return pickFlag(cmd, f, result, fn, false)
}
func (f *Flag) PickU(cmd *cobra.Command, result *string, fn pickerOptionsFunc) error {
return pickFlag(cmd, f, result, fn, true)
}
func (f *Flag) OpenEditor(cmd *cobra.Command, value *string, defaultValue, filename string, infoFn func()) error {
return openEditorFlag(cmd, f, value, defaultValue, filename, infoFn, nil, false)
}
func (f *Flag) OpenEditorW(cmd *cobra.Command, value *string, defaultValue, filename string, infoFn func(), tempFileFn func(string)) error {
return openEditorFlag(cmd, f, value, defaultValue, filename, infoFn, tempFileFn, false)
}
func (f *Flag) OpenEditorU(cmd *cobra.Command, value *string, defaultValue string, filename string) error {
return openEditorFlag(cmd, f, value, defaultValue, filename, nil, nil, true)
}
func (f *Flag) AskPassword(cmd *cobra.Command, value *string) error {
return askPasswordFlag(cmd, f, value, false)
}
func (f *Flag) AskPasswordU(cmd *cobra.Command, value *string) error {
return askPasswordFlag(cmd, f, value, true)
}
func (f *Flag) RegisterString(cmd *cobra.Command, value *string, defaultValue string) {
registerString(cmd, f, value, defaultValue, false)
}
func (f *Flag) RegisterStringU(cmd *cobra.Command, value *string, defaultValue string) {
registerString(cmd, f, value, defaultValue, true)
}
func (f *Flag) RegisterStringSlice(cmd *cobra.Command, value *[]string, defaultValue []string) {
registerStringSlice(cmd, f, value, defaultValue, false)
}
func (f *Flag) RegisterStringSliceU(cmd *cobra.Command, value *[]string, defaultValue []string) {
registerStringSlice(cmd, f, value, defaultValue, true)
}
func (f *Flag) RegisterStringMap(cmd *cobra.Command, value *map[string]string, defaultValue map[string]string) {
registerStringMap(cmd, f, value, defaultValue, false)
}
func (f *Flag) RegisterStringMapU(cmd *cobra.Command, value *map[string]string, defaultValue map[string]string) {
registerStringMap(cmd, f, value, defaultValue, true)
}
func (f *Flag) RegisterInt(cmd *cobra.Command, value *int, defaultValue int) {
registerInt(cmd, f, value, defaultValue, false)
}
func (f *Flag) RegisterIntU(cmd *cobra.Command, value *int, defaultValue int) {
registerInt(cmd, f, value, defaultValue, true)
}
func (f *Flag) RegisterBool(cmd *cobra.Command, value *bool, defaultValue bool) {
registerBool(cmd, f, value, defaultValue, false)
}
func (f *Flag) RegisterBoolU(cmd *cobra.Command, value *bool, defaultValue bool) {
registerBool(cmd, f, value, defaultValue, true)
}
func askFlag(cmd *cobra.Command, f *Flag, value interface{}, defaultValue *string, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
return ask(f, value, defaultValue, isUpdate)
}
return nil
}
func askManyFlag(cmd *cobra.Command, f *Flag, value interface{}, defaultValue *string, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
var strInput string
if err := ask(f, &strInput, defaultValue, isUpdate); err != nil {
return err
}
*value.(*[]string) = commaSeparatedStringToSlice(strInput)
}
return nil
}
func askBoolFlag(cmd *cobra.Command, f *Flag, value *bool, defaultValue *bool, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
if err := askBool(f, value, defaultValue); err != nil {
return err
}
}
return nil
}
func askIntFlag(cmd *cobra.Command, f *Flag, value *int, defaultValue *string, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
return askInt(f, value, defaultValue, isUpdate)
}
return nil
}
func selectFlag(cmd *cobra.Command, f *Flag, value interface{}, options []string, defaultValue *string, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
return _select(f, value, options, defaultValue, isUpdate)
}
return nil
}
func pickFlag(cmd *cobra.Command, f *Flag, result *string, fn pickerOptionsFunc, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
var opts pickerOptions
err := ansi.Waiting(func() error {
var err error
opts, err = fn(cmd.Context())
return err
})
if err != nil {
return err
}
defaultLabel := opts.defaultLabel()
var val string
if err := selectFlag(cmd, f, &val, opts.labels(), &defaultLabel, isUpdate); err != nil {
return err
}
*result = opts.getValue(val)
}
return nil
}
func askPasswordFlag(cmd *cobra.Command, f *Flag, value *string, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
if err := askPassword(f, value, isUpdate); err != nil {
return err
}
}
return nil
}
func openEditorFlag(cmd *cobra.Command, f *Flag, value *string, defaultValue string, filename string, infoFn func(), tempFileFn func(string), isUpdate bool) error {
if shouldAsk(cmd, f, false) { // Always open the editor on update.
if isUpdate {
return openUpdateEditor(f, value, defaultValue, filename)
}
return openCreateEditor(value, defaultValue, filename, infoFn, tempFileFn)
}
return nil
}
func registerString(cmd *cobra.Command, f *Flag, value *string, defaultValue string, isUpdate bool) {
cmd.Flags().StringVarP(value, f.LongForm, f.ShortForm, defaultValue, f.Help)
if err := markFlagRequired(cmd, f, isUpdate); err != nil {
panic(auth0.Error(err, "failed to register string flag"))
}
}
func registerStringSlice(cmd *cobra.Command, f *Flag, value *[]string, defaultValue []string, isUpdate bool) {
cmd.Flags().StringSliceVarP(value, f.LongForm, f.ShortForm, defaultValue, f.Help)
if err := markFlagRequired(cmd, f, isUpdate); err != nil {
panic(auth0.Error(err, "failed to register string slice flag"))
}
}
func registerStringMap(cmd *cobra.Command, f *Flag, value *map[string]string, defaultValue map[string]string, isUpdate bool) {
cmd.Flags().StringToStringVarP(value, f.LongForm, f.ShortForm, defaultValue, f.Help)
if err := markFlagRequired(cmd, f, isUpdate); err != nil {
panic(auth0.Error(err, "failed to register string map flag"))
}
}
func registerInt(cmd *cobra.Command, f *Flag, value *int, defaultValue int, isUpdate bool) {
cmd.Flags().IntVarP(value, f.LongForm, f.ShortForm, defaultValue, f.Help)
if err := markFlagRequired(cmd, f, isUpdate); err != nil {
panic(auth0.Error(err, "failed to register int flag"))
}
}
func registerBool(cmd *cobra.Command, f *Flag, value *bool, defaultValue bool, isUpdate bool) {
cmd.Flags().BoolVarP(value, f.LongForm, f.ShortForm, defaultValue, f.Help)
if err := markFlagRequired(cmd, f, isUpdate); err != nil {
panic(auth0.Error(err, "failed to register bool flag"))
}
}
func shouldAsk(cmd *cobra.Command, f *Flag, isUpdate bool) bool {
if isUpdate {
if !f.IsRequired && !f.AlwaysPrompt {
return false
}
return shouldPromptWhenNoLocalFlagsSet(cmd)
}
return canPrompt(cmd) && !f.IsSet(cmd)
}
func markFlagRequired(cmd *cobra.Command, f *Flag, isUpdate bool) error {
if f.IsRequired && !isUpdate {
return cmd.MarkFlagRequired(f.LongForm)
}
return nil
}
func unexpectedError(err error) error {
return fmt.Errorf("An unexpected error occurred: %w", err)
}