Skip to content

Commit

Permalink
Merge pull request #72 from auth0/add/update-rule
Browse files Browse the repository at this point in the history
[A0CLI-54] feat: Adding the update rule subcommand
  • Loading branch information
Alexis Kulash authored Jan 27, 2021
2 parents a801220 + f522529 commit 0aa048a
Showing 1 changed file with 95 additions and 0 deletions.
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)")
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

0 comments on commit 0aa048a

Please sign in to comment.