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 rules command #12

Merged
merged 4 commits into from
Jan 25, 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
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)

}