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 1 commit
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
27 changes: 27 additions & 0 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,6 +32,32 @@ func (a *Argument) Ask(cmd *cobra.Command, value interface{}) error {
return askArgument(cmd, a, value)
}

type pickerOptionsFunc func() (pickerOptions, error)

func (a *Argument) Picker(cmd *cobra.Command, result *string, fn pickerOptionsFunc) error {
cyx marked this conversation as resolved.
Show resolved Hide resolved
var opts pickerOptions
err := ansi.Waiting(func() error {
var err error
opts, err = fn()
return err
})

if err != nil {
return err
}

// TODO(cyx): Fix this up. For now everything depends on `flag`.
cyx marked this conversation as resolved.
Show resolved Hide resolved
f := Flag{Name: a.Name}
defaultLabel := opts.defaultLabel()
var val string
if err := selectFlag(cmd, &f, &val, opts.labels(), &defaultLabel, false); err != nil {
return err
}

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

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)

Expand Down
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
}
75 changes: 27 additions & 48 deletions internal/cli/rules.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 All @@ -11,6 +12,11 @@ import (
)

var (
ruleID = Argument{
Name: "Rule ID",
Help: "Id of the rule.",
}

ruleName = Flag{
Name: "Name",
LongForm: "name",
Expand All @@ -26,21 +32,19 @@ var (
Help: "Template to use for the rule.",
}

ruleTemplateOptions = flagOptionsFromMapping(ruleTemplateMappings)

ruleEnabled = Flag{
Name: "Enabled",
LongForm: "enabled",
ShortForm: "e",
Help: "Enable (or disable) a rule.",
}

ruleTemplateMappings = map[string]string{
"Empty rule": ruleTemplateEmptyRule,
"Add email to access token": ruleTemplateAddEmailToAccessToken,
"Check last password reset": ruleTemplateCheckLastPasswordReset,
"IP address allow list": ruleTemplateIPAddressAllowList,
"IP address deny list": ruleTemplateIPAddressDenyList,
ruleTemplateOptions = pickerOptions{
{"Empty rule", ruleTemplateEmptyRule},
{"Add email to access token", ruleTemplateAddEmailToAccessToken},
{"Check last password reset", ruleTemplateCheckLastPasswordReset},
{"IP address allow list", ruleTemplateIPAddressAllowList},
{"IP address deny list", ruleTemplateIPAddressDenyList},
}
)

Expand Down Expand Up @@ -110,15 +114,15 @@ auth0 rules create --name "My Rule" --template [empty-rule]"
return err
}

if err := ruleTemplate.Select(cmd, &inputs.Template, ruleTemplateOptions, nil); err != nil {
if err := ruleTemplate.Select(cmd, &inputs.Template, ruleTemplateOptions.labels(), nil); err != nil {
return err
}

// TODO(cyx): we can re-think this once we have
// `--stdin` based commands. For now we don't have
// those yet, so keeping this simple.
script, err := prompt.CaptureInputViaEditor(
ruleTemplateMappings[inputs.Template],
ruleTemplateOptions.getValue(inputs.Template),
inputs.Name+".*.js",
)
if err != nil {
Expand Down Expand Up @@ -171,10 +175,7 @@ auth0 rules show <id>
if len(args) > 0 {
inputs.ID = args[0]
} else {
// TODO(cyx): Consider making a primitive for
// Argument to ask using a provided func.
var err error
inputs.ID, err = promptForRuleViaDropdown(cli, cmd)
err := ruleID.Picker(cmd, &inputs.ID, cli.rulePickerOptions)
if err != nil {
return err
}
Expand Down Expand Up @@ -223,10 +224,7 @@ auth0 rules delete rul_d2VSaGlyaW5n`,
if len(args) > 0 {
inputs.ID = args[0]
} else {
// TODO(cyx): Consider making a primitive for
// Argument to ask using a provided func.
var err error
inputs.ID, err = promptForRuleViaDropdown(cli, cmd)
err := ruleID.Picker(cmd, &inputs.ID, cli.rulePickerOptions)
if err != nil {
return err
}
Expand Down Expand Up @@ -273,10 +271,7 @@ auth0 rules update --id rul_d2VSaGlyaW5n --name "My Updated Rule" --enabled=fal
if len(args) > 0 {
inputs.ID = args[0]
} else {
// TODO(cyx): Consider making a primitive for
// Argument to ask using a provided func.
var err error
inputs.ID, err = promptForRuleViaDropdown(cli, cmd)
err := ruleID.Picker(cmd, &inputs.ID, cli.rulePickerOptions)
if err != nil {
return err
}
Expand Down Expand Up @@ -345,36 +340,20 @@ auth0 rules update --id rul_d2VSaGlyaW5n --name "My Updated Rule" --enabled=fal
return cmd
}

func promptForRuleViaDropdown(cli *cli, cmd *cobra.Command) (id string, err error) {
dropdown := Flag{Name: "Rule"}

var rules []*management.Rule

// == Start experimental dropdown for names => id.
// TODO(cyx): Consider extracting this
// pattern once we've done more of it.
err = ansi.Waiting(func() error {
list, err := cli.api.Rule.List()
if err != nil {
return err
}
rules = list.Rules
return nil
})

if err != nil || len(rules) == 0 {
return "", err
func (c *cli) rulePickerOptions() (pickerOptions, error) {
list, err := c.api.Rule.List()
if err != nil {
return nil, err
}

mapping := map[string]string{}
for _, r := range rules {
mapping[r.GetName()] = r.GetID()
var opts pickerOptions
for _, r := range list.Rules {
opts = append(opts, pickerOption{value: r.GetID(), label: r.GetName()})
}

var name string
if err := dropdown.Select(cmd, &name, flagOptionsFromMapping(mapping), nil); err != nil {
return "", err
if len(opts) == 0 {
return nil, errors.New("There are currently no rules.")
}

return mapping[name], nil
return opts, nil
}