diff --git a/internal/cli/apis.go b/internal/cli/apis.go index 46c21c0d6..2b7c9eb9d 100644 --- a/internal/cli/apis.go +++ b/internal/cli/apis.go @@ -1,7 +1,6 @@ package cli import ( - "errors" "fmt" "github.com/auth0/auth0-cli/internal/ansi" @@ -10,11 +9,12 @@ import ( "gopkg.in/auth0.v5/management" ) -const ( - apiID = "id" -) - var ( + apiID = Argument{ + Name: "Id", + Help: "Id of the API.", + IsRequired: true, + } apiName = Flag{ Name: "Name", LongForm: "name", @@ -116,14 +116,8 @@ auth0 apis show }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if canPrompt(cmd) { - input := prompt.TextInput(apiID, "Id:", "Id of the API.", true) - - if err := prompt.AskOne(input, &inputs); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) - } - } else { - return errors.New("Please include an API Id") + if err := apiID.Ask(cmd, &inputs.ID); err != nil { + return err } } else { inputs.ID = args[0] @@ -232,14 +226,8 @@ auth0 apis update --name myapi }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if canPrompt(cmd) { - input := prompt.TextInput(apiID, "Id:", "Id of the API.", true) - - if err := prompt.AskOne(input, &inputs); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) - } - } else { - return errors.New("Please include an API Id") + if err := apiID.Ask(cmd, &inputs.ID); err != nil { + return err } } else { inputs.ID = args[0] @@ -314,14 +302,8 @@ auth0 apis delete }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if canPrompt(cmd) { - input := prompt.TextInput(apiID, "Id:", "Id of the API.", true) - - if err := prompt.AskOne(input, &inputs); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) - } - } else { - return errors.New("Please include an API Id") + if err := apiID.Ask(cmd, &inputs.ID); err != nil { + return err } } else { inputs.ID = args[0] @@ -364,14 +346,8 @@ auth0 apis scopes list }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if canPrompt(cmd) { - input := prompt.TextInput(apiID, "Id:", "Id of the API.", true) - - if err := prompt.AskOne(input, &inputs); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) - } - } else { - return errors.New("Please include an API Id") + if err := apiID.Ask(cmd, &inputs.ID); err != nil { + return err } } else { inputs.ID = args[0] diff --git a/internal/cli/apps.go b/internal/cli/apps.go index 07e605b08..358f843cb 100644 --- a/internal/cli/apps.go +++ b/internal/cli/apps.go @@ -1,7 +1,6 @@ package cli import ( - "errors" "fmt" "strings" @@ -12,11 +11,12 @@ import ( "gopkg.in/auth0.v5/management" ) -const ( - appID = "id" -) - var ( + appID = Argument{ + Name: "Client ID", + Help: "Id of the application.", + IsRequired: true, + } appName = Flag{ Name: "Name", LongForm: "name", @@ -157,14 +157,8 @@ auth0 apps show }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if canPrompt(cmd) { - input := prompt.TextInput(appID, "Client Id:", "Id of the application.", true) - - if err := prompt.AskOne(input, &inputs); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) - } - } else { - return errors.New("Please provide an application Id") + if err := appID.Ask(cmd, &inputs.ID); err != nil { + return err } } else { inputs.ID = args[0] @@ -209,14 +203,8 @@ auth0 apps delete }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if canPrompt(cmd) { - input := prompt.TextInput(appID, "Client Id:", "Id of the application.", true) - - if err := prompt.AskOne(input, &inputs); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) - } - } else { - return errors.New("Please provide an application Id") + if err := appID.Ask(cmd, &inputs.ID); err != nil { + return err } } else { inputs.ID = args[0] @@ -351,14 +339,8 @@ auth0 apps update --name myapp --type [native|spa|regular|m2m] }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if canPrompt(cmd) { - input := prompt.TextInput(appID, "Client Id:", "Id of the application.", true) - - if err := prompt.AskOne(input, &inputs); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) - } - } else { - return errors.New("Please provide an application Id") + if err := appID.Ask(cmd, &inputs.ID); err != nil { + return err } } else { inputs.ID = args[0] diff --git a/internal/cli/arguments.go b/internal/cli/arguments.go new file mode 100644 index 000000000..836c28d43 --- /dev/null +++ b/internal/cli/arguments.go @@ -0,0 +1,41 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +type Argument struct { + Name string + Help string + IsRequired bool +} + +func (a Argument) GetName() string { + return a.Name +} + +func (a Argument) GetLabel() string { + return inputLabel(a.Name) +} + +func (a Argument) GetHelp() string { + return a.Help +} + +func (a Argument) GetIsRequired() bool { + return a.IsRequired +} + +func (a *Argument) Ask(cmd *cobra.Command, value interface{}) error { + return askArgument(cmd, a, value) +} + +func askArgument(cmd *cobra.Command, i commandInput, value interface{}) error { + if canPrompt(cmd) { + return ask(cmd, i, value, true) + } else { + return fmt.Errorf("Missing a required argument: %s", i.GetName()) + } +} diff --git a/internal/cli/flags.go b/internal/cli/flags.go index 0d41089b0..162219f9f 100644 --- a/internal/cli/flags.go +++ b/internal/cli/flags.go @@ -3,7 +3,6 @@ package cli import ( "fmt" - "github.com/auth0/auth0-cli/internal/prompt" "github.com/spf13/cobra" ) @@ -15,20 +14,36 @@ type Flag struct { IsRequired 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) Ask(cmd *cobra.Command, value interface{}) error { - return askInput(cmd, f, value, false) + return askFlag(cmd, f, value, false) } func (f *Flag) AskU(cmd *cobra.Command, value interface{}) error { - return askInput(cmd, f, value, true) + return askFlag(cmd, f, value, true) } func (f *Flag) Select(cmd *cobra.Command, value interface{}, options []string) error { - return selectInput(cmd, f, value, options, false) + return selectFlag(cmd, f, value, options, false) } func (f *Flag) SelectU(cmd *cobra.Command, value interface{}, options []string) error { - return selectInput(cmd, f, value, options, true) + return selectFlag(cmd, f, value, options, true) } func (f *Flag) RegisterString(cmd *cobra.Command, value *string, defaultValue string) { @@ -51,6 +66,10 @@ 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) } @@ -59,36 +78,28 @@ func (f *Flag) RegisterBoolU(cmd *cobra.Command, value *bool, defaultValue bool) registerBool(cmd, f, value, defaultValue, true) } -func (f *Flag) RegisterIntU(cmd *cobra.Command, value *int, defaultValue int) { - registerInt(cmd, f, value, defaultValue, true) -} +func askFlag(cmd *cobra.Command, f *Flag, value interface{}, isUpdate bool) error { + if shouldAsk(cmd, f, isUpdate) { + return ask(cmd, f, value, isUpdate) + } -func (f *Flag) label() string { - return fmt.Sprintf("%s:", f.Name) + return nil } -func askInput(cmd *cobra.Command, f *Flag, value interface{}, isUpdate bool) error { +func selectFlag(cmd *cobra.Command, f *Flag, value interface{}, options []string, isUpdate bool) error { if shouldAsk(cmd, f, isUpdate) { - input := prompt.TextInput("", f.label(), f.Help, f.IsRequired) - - if err := prompt.AskOne(input, value); err != nil { - return unexpectedError(err) - } + return _select(cmd, f, value, options, isUpdate) } return nil } -func selectInput(cmd *cobra.Command, f *Flag, value interface{}, options []string, isUpdate bool) error { - if shouldAsk(cmd, f, isUpdate) { - input := prompt.SelectInput("", f.label(), f.Help, options, f.IsRequired) - - if err := prompt.AskOne(input, value); err != nil { - return unexpectedError(err) - } +func shouldAsk(cmd *cobra.Command, f *Flag, isUpdate bool) bool { + if isUpdate { + return shouldPromptWhenFlagless(cmd, f.LongForm) } - return nil + return shouldPrompt(cmd, f.LongForm) } func registerString(cmd *cobra.Command, f *Flag, value *string, defaultValue string, isUpdate bool) { @@ -123,14 +134,6 @@ func registerBool(cmd *cobra.Command, f *Flag, value *bool, defaultValue bool, i } } -func shouldAsk(cmd *cobra.Command, f *Flag, isUpdate bool) bool { - if isUpdate { - return shouldPromptWhenFlagless(cmd, f.LongForm) - } - - return shouldPrompt(cmd, f.LongForm) -} - func markFlagRequired(cmd *cobra.Command, f *Flag, isUpdate bool) error { if f.IsRequired && !isUpdate { return cmd.MarkFlagRequired(f.LongForm) diff --git a/internal/cli/input.go b/internal/cli/input.go new file mode 100644 index 000000000..469bb14f6 --- /dev/null +++ b/internal/cli/input.go @@ -0,0 +1,41 @@ +package cli + +import ( + "fmt" + + "github.com/auth0/auth0-cli/internal/prompt" + "github.com/spf13/cobra" +) + +type commandInput interface { + GetName() string + GetLabel() string + GetHelp() string + GetIsRequired() bool +} + +func ask(cmd *cobra.Command, i commandInput, value interface{}, isUpdate bool) error { + isRequired := !isUpdate && i.GetIsRequired() + input := prompt.TextInput("", i.GetLabel(), i.GetHelp(), isRequired) + + if err := prompt.AskOne(input, value); err != nil { + return unexpectedError(err) + } + + return nil +} + +func _select(cmd *cobra.Command, i commandInput, value interface{}, options []string, isUpdate bool) error { + isRequired := !isUpdate && i.GetIsRequired() + input := prompt.SelectInput("", i.GetLabel(), i.GetHelp(), options, isRequired) + + if err := prompt.AskOne(input, value); err != nil { + return unexpectedError(err) + } + + return nil +} + +func inputLabel(name string) string { + return fmt.Sprintf("%s:", name) +}