@@ -8,20 +8,23 @@ import (
8
8
)
9
9
10
10
func (r * RootCmd ) sharing () * serpent.Command {
11
+ orgContext := NewOrganizationContext ()
12
+
11
13
cmd := & serpent.Command {
12
14
Use : "sharing [subcommand]" ,
13
15
Short : "Commands for managing shared workspaces" ,
14
16
Aliases : []string {"share" },
15
17
Handler : func (inv * serpent.Invocation ) error {
16
18
return inv .Command .HelpHandler (inv )
17
19
},
18
- Children : []* serpent.Command {r .shareWorkspace ()},
20
+ Children : []* serpent.Command {r .shareWorkspace (orgContext )},
19
21
}
20
22
23
+ orgContext .AttachOptions (cmd )
21
24
return cmd
22
25
}
23
26
24
- func (r * RootCmd ) shareWorkspace () * serpent.Command {
27
+ func (r * RootCmd ) shareWorkspace (orgContext * OrganizationContext ) * serpent.Command {
25
28
var (
26
29
client = new (codersdk.Client )
27
30
userAndGroupRegex = regexp .MustCompile (`([A-Za-z0-9]+)(?::([A-Za-z0-9]+))?` )
@@ -54,6 +57,8 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
54
57
Handler : func (inv * serpent.Invocation ) error {
55
58
workspaceAndOwner := workspaceRegex .FindStringSubmatch (inv .Args [0 ])
56
59
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
57
62
ownerUsername := workspaceAndOwner [1 ]
58
63
workspaceName := workspaceAndOwner [2 ]
59
64
if workspaceName == "" {
@@ -65,7 +70,7 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
65
70
if ownerUsername != "" {
66
71
owner , err := client .User (inv .Context (), ownerUsername )
67
72
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 )
69
74
}
70
75
71
76
ownerId = owner .ID .String ()
@@ -75,7 +80,7 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
75
80
IncludeDeleted : false ,
76
81
})
77
82
if err != nil {
78
- return xerrors .Errorf ("could not fetch workspace %s." , workspaceName )
83
+ return xerrors .Errorf ("could not fetch the workspace %s." , workspaceName )
79
84
}
80
85
81
86
userRoles := make (map [string ]codersdk.WorkspaceRole , len (users ))
@@ -97,26 +102,45 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
97
102
userRoles [user .ID .String ()] = workspaceRole
98
103
}
99
104
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
+ }
105
127
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
+ }
107
131
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
+ }
112
136
113
- // groupRoles[groupname ] = workspaceRole
137
+ groupRoles [orgGroup . ID . String () ] = workspaceRole
114
138
115
- // }
139
+ }
116
140
117
141
err = client .UpdateWorkspaceACL (inv .Context (), workspace .ID , codersdk.UpdateWorkspaceACL {
118
- UserRoles : userRoles ,
119
- // GroupRoles: groupRoles,
142
+ UserRoles : userRoles ,
143
+ GroupRoles : groupRoles ,
120
144
})
121
145
if err != nil {
122
146
return err
0 commit comments