mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
1b4ca00428
* chore: include custom roles in list org roles * move cli show roles to org scope
114 lines
3.1 KiB
Go
114 lines
3.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/serpent"
|
|
)
|
|
|
|
func (r *RootCmd) organizationRoles() *serpent.Command {
|
|
cmd := &serpent.Command{
|
|
Use: "roles",
|
|
Short: "Manage organization roles.",
|
|
Aliases: []string{"role"},
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
return inv.Command.HelpHandler(inv)
|
|
},
|
|
Hidden: true,
|
|
Children: []*serpent.Command{
|
|
r.showOrganizationRoles(),
|
|
},
|
|
}
|
|
return cmd
|
|
}
|
|
|
|
func (r *RootCmd) showOrganizationRoles() *serpent.Command {
|
|
formatter := cliui.NewOutputFormatter(
|
|
cliui.ChangeFormatterData(
|
|
cliui.TableFormat([]assignableRolesTableRow{}, []string{"name", "display_name", "built_in", "site_permissions", "org_permissions", "user_permissions"}),
|
|
func(data any) (any, error) {
|
|
input, ok := data.([]codersdk.AssignableRoles)
|
|
if !ok {
|
|
return nil, xerrors.Errorf("expected []codersdk.AssignableRoles got %T", data)
|
|
}
|
|
rows := make([]assignableRolesTableRow, 0, len(input))
|
|
for _, role := range input {
|
|
rows = append(rows, assignableRolesTableRow{
|
|
Name: role.Name,
|
|
DisplayName: role.DisplayName,
|
|
SitePermissions: fmt.Sprintf("%d permissions", len(role.SitePermissions)),
|
|
OrganizationPermissions: fmt.Sprintf("%d organizations", len(role.OrganizationPermissions)),
|
|
UserPermissions: fmt.Sprintf("%d permissions", len(role.UserPermissions)),
|
|
Assignable: role.Assignable,
|
|
BuiltIn: role.BuiltIn,
|
|
})
|
|
}
|
|
return rows, nil
|
|
},
|
|
),
|
|
cliui.JSONFormat(),
|
|
)
|
|
|
|
client := new(codersdk.Client)
|
|
cmd := &serpent.Command{
|
|
Use: "show [role_names ...]",
|
|
Short: "Show role(s)",
|
|
Middleware: serpent.Chain(
|
|
r.InitClient(client),
|
|
),
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
ctx := inv.Context()
|
|
org, err := CurrentOrganization(r, inv, client)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
roles, err := client.ListOrganizationRoles(ctx, org.ID)
|
|
if err != nil {
|
|
return xerrors.Errorf("listing roles: %w", err)
|
|
}
|
|
|
|
if len(inv.Args) > 0 {
|
|
// filter roles
|
|
filtered := make([]codersdk.AssignableRoles, 0)
|
|
for _, role := range roles {
|
|
if slices.ContainsFunc(inv.Args, func(s string) bool {
|
|
return strings.EqualFold(s, role.Name)
|
|
}) {
|
|
filtered = append(filtered, role)
|
|
}
|
|
}
|
|
roles = filtered
|
|
}
|
|
|
|
out, err := formatter.Format(inv.Context(), roles)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = fmt.Fprintln(inv.Stdout, out)
|
|
return err
|
|
},
|
|
}
|
|
formatter.AttachOptions(&cmd.Options)
|
|
|
|
return cmd
|
|
}
|
|
|
|
type assignableRolesTableRow struct {
|
|
Name string `table:"name,default_sort"`
|
|
DisplayName string `table:"display_name"`
|
|
SitePermissions string ` table:"site_permissions"`
|
|
// map[<org_id>] -> Permissions
|
|
OrganizationPermissions string `table:"org_permissions"`
|
|
UserPermissions string `table:"user_permissions"`
|
|
Assignable bool `table:"assignable"`
|
|
BuiltIn bool `table:"built_in"`
|
|
}
|