1
1
package cli
2
2
3
3
import (
4
+ "fmt"
5
+ "regexp"
6
+
7
+ "github.com/coder/coder/v2/cli/cliui"
4
8
"github.com/coder/coder/v2/codersdk"
5
9
"github.com/coder/serpent"
6
10
"golang.org/x/xerrors"
7
- "regexp"
8
11
)
9
12
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
+
10
19
func (r * RootCmd ) sharing () * serpent.Command {
11
20
orgContext := NewOrganizationContext ()
12
21
@@ -30,6 +39,11 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
30
39
userAndGroupRegex = regexp .MustCompile (`([A-Za-z0-9]+)(?::([A-Za-z0-9]+))?` )
31
40
users []string
32
41
groups []string
42
+ formatter = cliui .NewOutputFormatter (
43
+ cliui .TableFormat (
44
+ []workspaceShareRow {}, []string {"User" , "Group" , "Role" }),
45
+ cliui .JSONFormat (),
46
+ )
33
47
)
34
48
35
49
cmd := & serpent.Command {
@@ -122,7 +136,36 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
122
136
return err
123
137
}
124
138
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
126
169
},
127
170
}
128
171
0 commit comments