Skip to content

Commit d27d3d4

Browse files
committed
feat: output the ACL state after the share command completes
1 parent 8b443b0 commit d27d3d4

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

cli/sharing.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package cli
22

33
import (
4+
"fmt"
5+
"regexp"
6+
7+
"github.com/coder/coder/v2/cli/cliui"
48
"github.com/coder/coder/v2/codersdk"
59
"github.com/coder/serpent"
610
"golang.org/x/xerrors"
7-
"regexp"
811
)
912

13+
type workspaceShareRow struct {
14+
User string `table:"user"`
15+
Group string `table:"group,default_sort"`
16+
Role codersdk.WorkspaceRole `table:"workspace_role"`
17+
}
18+
1019
func (r *RootCmd) sharing() *serpent.Command {
1120
orgContext := NewOrganizationContext()
1221

@@ -30,6 +39,11 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
3039
userAndGroupRegex = regexp.MustCompile(`([A-Za-z0-9]+)(?::([A-Za-z0-9]+))?`)
3140
users []string
3241
groups []string
42+
formatter = cliui.NewOutputFormatter(
43+
cliui.TableFormat(
44+
[]workspaceShareRow{}, []string{"User", "Group", "Role"}),
45+
cliui.JSONFormat(),
46+
)
3347
)
3448

3549
cmd := &serpent.Command{
@@ -122,7 +136,36 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
122136
return err
123137
}
124138

125-
return nil
139+
workspaceAcl, err := client.WorkspaceACL(inv.Context(), workspace.ID)
140+
if err != nil {
141+
return xerrors.Errorf("Could not fetch current workspace ACL after sharing %w", err)
142+
}
143+
144+
outputRows := make([]workspaceShareRow, 0)
145+
for _, user := range workspaceAcl.Users {
146+
outputRows = append(outputRows, workspaceShareRow{
147+
User: user.Username,
148+
Group: "-",
149+
Role: user.Role,
150+
})
151+
}
152+
for _, group := range workspaceAcl.Groups {
153+
for _, user := range group.Members {
154+
outputRows = append(outputRows, workspaceShareRow{
155+
User: user.Username,
156+
Group: group.Name,
157+
Role: group.Role,
158+
})
159+
}
160+
}
161+
162+
out, err := formatter.Format(inv.Context(), outputRows)
163+
if err != nil {
164+
return err
165+
}
166+
167+
_, err = fmt.Fprintln(inv.Stdout, out)
168+
return err
126169
},
127170
}
128171

0 commit comments

Comments
 (0)