Skip to content

Commit 3eb707c

Browse files
committed
feat: allow sharing workspaces with groups via the share command
1 parent 875c358 commit 3eb707c

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

cli/sharing.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ import (
88
)
99

1010
func (r *RootCmd) sharing() *serpent.Command {
11+
orgContext := NewOrganizationContext()
12+
1113
cmd := &serpent.Command{
1214
Use: "sharing [subcommand]",
1315
Short: "Commands for managing shared workspaces",
1416
Aliases: []string{"share"},
1517
Handler: func(inv *serpent.Invocation) error {
1618
return inv.Command.HelpHandler(inv)
1719
},
18-
Children: []*serpent.Command{r.shareWorkspace()},
20+
Children: []*serpent.Command{r.shareWorkspace(orgContext)},
1921
}
2022

23+
orgContext.AttachOptions(cmd)
2124
return cmd
2225
}
2326

24-
func (r *RootCmd) shareWorkspace() *serpent.Command {
27+
func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Command {
2528
var (
2629
client = new(codersdk.Client)
2730
userAndGroupRegex = regexp.MustCompile(`([A-Za-z0-9]+)(?::([A-Za-z0-9]+))?`)
@@ -54,6 +57,8 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
5457
Handler: func(inv *serpent.Invocation) error {
5558
workspaceAndOwner := workspaceRegex.FindStringSubmatch(inv.Args[0])
5659

60+
// Assume the workspace is being given in the format <owner>/<workspace>
61+
// If the owner is an empty string assume just a workspace name was given
5762
ownerUsername := workspaceAndOwner[1]
5863
workspaceName := workspaceAndOwner[2]
5964
if workspaceName == "" {
@@ -65,7 +70,7 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
6570
if ownerUsername != "" {
6671
owner, err := client.User(inv.Context(), ownerUsername)
6772
if err != nil {
68-
return xerrors.Errorf("could not workspace owner with username %s.", ownerUsername)
73+
return xerrors.Errorf("could not fetch the workspace owner with the username %s.", ownerUsername)
6974
}
7075

7176
ownerId = owner.ID.String()
@@ -75,7 +80,7 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
7580
IncludeDeleted: false,
7681
})
7782
if err != nil {
78-
return xerrors.Errorf("could not fetch workspace %s.", workspaceName)
83+
return xerrors.Errorf("could not fetch the workspace %s.", workspaceName)
7984
}
8085

8186
userRoles := make(map[string]codersdk.WorkspaceRole, len(users))
@@ -97,26 +102,45 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
97102
userRoles[user.ID.String()] = workspaceRole
98103
}
99104

100-
// groupRoles := make(map[string]codersdk.WorkspaceRole)
101-
// for _, group := range groups {
102-
// groupAndRole := userAndGroupRegex.FindStringSubmatch(group)
103-
// groupname := groupAndRole[1]
104-
// role := groupAndRole[2]
105+
groupRoles := make(map[string]codersdk.WorkspaceRole)
106+
107+
org, err := orgContext.Selected(inv, client)
108+
if err != nil {
109+
return err
110+
}
111+
orgGroups, err := client.Groups(inv.Context(), codersdk.GroupArguments{
112+
Organization: org.ID.String(),
113+
})
114+
115+
for _, group := range groups {
116+
groupAndRole := userAndGroupRegex.FindStringSubmatch(group)
117+
groupName := groupAndRole[1]
118+
role := groupAndRole[2]
119+
120+
var orgGroup *codersdk.Group
121+
for _, g := range orgGroups {
122+
if g.Name == groupName {
123+
orgGroup = &g
124+
break
125+
}
126+
}
105127

106-
// group, err := client.Groups()
128+
if orgGroup == nil {
129+
return xerrors.Errorf("Could not find group named %s belonging to the organization %s", groupName, org.Name)
130+
}
107131

108-
// workspaceRole, err := stringToWorkspaceRole(role)
109-
// if err != nil {
110-
// return err
111-
// }
132+
workspaceRole, err := stringToWorkspaceRole(role)
133+
if err != nil {
134+
return err
135+
}
112136

113-
// groupRoles[groupname] = workspaceRole
137+
groupRoles[orgGroup.ID.String()] = workspaceRole
114138

115-
// }
139+
}
116140

117141
err = client.UpdateWorkspaceACL(inv.Context(), workspace.ID, codersdk.UpdateWorkspaceACL{
118-
UserRoles: userRoles,
119-
// GroupRoles: groupRoles,
142+
UserRoles: userRoles,
143+
GroupRoles: groupRoles,
120144
})
121145
if err != nil {
122146
return err

0 commit comments

Comments
 (0)