Skip to content

Commit

Permalink
feat: add name flag support for rules delete command (#54)
Browse files Browse the repository at this point in the history
- ensure id and name flags are mutually exclusive
- added rule id pattern check
- fixed bug that didn't report not found rule when id was used
- added global force flag to skip confirmation prompt

Close A0CLI-40
  • Loading branch information
vprasanth authored Jan 26, 2021
1 parent 3119474 commit 1a9e75c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type cli struct {
verbose bool
tenant string
format string
force bool

// config state management.
initOnce sync.Once
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func Execute() {
rootCmd.PersistentFlags().StringVar(&cli.format,
"format", "", "Command output format. Options: json.")

rootCmd.PersistentFlags().BoolVarP(&cli.force,
"force", "f", false, "Skip confirmation.")

rootCmd.AddCommand(loginCmd(cli))
rootCmd.AddCommand(clientsCmd(cli))
rootCmd.AddCommand(apisCmd(cli))
Expand Down
49 changes: 42 additions & 7 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"regexp"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
Expand Down Expand Up @@ -191,7 +192,9 @@ func createRulesCmd(cli *cli) *cobra.Command {

func deleteRulesCmd(cli *cli) *cobra.Command {
var flags struct {
id string
id string
name string
confirm bool
}

cmd := &cobra.Command{
Expand All @@ -200,12 +203,44 @@ func deleteRulesCmd(cli *cli) *cobra.Command {
Long: `Delete a rule:
auth0 rules delete --id "12345"`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if flags.id != "" && flags.name != "" {
return fmt.Errorf("TMI! 🤯 use either --name or --id")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
r := &management.Rule{ID: &flags.id}
var r *management.Rule
ruleIDPattern := "^rul_[A-Za-z0-9]{16}$"
re := regexp.MustCompile(ruleIDPattern)

// TODO: Should add validation of rule
if confirmed := prompt.Confirm("Are you sure you want to proceed?"); !confirmed {
return nil
if flags.id != "" {
if !re.Match([]byte(flags.id)) {
return fmt.Errorf("Rule with id %q does not match pattern %s", flags.id, ruleIDPattern)
}

rule, err := cli.api.Rule.Read(flags.id)
if err != nil {
return err
}
r = rule
} else {
data, err := getRules(cli)
if err != nil {
return err
}
if rule := findRuleByName(flags.name, data.Rules); rule != nil {
r = rule
} else {
return fmt.Errorf("No rule found with name: %q", flags.name)
}
}

if !cli.force {
// TODO: Should add validation of rule
if confirmed := prompt.Confirm("Are you sure you want to proceed?"); !confirmed {
return nil
}
}

err := ansi.Spinner("Deleting rule", func() error {
Expand All @@ -220,8 +255,8 @@ func deleteRulesCmd(cli *cli) *cobra.Command {
},
}

cmd.Flags().StringVar(&flags.id, "id", "", "ID of the rule to delete (required)")
mustRequireFlags(cmd, "id")
cmd.Flags().StringVar(&flags.id, "id", "", "ID of the rule to delete")
cmd.Flags().StringVar(&flags.name, "name", "", "Name of the rule to delete")

return cmd
}
Expand Down

0 comments on commit 1a9e75c

Please sign in to comment.