diff --git a/internal/cli/roles.go b/internal/cli/roles.go new file mode 100644 index 000000000..ea3472d83 --- /dev/null +++ b/internal/cli/roles.go @@ -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 +} diff --git a/internal/cli/root.go b/internal/cli/root.go index c70045fe1..363fac1df 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -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)) @@ -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)) diff --git a/internal/display/roles.go b/internal/display/roles.go new file mode 100644 index 000000000..0944be3cc --- /dev/null +++ b/internal/display/roles.go @@ -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), + }}) +}