- Notifications
You must be signed in to change notification settings - Fork927
chore: implement assign organization roles from the cli#13558
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -2,6 +2,7 @@ package cli | ||||
import ( | ||||
"fmt" | ||||
"strings" | ||||
"golang.org/x/xerrors" | ||||
@@ -11,16 +12,75 @@ import ( | ||||
) | ||||
func (r *RootCmd) organizationMembers() *serpent.Command { | ||||
cmd := &serpent.Command{ | ||||
Use: "members", | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Is this ready for end users to start consuming? If not, do we want to start with it hidden? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It is not, but the parent command is hidden: Line 24 ina8fe429
So this is also hidden There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nice 👍 | ||||
Aliases: []string{"member"}, | ||||
Short: "Manage organization members", | ||||
Children: []*serpent.Command{ | ||||
r.listOrganizationMembers(), | ||||
r.assignOrganizationRoles(), | ||||
}, | ||||
Handler: func(inv *serpent.Invocation) error { | ||||
return inv.Command.HelpHandler(inv) | ||||
}, | ||||
} | ||||
return cmd | ||||
} | ||||
func (r *RootCmd) assignOrganizationRoles() *serpent.Command { | ||||
client := new(codersdk.Client) | ||||
cmd := &serpent.Command{ | ||||
Use: "edit-roles <username | user_id> [roles...]", | ||||
Aliases: []string{"edit-role"}, | ||||
Short: "Edit organization member's roles", | ||||
Middleware: serpent.Chain( | ||||
r.InitClient(client), | ||||
), | ||||
Handler: func(inv *serpent.Invocation) error { | ||||
ctx := inv.Context() | ||||
organization, err := CurrentOrganization(r, inv, client) | ||||
if err != nil { | ||||
return err | ||||
} | ||||
if len(inv.Args) < 1 { | ||||
return xerrors.Errorf("user_id or username is required as the first argument") | ||||
} | ||||
userIdentifier := inv.Args[0] | ||||
roles := inv.Args[1:] | ||||
member, err := client.UpdateOrganizationMemberRoles(ctx, organization.ID, userIdentifier, codersdk.UpdateRoles{ | ||||
Roles: roles, | ||||
}) | ||||
if err != nil { | ||||
return xerrors.Errorf("update member roles: %w", err) | ||||
} | ||||
updatedTo := make([]string, 0) | ||||
for _, role := range member.Roles { | ||||
updatedTo = append(updatedTo, role.String()) | ||||
} | ||||
_, _ = fmt.Fprintf(inv.Stdout, "Member roles updated to [%s]\n", strings.Join(updatedTo, ", ")) | ||||
return nil | ||||
}, | ||||
} | ||||
return cmd | ||||
} | ||||
func (r *RootCmd) listOrganizationMembers() *serpent.Command { | ||||
formatter := cliui.NewOutputFormatter( | ||||
cliui.TableFormat([]codersdk.OrganizationMemberWithName{}, []string{"username", "organization_roles"}), | ||||
cliui.JSONFormat(), | ||||
) | ||||
client := new(codersdk.Client) | ||||
cmd := &serpent.Command{ | ||||
Use: "list", | ||||
Short: "List all organization members", | ||||
Middleware: serpent.Chain( | ||||
serpent.RequireNArgs(0), | ||||
r.InitClient(client), | ||||
Uh oh!
There was an error while loading.Please reload this page.