Skip to content

Commit

Permalink
Merge pull request #12 from auth0/list-rules
Browse files Browse the repository at this point in the history
feat: add rules command
  • Loading branch information
cyx authored Jan 25, 2021
2 parents c371cc0 + 7a1428f commit 70b0842
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 read:clients read:logs"
scope = "openid read:roles read:clients read:logs 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 @@ -53,6 +53,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"
"strconv"

"github.com/auth0/auth0-cli/internal/ansi"
"gopkg.in/auth0.v5/management"
)

type ruleView struct {
Name string
Enabled bool
ID string
Order int
}

func (v *ruleView) AsTableHeader() []string {
return []string{"Id", "Name", "Enabled", "Order"}
}

func (v *ruleView) AsTableRow() []string {
return []string{v.ID, v.Name, strconv.FormatBool(v.Enabled), fmt.Sprintf("%d", v.Order)}
}

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{
Name: *rule.Name,
ID: *rule.ID,
Enabled: *rule.Enabled,
Order: *rule.Order,
})
}

r.Results(res)

}

0 comments on commit 70b0842

Please sign in to comment.