Skip to content

Commit

Permalink
Add APIs picker
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Mar 23, 2021
1 parent 09341ce commit f6ef9a8
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"fmt"

"github.com/auth0/auth0-cli/internal/ansi"
Expand Down Expand Up @@ -113,9 +114,16 @@ auth0 apis show <id>
},
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]
}
Expand Down Expand Up @@ -217,9 +225,16 @@ auth0 apis update <id> --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]
}
Expand Down Expand Up @@ -289,9 +304,16 @@ auth0 apis delete <id>
},
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]
}
Expand Down Expand Up @@ -333,9 +355,16 @@ auth0 apis scopes list <id>
},
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]
}
Expand Down Expand Up @@ -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
}

0 comments on commit f6ef9a8

Please sign in to comment.