diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 4b9d82c53..a8f93760f 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -33,7 +33,7 @@ const ( deviceCodeEndpoint = "https://auth0.auth0.com/oauth/device/code" oauthTokenEndpoint = "https://auth0.auth0.com/oauth/token" // TODO(jfatta) extend the scope as we extend the CLI: - scope = "openid read:roles" + scope = "openid read:roles read:rules" audiencePath = "/api/v2/" ) diff --git a/internal/cli/root.go b/internal/cli/root.go index 716b3ba02..0ccc35d29 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -51,6 +51,7 @@ func Execute() { rootCmd.AddCommand(loginCmd(cli)) rootCmd.AddCommand(clientsCmd(cli)) rootCmd.AddCommand(logsCmd(cli)) + rootCmd.AddCommand(rulesCmd(cli)) // TODO(cyx): backport this later on using latest auth0/v5. // rootCmd.AddCommand(actionsCmd(cli)) diff --git a/internal/cli/rules.go b/internal/cli/rules.go new file mode 100644 index 000000000..f3bb6bce3 --- /dev/null +++ b/internal/cli/rules.go @@ -0,0 +1,36 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +func rulesCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "rules", + Short: "manage rules for clients.", + } + + cmd.SetUsageTemplate(resourceUsageTemplate()) + cmd.AddCommand(listRulesCmd(cli)) + return cmd +} + +func listRulesCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + 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() + + if err != nil { + return err + } + + cli.renderer.RulesList(rules) + return nil + }, + } + + return cmd +} diff --git a/internal/display/rules.go b/internal/display/rules.go new file mode 100644 index 000000000..b37613c11 --- /dev/null +++ b/internal/display/rules.go @@ -0,0 +1,47 @@ +package display + +import ( + "fmt" + "sort" + + "github.com/auth0/auth0-cli/internal/ansi" + "gopkg.in/auth0.v5/management" +) + +type ruleView struct { + rule management.Rule +} + +func (v *ruleView) AsTableHeader() []string { + return []string{"Id", "Name", "Status", "Order"} +} + +func (v *ruleView) AsTableRow() []string { + return []string{*v.rule.ID, *v.rule.Name, isEnabled(*v.rule.Enabled), fmt.Sprintf("%d", *v.rule.Order)} +} + +func isEnabled(value bool) string { + if value { + return "True" + } + return "False" +} + +func (r *Renderer) RulesList(ruleList *management.RuleList) { + r.Heading(ansi.Bold(r.Tenant), "rules\n") + var res []View + + //@TODO Provide sort options via flags + sort.Slice(ruleList.Rules, func(i, j int) bool { + return ruleList.Rules[i].GetOrder() < ruleList.Rules[j].GetOrder() + }) + + for _, rule := range ruleList.Rules { + res = append(res, &ruleView{ + rule: *rule, + }) + } + + r.Results(res) + +}