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

feat: add name flag support for rules delete command #54

Merged
merged 2 commits into from
Jan 26, 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
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")
Copy link
Contributor

Choose a reason for hiding this comment

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

🍭

}
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)
Comment on lines +214 to +215
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a big deal since this is a CLI, but typically you put MustCompile lines in vars outside so you don't have to re-do it.

(We can keep it like this for this PR though, just wanted to share that compiling regexp is typically expensive if you're doing server side code).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that makes sense, good to know, ty!


// 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