diff --git a/internal/cli/rules.go b/internal/cli/rules.go index 364d39173..45cfee0dd 100644 --- a/internal/cli/rules.go +++ b/internal/cli/rules.go @@ -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" ) @@ -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)) @@ -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 } @@ -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 } @@ -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)}) +} diff --git a/internal/rules/rules.go b/internal/rules/rules.go new file mode 100644 index 000000000..1e8c7fa08 --- /dev/null +++ b/internal/rules/rules.go @@ -0,0 +1 @@ +package rules