Skip to content

Commit

Permalink
[CLI-138] add enable and disable rules commands (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
bright-poku authored Apr 23, 2021
1 parent ba0d855 commit e5b542e
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
112 changes: 112 additions & 0 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func rulesCmd(cli *cli) *cobra.Command {
cmd.AddCommand(showRuleCmd(cli))
cmd.AddCommand(updateRuleCmd(cli))
cmd.AddCommand(deleteRuleCmd(cli))
cmd.AddCommand(enableRuleCmd(cli))
cmd.AddCommand(disableRuleCmd(cli))

return cmd
}
Expand Down Expand Up @@ -360,6 +362,116 @@ auth0 rules update <rule-id> -n "My Updated Rule" --enabled=false`,
return cmd
}

func enableRuleCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
Enabled bool
}

cmd := &cobra.Command{
Use: "enable",
Args: cobra.MaximumNArgs(1),
Short: "Enable a rule",
Long: "Enable a rule.",
Example: `auth0 rules enable <rule-id>`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
inputs.ID = args[0]
} else {
err := ruleID.Pick(cmd, &inputs.ID, cli.rulePickerOptions)
if err != nil {
return err
}
}

var rule *management.Rule
err := ansi.Waiting(func() error {
var err error
rule, err = cli.api.Rule.Read(inputs.ID)
return err
})
if err != nil {
return fmt.Errorf("Failed to fetch rule with ID: %s %v", inputs.ID, err)
}

rule = &management.Rule{
Enabled: auth0.Bool(true),
}

err = ansi.Waiting(func() error {
return cli.api.Rule.Update(inputs.ID, rule)
})

if err != nil {
return err
}

cli.renderer.RuleEnable(rule)
return nil
},
}

return cmd
}

func disableRuleCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
Enabled bool
}

cmd := &cobra.Command{
Use: "disable",
Args: cobra.MaximumNArgs(1),
Short: "Disable a rule",
Long: "Disable a rule.",
Example: `auth0 rules disable <rule-id>`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
inputs.ID = args[0]
} else {
err := ruleID.Pick(cmd, &inputs.ID, cli.rulePickerOptions)
if err != nil {
return err
}
}

var rule *management.Rule
err := ansi.Waiting(func() error {
var err error
rule, err = cli.api.Rule.Read(inputs.ID)
return err
})
if err != nil {
return fmt.Errorf("Failed to fetch rule with ID: %s %v", inputs.ID, err)
}

rule = &management.Rule{
Enabled: auth0.Bool(false),
}

err = ansi.Waiting(func() error {
return cli.api.Rule.Update(inputs.ID, rule)
})

if err != nil {
return err
}

cli.renderer.RuleDisable(rule)
return nil
},
}

return cmd
}

func (c *cli) rulePickerOptions() (pickerOptions, error) {
list, err := c.api.Rule.List()
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/display/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ func (r *Renderer) RuleShow(rule *management.Rule) {
r.Result(makeRuleView(rule))
}

func (r *Renderer) RuleEnable(rule *management.Rule) {
r.Heading("rule enabled")
r.Result(makeRuleView(rule))
}

func (r *Renderer) RuleDisable(rule *management.Rule) {
r.Heading("rule disabled")
r.Result(makeRuleView(rule))
}

func makeRuleView(rule *management.Rule) *ruleView {
return &ruleView{
Name: rule.GetName(),
Expand Down

0 comments on commit e5b542e

Please sign in to comment.