Skip to content

Commit 3ff3630

Browse files
committed
wip
1 parent a88364c commit 3ff3630

File tree

1 file changed

+83
-43
lines changed

1 file changed

+83
-43
lines changed

cli/sharing.go

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"context"
45
"fmt"
56
"regexp"
67

@@ -13,12 +14,6 @@ import (
1314

1415
const defaultGroupDisplay = "-"
1516

16-
type workspaceShareRow struct {
17-
User string `table:"user"`
18-
Group string `table:"group,default_sort"`
19-
Role codersdk.WorkspaceRole `table:"role"`
20-
}
21-
2217
func (r *RootCmd) sharing() *serpent.Command {
2318
orgContext := NewOrganizationContext()
2419

@@ -29,26 +24,54 @@ func (r *RootCmd) sharing() *serpent.Command {
2924
Handler: func(inv *serpent.Invocation) error {
3025
return inv.Command.HelpHandler(inv)
3126
},
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,
3432
}
3533

3634
orgContext.AttachOptions(cmd)
3735
return cmd
3836
}
3937

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+
4068
func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Command {
4169
var (
4270
// Username regex taken from codersdk/name.go
4371
nameRoleRegex = regexp.MustCompile(`(^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)+(?::([A-Za-z0-9-]+))?`)
4472
client = new(codersdk.Client)
4573
users []string
4674
groups []string
47-
formatter = cliui.NewOutputFormatter(
48-
cliui.TableFormat(
49-
[]workspaceShareRow{}, []string{"User", "Group", "Role"}),
50-
cliui.JSONFormat(),
51-
)
5275
)
5376

5477
cmd := &serpent.Command{
@@ -161,40 +184,12 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
161184
return err
162185
}
163186

164-
workspaceACL, err := client.WorkspaceACL(inv.Context(), workspace.ID)
187+
acl, err := client.WorkspaceACL(inv.Context(), workspace.ID)
165188
if err != nil {
166189
return xerrors.Errorf("could not fetch current workspace ACL after sharing %w", err)
167190
}
168191

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)
198193

199194
_, err = fmt.Fprintln(inv.Stdout, out)
200195
return err
@@ -215,3 +210,48 @@ func stringToWorkspaceRole(role string) (codersdk.WorkspaceRole, error) {
215210
role, codersdk.WorkspaceRoleAdmin, codersdk.WorkspaceRoleUse)
216211
}
217212
}
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

Comments
 (0)