Skip to content

feat: add edit-role within user command #17341

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

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: add roles flag to user edit-roles command for passing in roles
  • Loading branch information
brettkolodny committed Apr 15, 2025
commit 681ecba2c54e2bfa7b84d3a96aa75229ad7e094b
54 changes: 44 additions & 10 deletions cli/usereditroles.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cli

import (
"slices"
"sort"
"strings"

"golang.org/x/xerrors"

Expand All @@ -12,9 +14,20 @@ import (

func (r *RootCmd) userEditRoles() *serpent.Command {
client := new(codersdk.Client)

var givenRoles []string

cmd := &serpent.Command{
Use: "edit-roles <username|user_id>",
Short: "Edit a user's roles by username or id",
Use: "edit-roles <username|user_id>",
Short: "Edit a user's roles by username or id",
Options: []serpent.Option{
cliui.SkipPromptOption(),
{
Name: "roles",
Description: "A list of roles to give to the user. This removes any existing roles the user may have.",
Flag: "roles",
Value: serpent.StringArrayOf(&givenRoles)},
},
Middleware: serpent.Chain(serpent.RequireNArgs(1), r.InitClient(client)),
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()
Expand All @@ -35,20 +48,41 @@ func (r *RootCmd) userEditRoles() *serpent.Command {
siteRoles = append(siteRoles, role.Name)
}
}
sort.Strings(siteRoles)

userRoles, err := client.UserRoles(ctx, user.Username)
if err != nil {
return xerrors.Errorf("fetch user roles: %w", err)
}

sort.Strings(siteRoles)
selectedRoles, err := cliui.MultiSelect(inv, cliui.MultiSelectOptions{
Message: "Select the roles you'd like to assign to the user",
Options: siteRoles,
Defaults: userRoles.Roles,
})
if err != nil {
return xerrors.Errorf("selecting roles for user: %w", err)
var selectedRoles []string
if len(givenRoles) > 0 {
// If the none role is present ignore all other roles.
// This is so there is a way to clear roles from the CLI without making a
// new command.
if slices.Contains(givenRoles, "none") {
selectedRoles = []string{}
} else {
// Make sure all of the given roles are valid site roles
for _, givenRole := range givenRoles {
if !slices.Contains(siteRoles, givenRole) {
siteRolesPretty := strings.Join(siteRoles, ", ")
return xerrors.Errorf("The role %s is not valid. Please use one or more of the following roles: %s, or none\n", givenRole, siteRolesPretty)
}
}

selectedRoles = givenRoles
}
} else {
selectedRoles, err = cliui.MultiSelect(inv, cliui.MultiSelectOptions{
Message: "Select the roles you'd like to assign to the user",
Options: siteRoles,
Defaults: userRoles.Roles,
})
if err != nil {
return xerrors.Errorf("selecting roles for user: %w", err)
}

}

_, err = client.UpdateUserRoles(ctx, user.Username, codersdk.UpdateRoles{
Expand Down