Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apps, apis, rules: first stab at generalizing picker options [CLI-82] #182

Merged
merged 11 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 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,7 +114,8 @@ 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.Pick(cmd, &inputs.ID, cli.apiPickerOptions)
if err != nil {
return err
}
} else {
Expand Down Expand Up @@ -217,7 +219,8 @@ 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.Pick(cmd, &inputs.ID, cli.apiPickerOptions)
if err != nil {
return err
}
} else {
Expand Down Expand Up @@ -289,7 +292,8 @@ 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.Pick(cmd, &inputs.ID, cli.apiPickerOptions)
if err != nil {
return err
}
} else {
Expand Down Expand Up @@ -333,7 +337,8 @@ 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.Pick(cmd, &inputs.ID, cli.apiPickerOptions)
if err != nil {
return err
}
} else {
Expand Down Expand Up @@ -368,3 +373,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
}
34 changes: 30 additions & 4 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -165,7 +166,8 @@ auth0 apps show <id>
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if err := appID.Ask(cmd, &inputs.ID); err != nil {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
if err != nil {
return err
}
} else {
Expand Down Expand Up @@ -209,7 +211,8 @@ auth0 apps delete <id>
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if err := appID.Ask(cmd, &inputs.ID); err != nil {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
if err != nil {
return err
}
} else {
Expand Down Expand Up @@ -389,9 +392,9 @@ auth0 apps update <id> --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.Pick(cmd, &inputs.ID, cli.appPickerOptions)
if err != nil {
return err
}
} else {
Expand Down Expand Up @@ -680,3 +683,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
}
37 changes: 35 additions & 2 deletions internal/cli/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"

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

Expand Down Expand Up @@ -31,10 +32,42 @@ func (a *Argument) Ask(cmd *cobra.Command, value interface{}) error {
return askArgument(cmd, a, value)
}

type pickerOptionsFunc func() (pickerOptions, error)

func (a *Argument) Pick(cmd *cobra.Command, result *string, fn pickerOptionsFunc) error {
var opts pickerOptions
err := ansi.Waiting(func() error {
var err error
opts, err = fn()
return err
})

if err != nil {
return err
}

defaultLabel := opts.defaultLabel()
var val string
if err := selectArgument(cmd, a, &val, opts.labels(), &defaultLabel); err != nil {
return err
}

*result = opts.getValue(val)
return nil
}

func selectArgument(cmd *cobra.Command, a *Argument, value interface{}, options []string, defaultValue *string) error {
if canPrompt(cmd) {
return _select(cmd, a, value, options, defaultValue, false)
}

cyx marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("Missing a required argument: %s", a.GetName())
}

func askArgument(cmd *cobra.Command, i commandInput, value interface{}) error {
if canPrompt(cmd) {
return ask(cmd, i, value, nil, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return ask(cmd, i, value, nil, true)
return ask(cmd, i, value, nil, false)

} else {
return fmt.Errorf("Missing a required argument: %s", i.GetName())
}

return fmt.Errorf("Missing a required argument: %s", i.GetName())
}
8 changes: 0 additions & 8 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,3 @@ func prepareInteractivity(cmd *cobra.Command) {
})
}
}

func flagOptionsFromMapping(mapping map[string]string) []string {
result := make([]string, 0, len(mapping))
for k := range mapping {
result = append(result, k)
}
return result
}
32 changes: 32 additions & 0 deletions internal/cli/picker_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cli

type pickerOptions []pickerOption
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous iteration using maps had an undesirable behavior that options get randomized on every run. Formalizing this data structure to a list to have a deterministic order -- but also having methods on the list simplifies the call sites.


func (p pickerOptions) labels() []string {
result := make([]string, 0, len(p))
for _, o := range p {
result = append(result, o.label)
}
return result
}

func (p pickerOptions) defaultLabel() string {
if len(p) > 0 {
return p[0].label
}
return ""
}

func (p pickerOptions) getValue(label string) string {
for _, o := range p {
if o.label == label {
return o.value
}
}
return ""
}

type pickerOption struct {
label string
value string
}
Loading