From 56db5edb149ff2f18ee673fd4e06840a806b46a5 Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Mon, 25 Jan 2021 00:27:05 -0500 Subject: [PATCH 1/3] feat: add rules command - list all rules in tenant --- internal/auth/auth.go | 2 +- internal/cli/root.go | 1 + internal/cli/rules.go | 36 ++++++++++++++++++++++++++++++ internal/display/rules.go | 47 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 internal/cli/rules.go create mode 100644 internal/display/rules.go 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) + +} From 30944b74f8fb11844cb69a12955ded1533267be9 Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Mon, 25 Jan 2021 11:10:06 -0500 Subject: [PATCH 2/3] fix: get json format output working --- internal/display/rules.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/display/rules.go b/internal/display/rules.go index b37613c11..bc598f0c1 100644 --- a/internal/display/rules.go +++ b/internal/display/rules.go @@ -9,15 +9,18 @@ import ( ) type ruleView struct { - rule management.Rule + Name string + Enabled bool + Id string + Order int } func (v *ruleView) AsTableHeader() []string { - return []string{"Id", "Name", "Status", "Order"} + return []string{"Id", "Name", "Enabled", "Order"} } func (v *ruleView) AsTableRow() []string { - return []string{*v.rule.ID, *v.rule.Name, isEnabled(*v.rule.Enabled), fmt.Sprintf("%d", *v.rule.Order)} + return []string{v.Id, v.Name, isEnabled(v.Enabled), fmt.Sprintf("%d", v.Order)} } func isEnabled(value bool) string { @@ -38,7 +41,10 @@ func (r *Renderer) RulesList(ruleList *management.RuleList) { for _, rule := range ruleList.Rules { res = append(res, &ruleView{ - rule: *rule, + Name: *rule.Name, + Id: *rule.ID, + Enabled: *rule.Enabled, + Order: *rule.Order, }) } From 7a1428f9ce9222ea79d40bd7acb43b70baa50b6d Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Mon, 25 Jan 2021 12:53:47 -0500 Subject: [PATCH 3/3] refactor: swap to strconv --- internal/display/rules.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/internal/display/rules.go b/internal/display/rules.go index bc598f0c1..31bebf4e1 100644 --- a/internal/display/rules.go +++ b/internal/display/rules.go @@ -3,6 +3,7 @@ package display import ( "fmt" "sort" + "strconv" "github.com/auth0/auth0-cli/internal/ansi" "gopkg.in/auth0.v5/management" @@ -11,7 +12,7 @@ import ( type ruleView struct { Name string Enabled bool - Id string + ID string Order int } @@ -20,14 +21,7 @@ func (v *ruleView) AsTableHeader() []string { } func (v *ruleView) AsTableRow() []string { - return []string{v.Id, v.Name, isEnabled(v.Enabled), fmt.Sprintf("%d", v.Order)} -} - -func isEnabled(value bool) string { - if value { - return "True" - } - return "False" + return []string{v.ID, v.Name, strconv.FormatBool(v.Enabled), fmt.Sprintf("%d", v.Order)} } func (r *Renderer) RulesList(ruleList *management.RuleList) { @@ -42,7 +36,7 @@ func (r *Renderer) RulesList(ruleList *management.RuleList) { for _, rule := range ruleList.Rules { res = append(res, &ruleView{ Name: *rule.Name, - Id: *rule.ID, + ID: *rule.ID, Enabled: *rule.Enabled, Order: *rule.Order, })