Skip to content

Commit

Permalink
Refactor arguments in apps and apis commands [CLI-78] (#177)
Browse files Browse the repository at this point in the history
* Refactor arguments in apps and apis commands

* Add missing return statement

* Rename method

* Make commandInput private
  • Loading branch information
Widcket authored Mar 20, 2021
1 parent 581b9de commit 96b6be4
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 98 deletions.
50 changes: 13 additions & 37 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"errors"
"fmt"

"github.com/auth0/auth0-cli/internal/ansi"
Expand All @@ -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",
Expand Down Expand Up @@ -116,14 +116,8 @@ auth0 apis show <id>
},
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]
Expand Down Expand Up @@ -232,14 +226,8 @@ auth0 apis update <id> --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]
Expand Down Expand Up @@ -314,14 +302,8 @@ auth0 apis delete <id>
},
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]
Expand Down Expand Up @@ -364,14 +346,8 @@ auth0 apis scopes list <id>
},
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]
Expand Down
40 changes: 11 additions & 29 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"errors"
"fmt"
"strings"

Expand All @@ -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",
Expand Down Expand Up @@ -157,14 +157,8 @@ auth0 apps show <id>
},
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]
Expand Down Expand Up @@ -209,14 +203,8 @@ auth0 apps delete <id>
},
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]
Expand Down Expand Up @@ -351,14 +339,8 @@ auth0 apps update <id> --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]
Expand Down
41 changes: 41 additions & 0 deletions internal/cli/arguments.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
67 changes: 35 additions & 32 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"fmt"

"github.com/auth0/auth0-cli/internal/prompt"
"github.com/spf13/cobra"
)

Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions internal/cli/input.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 96b6be4

Please sign in to comment.