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

[A0CLI-54] feat: Adding the update rule subcommand #72

Merged
merged 1 commit into from
Jan 27, 2021
Merged
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
95 changes: 95 additions & 0 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func rulesCmd(cli *cli) *cobra.Command {
cmd.AddCommand(disableRuleCmd(cli))
cmd.AddCommand(createRulesCmd(cli))
cmd.AddCommand(deleteRulesCmd(cli))
cmd.AddCommand(updateRulesCmd(cli))

return cmd
}
Expand Down Expand Up @@ -354,6 +355,100 @@ func deleteRulesCmd(cli *cli) *cobra.Command {
return cmd
}

func updateRulesCmd(cli *cli) *cobra.Command {
var flags struct {
ID string
Name string
Script string
Order int
Enabled bool
}

cmd := &cobra.Command{
Use: "update",
Short: "update a rule",
Long: `Update a rule:

auth0 rules update --id "12345" --name "My Updated Rule" --script "function (user, context, callback) { console.log( 'Hello, world!' ); return callback(null, user, context); }" --order 1 --enabled true
`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, ruleID) {
input := prompt.TextInput(ruleID, "Id:", "Id of the rule.", "", true)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

if shouldPrompt(cmd, ruleName) {
input := prompt.TextInput(
"name", "Name:",
"Name of the rule. You can change the rule name later in the rule settings.",
"",
true)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

if shouldPrompt(cmd, ruleScript) {
input := prompt.TextInput(ruleScript, "Script:", "Script of the rule.", "", true)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

if shouldPrompt(cmd, ruleOrder) {
input := prompt.TextInput(ruleOrder, "Order:", "Order of the rule.", "0", false)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

if shouldPrompt(cmd, ruleEnabled) {
input := prompt.BoolInput(ruleEnabled, "Enabled:", "Enable the rule.", false)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

r := &management.Rule{
Name: &flags.Name,
Script: &flags.Script,
Order: &flags.Order,
Enabled: &flags.Enabled,
}

err := ansi.Spinner("Updating rule", func() error {
return cli.api.Rule.Update(flags.ID, r)
})

if err != nil {
return err
}

cli.renderer.Infof("Your rule `%s` was successfully updated.", flags.Name)
return nil
},
}

cmd.Flags().StringVarP(&flags.ID, ruleID, "i", "", "ID of the rule to update (required)")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can probably skip the i here, but keep n, s, o, e.

Copy link
Author

@alexiskulash alexiskulash Jan 27, 2021

Choose a reason for hiding this comment

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

I left that as i in order to standardize how this was done on L352 -- I'm up for removing the i, but might need to do it in a few spots across the board. Might leave that out of this PR for now, then take a broad sweep later?

cmd.Flags().StringVarP(&flags.Name, ruleName, "n", "", "Name of this rule")
cmd.Flags().StringVarP(&flags.Script, ruleScript, "s", "", "Code to be executed when this rule runs")
cmd.Flags().IntVarP(&flags.Order, ruleOrder, "o", 0, "Order that this rule should execute in relative to other rules. Lower-valued rules execute first.")
cmd.Flags().BoolVarP(&flags.Enabled, ruleEnabled, "e", false, "Whether the rule is enabled (true), or disabled (false).")
mustRequireFlags(cmd, ruleID)

return cmd
}

// @TODO move to rules package
func getRules(cli *cli) (list *management.RuleList, err error) {
return cli.api.Rule.List()
Expand Down