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

feat: add enable/disable rules support to cli #16

Merged
merged 15 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
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
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