@@ -67,67 +67,85 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
67
67
},
68
68
Middleware : serpent .Chain (
69
69
r .InitClient (client ),
70
- serpent .RequireRangeArgs (1 , - 1 ),
70
+ // serpent.RequireRangeArgs(1, -1),
71
71
),
72
72
Handler : func (inv * serpent.Invocation ) error {
73
73
workspace , err := namedWorkspace (inv .Context (), client , inv .Args [0 ])
74
74
if err != nil {
75
75
return xerrors .Errorf ("could not fetch the workspace %s: %w" , inv .Args [0 ], err )
76
76
}
77
77
78
- userRoles := make (map [string ]codersdk.WorkspaceRole , len (users ))
79
- for _ , user := range users {
80
- userAndRole := userAndGroupRegex .FindStringSubmatch (user )
81
- username := userAndRole [1 ]
82
- role := userAndRole [2 ]
78
+ org , err := orgContext .Selected (inv , client )
79
+ if err != nil {
80
+ return err
81
+ }
83
82
84
- user , err := client .User (inv .Context (), username )
83
+ userRoles := make (map [string ]codersdk.WorkspaceRole , len (users ))
84
+ if len (users ) > 0 {
85
+ orgMembers , err := client .OrganizationMembers (inv .Context (), org .ID )
85
86
if err != nil {
86
87
return err
87
88
}
88
89
89
- workspaceRole , err := stringToWorkspaceRole (role )
90
- if err != nil {
91
- return err
92
- }
90
+ for _ , user := range users {
91
+ userAndRole := userAndGroupRegex .FindStringSubmatch (user )
92
+ username := userAndRole [1 ]
93
+ role := userAndRole [2 ]
94
+
95
+ userId := ""
96
+ for _ , member := range orgMembers {
97
+ if member .Username == username {
98
+ userId = member .UserID .String ()
99
+ break
100
+ }
101
+ }
102
+ if userId == "" {
103
+ return xerrors .Errorf ("Could not find user %s in the organization %s" , username , org .Name )
104
+ }
105
+
106
+ workspaceRole , err := stringToWorkspaceRole (role )
107
+ if err != nil {
108
+ return err
109
+ }
93
110
94
- userRoles [user .ID .String ()] = workspaceRole
111
+ userRoles [userId ] = workspaceRole
112
+ }
95
113
}
96
114
97
115
groupRoles := make (map [string ]codersdk.WorkspaceRole )
116
+ if len (groups ) > 0 {
117
+ orgGroups , err := client .Groups (inv .Context (), codersdk.GroupArguments {
118
+ Organization : org .ID .String (),
119
+ })
120
+ if err != nil {
121
+ return err
122
+ }
98
123
99
- org , err := orgContext .Selected (inv , client )
100
- if err != nil {
101
- return err
102
- }
103
- orgGroups , err := client .Groups (inv .Context (), codersdk.GroupArguments {
104
- Organization : org .ID .String (),
105
- })
124
+ for _ , group := range groups {
125
+ groupAndRole := userAndGroupRegex .FindStringSubmatch (group )
126
+ groupName := groupAndRole [1 ]
127
+ role := groupAndRole [2 ]
128
+
129
+ var orgGroup * codersdk.Group
130
+ for _ , g := range orgGroups {
131
+ if g .Name == groupName {
132
+ orgGroup = & g
133
+ break
134
+ }
135
+ }
106
136
107
- for _ , group := range groups {
108
- groupAndRole := userAndGroupRegex .FindStringSubmatch (group )
109
- groupName := groupAndRole [1 ]
110
- role := groupAndRole [2 ]
137
+ if orgGroup == nil {
138
+ return xerrors .Errorf ("Could not find group named %s belonging to the organization %s" , groupName , org .Name )
139
+ }
111
140
112
- var orgGroup * codersdk.Group
113
- for _ , g := range orgGroups {
114
- if g .Name == groupName {
115
- orgGroup = & g
116
- break
141
+ workspaceRole , err := stringToWorkspaceRole (role )
142
+ if err != nil {
143
+ return err
117
144
}
118
- }
119
145
120
- if orgGroup == nil {
121
- return xerrors .Errorf ("Could not find group named %s belonging to the organization %s" , groupName , org .Name )
122
- }
146
+ groupRoles [orgGroup .ID .String ()] = workspaceRole
123
147
124
- workspaceRole , err := stringToWorkspaceRole (role )
125
- if err != nil {
126
- return err
127
148
}
128
-
129
- groupRoles [orgGroup .ID .String ()] = workspaceRole
130
-
131
149
}
132
150
133
151
err = client .UpdateWorkspaceACL (inv .Context (), workspace .ID , codersdk.UpdateWorkspaceACL {
0 commit comments