From 8eeb7fa34e492c9b8e6db04e95ea63f72eb0e012 Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Tue, 26 Jan 2021 17:35:04 -0500 Subject: [PATCH 1/2] feat: add --name flag support for rules delete command - 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 Close A0CLI-40 --- internal/cli/cli.go | 1 + internal/cli/root.go | 3 +++ internal/cli/rules.go | 49 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 96d3710f0..344b2f99f 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -57,6 +57,7 @@ type cli struct { verbose bool tenant string format string + force bool // config state management. initOnce sync.Once diff --git a/internal/cli/root.go b/internal/cli/root.go index 5d30cff5d..07653f2a1 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -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)) diff --git a/internal/cli/rules.go b/internal/cli/rules.go index aee45fbb6..c5f14a6bd 100644 --- a/internal/cli/rules.go +++ b/internal/cli/rules.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "regexp" "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/auth0" @@ -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{ @@ -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)) == false { + 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 { @@ -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 } From 60b0537f5acc808abab34988e947c7da15afdfe8 Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Tue, 26 Jan 2021 17:44:03 -0500 Subject: [PATCH 2/2] chore: fix linter error --- internal/cli/rules.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/rules.go b/internal/cli/rules.go index c5f14a6bd..2ea65709d 100644 --- a/internal/cli/rules.go +++ b/internal/cli/rules.go @@ -215,7 +215,7 @@ func deleteRulesCmd(cli *cli) *cobra.Command { re := regexp.MustCompile(ruleIDPattern) if flags.id != "" { - if re.Match([]byte(flags.id)) == false { + if !re.Match([]byte(flags.id)) { return fmt.Errorf("Rule with id %q does not match pattern %s", flags.id, ruleIDPattern) }