1
1
package cli
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"regexp"
6
7
@@ -13,12 +14,6 @@ import (
13
14
14
15
const defaultGroupDisplay = "-"
15
16
16
- type workspaceShareRow struct {
17
- User string `table:"user"`
18
- Group string `table:"group,default_sort"`
19
- Role codersdk.WorkspaceRole `table:"role"`
20
- }
21
-
22
17
func (r * RootCmd ) sharing () * serpent.Command {
23
18
orgContext := NewOrganizationContext ()
24
19
@@ -29,26 +24,54 @@ func (r *RootCmd) sharing() *serpent.Command {
29
24
Handler : func (inv * serpent.Invocation ) error {
30
25
return inv .Command .HelpHandler (inv )
31
26
},
32
- Children : []* serpent.Command {r .shareWorkspace (orgContext )},
33
- Hidden : true ,
27
+ Children : []* serpent.Command {
28
+ r .shareWorkspace (orgContext ),
29
+ r .showWorkspaceSharing (orgContext ),
30
+ },
31
+ Hidden : true ,
34
32
}
35
33
36
34
orgContext .AttachOptions (cmd )
37
35
return cmd
38
36
}
39
37
38
+ func (r * RootCmd ) showWorkspaceSharing (orgContext * OrganizationContext ) * serpent.Command {
39
+ var (
40
+ client = new (codersdk.Client )
41
+ )
42
+
43
+ cmd := & serpent.Command {
44
+ Use : "show <workspace>" ,
45
+ Short : "Show all users and groups the given Workspace is shared with." ,
46
+ Middleware : serpent .Chain (serpent .RequireNArgs (1 )),
47
+ Handler : func (inv * serpent.Invocation ) error {
48
+ worksace , err := namedWorkspace (inv .Context (), client , inv .Args [0 ])
49
+ if err != nil {
50
+ return xerrors .Errorf ("unable to fetch Workspace %s: %w" , inv .Args [0 ], err )
51
+ }
52
+
53
+ acl , err := client .WorkspaceACL (inv .Context (), worksace .ID )
54
+ if err != nil {
55
+ return xerrors .Errorf ("unable to fetch ACL for Workspace: %w" , err )
56
+ }
57
+
58
+ out , err := workspaceACLToTable (inv .Context (), & acl )
59
+
60
+ _ , err = fmt .Fprintln (inv .Stdout , out )
61
+ return err
62
+ },
63
+ }
64
+
65
+ return cmd
66
+ }
67
+
40
68
func (r * RootCmd ) shareWorkspace (orgContext * OrganizationContext ) * serpent.Command {
41
69
var (
42
70
// Username regex taken from codersdk/name.go
43
71
nameRoleRegex = regexp .MustCompile (`(^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)+(?::([A-Za-z0-9-]+))?` )
44
72
client = new (codersdk.Client )
45
73
users []string
46
74
groups []string
47
- formatter = cliui .NewOutputFormatter (
48
- cliui .TableFormat (
49
- []workspaceShareRow {}, []string {"User" , "Group" , "Role" }),
50
- cliui .JSONFormat (),
51
- )
52
75
)
53
76
54
77
cmd := & serpent.Command {
@@ -161,40 +184,12 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
161
184
return err
162
185
}
163
186
164
- workspaceACL , err := client .WorkspaceACL (inv .Context (), workspace .ID )
187
+ acl , err := client .WorkspaceACL (inv .Context (), workspace .ID )
165
188
if err != nil {
166
189
return xerrors .Errorf ("could not fetch current workspace ACL after sharing %w" , err )
167
190
}
168
191
169
- outputRows := make ([]workspaceShareRow , 0 )
170
- for _ , user := range workspaceACL .Users {
171
- if user .Role == codersdk .WorkspaceRoleDeleted {
172
- continue
173
- }
174
-
175
- outputRows = append (outputRows , workspaceShareRow {
176
- User : user .Username ,
177
- Group : defaultGroupDisplay ,
178
- Role : user .Role ,
179
- })
180
- }
181
- for _ , group := range workspaceACL .Groups {
182
- if group .Role == codersdk .WorkspaceRoleDeleted {
183
- continue
184
- }
185
-
186
- for _ , user := range group .Members {
187
- outputRows = append (outputRows , workspaceShareRow {
188
- User : user .Username ,
189
- Group : group .Name ,
190
- Role : group .Role ,
191
- })
192
- }
193
- }
194
- out , err := formatter .Format (inv .Context (), outputRows )
195
- if err != nil {
196
- return err
197
- }
192
+ out , err := workspaceACLToTable (inv .Context (), & acl )
198
193
199
194
_ , err = fmt .Fprintln (inv .Stdout , out )
200
195
return err
@@ -215,3 +210,48 @@ func stringToWorkspaceRole(role string) (codersdk.WorkspaceRole, error) {
215
210
role , codersdk .WorkspaceRoleAdmin , codersdk .WorkspaceRoleUse )
216
211
}
217
212
}
213
+
214
+ func workspaceACLToTable (ctx context.Context , acl * codersdk.WorkspaceACL ) (string , error ) {
215
+ type workspaceShareRow struct {
216
+ User string `table:"user"`
217
+ Group string `table:"group,default_sort"`
218
+ Role codersdk.WorkspaceRole `table:"role"`
219
+ }
220
+
221
+ var formatter = cliui .NewOutputFormatter (
222
+ cliui .TableFormat (
223
+ []workspaceShareRow {}, []string {"User" , "Group" , "Role" }),
224
+ cliui .JSONFormat ())
225
+
226
+ outputRows := make ([]workspaceShareRow , 0 )
227
+ for _ , user := range acl .Users {
228
+ if user .Role == codersdk .WorkspaceRoleDeleted {
229
+ continue
230
+ }
231
+
232
+ outputRows = append (outputRows , workspaceShareRow {
233
+ User : user .Username ,
234
+ Group : defaultGroupDisplay ,
235
+ Role : user .Role ,
236
+ })
237
+ }
238
+ for _ , group := range acl .Groups {
239
+ if group .Role == codersdk .WorkspaceRoleDeleted {
240
+ continue
241
+ }
242
+
243
+ for _ , user := range group .Members {
244
+ outputRows = append (outputRows , workspaceShareRow {
245
+ User : user .Username ,
246
+ Group : group .Name ,
247
+ Role : group .Role ,
248
+ })
249
+ }
250
+ }
251
+ out , err := formatter .Format (ctx , outputRows )
252
+ if err != nil {
253
+ return "" , err
254
+ }
255
+
256
+ return out , nil
257
+ }
0 commit comments