Skip to content

Commit

Permalink
feat: add enable/disable sub-command to rules (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Cyril David <[email protected]>

Close A0CLI-10
  • Loading branch information
vprasanth authored Jan 26, 2021
1 parent d3c341c commit c668388
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
108 changes: 106 additions & 2 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cli

import (
"fmt"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5"
"gopkg.in/auth0.v5/management"
)

Expand All @@ -14,6 +17,8 @@ func rulesCmd(cli *cli) *cobra.Command {

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(listRulesCmd(cli))
cmd.AddCommand(enableRuleCmd(cli))
cmd.AddCommand(disableRuleCmd(cli))
cmd.AddCommand(createRulesCmd(cli))
cmd.AddCommand(deleteRulesCmd(cli))

Expand All @@ -26,17 +31,95 @@ func listRulesCmd(cli *cli) *cobra.Command {
Short: "Lists your rules",
Long: `Lists the rules in your current tenant.`,
RunE: func(cmd *cobra.Command, args []string) error {
rules, err := cli.api.Client.Rule.List()
rules, err := getRules(cli)

if err != nil {
return err
}

cli.renderer.RulesList(rules)
return nil
},
}

return cmd
}

func enableRuleCmd(cli *cli) *cobra.Command {
var name string
cmd := &cobra.Command{
Use: "enable",
Short: "enable rule(s)",
RunE: func(cmd *cobra.Command, args []string) error {
data, err := getRules(cli)
if err != nil {
return err
}

rule := findRuleByName(name, data.Rules)
if rule != nil {
err := enableRule(rule, cli)
if err != nil {
return err
}
} else {
return fmt.Errorf("No rule found with name: %q", name)
}

// @TODO Only display modified rules
rules, err := getRules(cli)

if err != nil {
return err
}

cli.renderer.RulesList(rules)

return nil
},
}

cmd.Flags().StringVarP(&name, "name", "n", "", "rule name")
mustRequireFlags(cmd, "name")
return cmd
}

func disableRuleCmd(cli *cli) *cobra.Command {
var name string
cmd := &cobra.Command{
Use: "disable",
Short: "disable rule",
RunE: func(cmd *cobra.Command, args []string) error {
data, err := getRules(cli)
if err != nil {
return err
}

rule := findRuleByName(name, data.Rules)
if rule != nil {
if err := disableRule(rule, cli); err != nil {
return err
}
} else {
return fmt.Errorf("No rule found with name: %q", name)
}

// @TODO Only display modified rules
rules, err := getRules(cli)

if err != nil {
return err
}

cli.renderer.RulesList(rules)

return nil
},
}

cmd.Flags().StringVarP(&name, "name", "n", "", "rule name")
mustRequireFlags(cmd, "name")

return cmd
}

Expand Down Expand Up @@ -80,7 +163,6 @@ func createRulesCmd(cli *cli) *cobra.Command {
cmd.Flags().StringVarP(&flags.script, "script", "s", "", "Code to be executed when this rule runs (required)")
cmd.Flags().IntVarP(&flags.order, "order", "o", 0, "Order that this rule should execute in relative to other rules. Lower-valued rules execute first.")
cmd.Flags().BoolVarP(&flags.enabled, "enabled", "e", false, "Whether the rule is enabled (true), or disabled (false).")

return cmd
}

Expand Down Expand Up @@ -117,3 +199,25 @@ func deleteRulesCmd(cli *cli) *cobra.Command {

return cmd
}

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

func findRuleByName(name string, rules []*management.Rule) *management.Rule {
for _, r := range rules {
if auth0.StringValue(r.Name) == name {
return r
}
}
return nil
}

func enableRule(rule *management.Rule, cli *cli) error {
return cli.api.Client.Rule.Update(rule.GetID(), &management.Rule{Enabled: auth0.Bool(true)})
}

func disableRule(rule *management.Rule, cli *cli) error {
return cli.api.Client.Rule.Update(rule.GetID(), &management.Rule{Enabled: auth0.Bool(false)})
}
1 change: 1 addition & 0 deletions internal/rules/rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rules

0 comments on commit c668388

Please sign in to comment.