Skip to content

Commit

Permalink
feat: add rules command
Browse files Browse the repository at this point in the history
- list all rules in tenant
  • Loading branch information
vprasanth committed Jan 25, 2021
1 parent 5dbd056 commit 56db5ed
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
)

Expand Down
1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
36 changes: 36 additions & 0 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions internal/display/rules.go
Original file line number Diff line number Diff line change
@@ -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)

}

0 comments on commit 56db5ed

Please sign in to comment.