-
Notifications
You must be signed in to change notification settings - Fork 55
/
flags.go
50 lines (39 loc) · 1.1 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
package cli
import (
"fmt"
"github.com/auth0/auth0-cli/internal/prompt"
"github.com/spf13/cobra"
)
type Flag struct {
Name string
LongForm string
ShortForm string
DefaultValue string
Help string
IsRequired bool
IsInteractive bool
}
func (f *Flag) Ask(cmd *cobra.Command, value interface{}) error {
return ask(cmd, f, value, false)
}
func (f *Flag) AskUpdate(cmd *cobra.Command, value interface{}) error {
return ask(cmd, f, value, true)
}
func (f *Flag) RegisterString(cmd *cobra.Command, value *string) {
cmd.Flags().StringVarP(value, f.LongForm, f.ShortForm, f.DefaultValue, f.Help)
}
func ask(cmd *cobra.Command, f *Flag, value interface{}, isUpdate bool) error {
var shouldAsk bool
if isUpdate {
shouldAsk = shouldPromptWhenFlagless(cmd, f.LongForm)
} else {
shouldAsk = shouldPrompt(cmd, f.LongForm)
}
if f.IsInteractive && shouldAsk {
input := prompt.TextInput("", fmt.Sprintf("%s:", f.Name), f.Help, f.IsRequired)
if err := prompt.AskOne(input, value); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
}
return nil
}