Skip to content

Commit

Permalink
Add list rolesCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed May 14, 2021
1 parent e83164c commit 48a18db
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
52 changes: 52 additions & 0 deletions internal/cli/roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cli

import (
"fmt"

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

func rolesCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "roles",
Short: "Manage resources for roles",
Long: "Manage resources for roles.",
Aliases: []string{"role"},
}

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(listRolesCmd(cli))

return cmd
}

func listRolesCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Args: cobra.NoArgs,
Short: "List your roles",
Long: `List your existing roles. To create one try:
auth0 roles create`,
Example: `auth0 roles list
auth0 roles ls`,
RunE: func(cmd *cobra.Command, args []string) error {
var list *management.RoleList

if err := ansi.Waiting(func() error {
var err error
list, err = cli.api.Role.List()
return err
}); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}

cli.renderer.RoleList(list.Roles)
return nil
},
}

return cmd
}
3 changes: 2 additions & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func Execute() {
"no-input", false, "Disable interactivity.")

rootCmd.PersistentFlags().BoolVar(&cli.noColor,
"no-color", false, "Disable colors.")
"no-color", false, "Disable colors.")
// order of the comamnds here matters
// so add new commands in a place that reflect its relevance or relation with other commands:
rootCmd.AddCommand(loginCmd(cli))
Expand All @@ -100,6 +100,7 @@ func Execute() {
rootCmd.AddCommand(logsCmd(cli))
rootCmd.AddCommand(logoutCmd(cli))
rootCmd.AddCommand(brandingCmd(cli))
rootCmd.AddCommand(rolesCmd(cli))

// keep completion at the bottom:
rootCmd.AddCommand(completionCmd(cli))
Expand Down
75 changes: 75 additions & 0 deletions internal/display/roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package display

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

type roleView struct {
Name string
ID string
Description string
}

func (v *roleView) AsTableHeader() []string {
return []string{"Name", "Role ID", "Description"}
}

func (v *roleView) AsTableRow() []string {
return []string{
v.Name,
ansi.Faint(v.ID),
v.Description,
}
}

func (r *Renderer) RoleList(roles []*management.Role) {
resource := "roles"

r.Heading(resource)

if len(roles) == 0 {
r.EmptyState(resource)
r.Infof("Use 'auth0 roles create' to add one")
return
}

var res []View
for _, role := range roles {
res = append(res, &roleView{
Name: role.GetName(),
ID: role.GetID(),
Description: role.GetDescription(),
})
}

r.Results(res)
}

func (r *Renderer) RoleGet(role *management.Role) {
r.Heading(ansi.Bold(r.Tenant), "role\n")
r.Results([]View{&roleView{
Name: auth0.StringValue(role.Name),
ID: auth0.StringValue(role.ID),
Description: auth0.StringValue(role.Description),
}})
}

func (r *Renderer) RoleUpdate(role *management.Role) {
r.Heading(ansi.Bold(r.Tenant), "role\n")
r.Results([]View{&roleView{
Name: auth0.StringValue(role.Name),
ID: auth0.StringValue(role.ID),
Description: auth0.StringValue(role.Description),
}})
}

func (r *Renderer) RoleCreate(role *management.Role) {
r.Heading(ansi.Bold(r.Tenant), "role\n")
r.Results([]View{&roleView{
Name: auth0.StringValue(role.Name),
ID: auth0.StringValue(role.ID),
Description: auth0.StringValue(role.Description),
}})
}

0 comments on commit 48a18db

Please sign in to comment.