From 09341cec86b21bd76dbcebb4db4b0e65e1ee9253 Mon Sep 17 00:00:00 2001 From: Cyril David Date: Tue, 23 Mar 2021 16:13:35 -0700 Subject: [PATCH] add apps picker --- internal/cli/apps.go | 52 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/internal/cli/apps.go b/internal/cli/apps.go index 100cf1dc9..318513412 100644 --- a/internal/cli/apps.go +++ b/internal/cli/apps.go @@ -1,6 +1,7 @@ package cli import ( + "errors" "fmt" "strings" @@ -165,9 +166,16 @@ auth0 apps show }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if err := appID.Ask(cmd, &inputs.ID); err != nil { + err := appID.Picker(cmd, &inputs.ID, cli.appPickerOptions) + if err != nil { return err } + + if inputs.ID == "" { + cli.renderer.Infof("There are currently no apps.") + return nil + } + } else { inputs.ID = args[0] } @@ -209,9 +217,16 @@ auth0 apps delete }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if err := appID.Ask(cmd, &inputs.ID); err != nil { + err := appID.Picker(cmd, &inputs.ID, cli.appPickerOptions) + if err != nil { return err } + + if inputs.ID == "" { + cli.renderer.Infof("There are currently no apps.") + return nil + } + } else { inputs.ID = args[0] } @@ -389,11 +404,17 @@ auth0 apps update --name myapp --type [native|spa|regular|m2m] RunE: func(cmd *cobra.Command, args []string) error { var current *management.Client - // Get app id if len(args) == 0 { - if err := appID.Ask(cmd, &inputs.ID); err != nil { + err := appID.Picker(cmd, &inputs.ID, cli.appPickerOptions) + if err != nil { return err } + + if inputs.ID == "" { + cli.renderer.Infof("There are currently no apps.") + return nil + } + } else { inputs.ID = args[0] } @@ -680,3 +701,26 @@ func interfaceToStringSlice(s []interface{}) []string { } return result } + +func (c *cli) appPickerOptions() (pickerOptions, error) { + list, err := c.api.Client.List() + if err != nil { + return nil, err + } + + // NOTE: because client names are not unique, we'll just number these + // labels. + var opts pickerOptions + for _, c := range list.Clients { + value := c.GetClientID() + label := fmt.Sprintf("%s %s", c.GetName(), ansi.Faint("("+value+")")) + + opts = append(opts, pickerOption{value: value, label: label}) + } + + if len(opts) == 0 { + return nil, errors.New("There are currently no applications.") + } + + return opts, nil +}