From f6ef9a8ac23ebc902078787742b72ec78631e3d7 Mon Sep 17 00:00:00 2001 From: Cyril David Date: Tue, 23 Mar 2021 16:32:23 -0700 Subject: [PATCH] Add APIs picker --- internal/cli/apis.go | 59 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/internal/cli/apis.go b/internal/cli/apis.go index 122818545..5a04f23f4 100644 --- a/internal/cli/apis.go +++ b/internal/cli/apis.go @@ -1,6 +1,7 @@ package cli import ( + "errors" "fmt" "github.com/auth0/auth0-cli/internal/ansi" @@ -113,9 +114,16 @@ auth0 apis show }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if err := apiID.Ask(cmd, &inputs.ID); err != nil { + err := apiID.Picker(cmd, &inputs.ID, cli.apiPickerOptions) + if err != nil { return err } + + if inputs.ID == "" { + cli.renderer.Infof("There are currently no APIs.") + return nil + } + } else { inputs.ID = args[0] } @@ -217,9 +225,16 @@ auth0 apis update --name myapi var current *management.ResourceServer if len(args) == 0 { - if err := apiID.Ask(cmd, &inputs.ID); err != nil { + err := apiID.Picker(cmd, &inputs.ID, cli.apiPickerOptions) + if err != nil { return err } + + if inputs.ID == "" { + cli.renderer.Infof("There are currently no APIs.") + return nil + } + } else { inputs.ID = args[0] } @@ -289,9 +304,16 @@ auth0 apis delete }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if err := apiID.Ask(cmd, &inputs.ID); err != nil { + err := apiID.Picker(cmd, &inputs.ID, cli.apiPickerOptions) + if err != nil { return err } + + if inputs.ID == "" { + cli.renderer.Infof("There are currently no APIs.") + return nil + } + } else { inputs.ID = args[0] } @@ -333,9 +355,16 @@ auth0 apis scopes list }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - if err := apiID.Ask(cmd, &inputs.ID); err != nil { + err := apiID.Picker(cmd, &inputs.ID, cli.apiPickerOptions) + if err != nil { return err } + + if inputs.ID == "" { + cli.renderer.Infof("There are currently no APIs.") + return nil + } + } else { inputs.ID = args[0] } @@ -368,3 +397,25 @@ func apiScopesFor(scopes []string) []*management.ResourceServerScope { return models } + +func (c *cli) apiPickerOptions() (pickerOptions, error) { + list, err := c.api.ResourceServer.List() + if err != nil { + return nil, err + } + + // NOTE: because client names are not unique, we'll just number these + // labels. + var opts pickerOptions + for _, r := range list.ResourceServers { + label := fmt.Sprintf("%s %s", r.GetName(), ansi.Faint("("+r.GetIdentifier()+")")) + + opts = append(opts, pickerOption{value: r.GetID(), label: label}) + } + + if len(opts) == 0 { + return nil, errors.New("There are currently no applications.") + } + + return opts, nil +}