@@ -38,11 +38,11 @@ func (r *RootCmd) sharing() *serpent.Command {
38
38
func (r * RootCmd ) shareWorkspace (orgContext * OrganizationContext ) * serpent.Command {
39
39
var (
40
40
// Username regex taken from codersdk/name.go
41
- userAndGroupRegex = regexp .MustCompile (`(^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)+(?::([A-Za-z0-9-]+))?` )
42
- client = new (codersdk.Client )
43
- users []string
44
- groups []string
45
- formatter = cliui .NewOutputFormatter (
41
+ nameRoleRegex = regexp .MustCompile (`(^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)+(?::([A-Za-z0-9-]+))?` )
42
+ client = new (codersdk.Client )
43
+ users []string
44
+ groups []string
45
+ formatter = cliui .NewOutputFormatter (
46
46
cliui .TableFormat (
47
47
[]workspaceShareRow {}, []string {"User" , "Group" , "Role" }),
48
48
cliui .JSONFormat (),
@@ -92,7 +92,7 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
92
92
}
93
93
94
94
for _ , user := range users {
95
- userAndRole := userAndGroupRegex .FindStringSubmatch (user )
95
+ userAndRole := nameRoleRegex .FindStringSubmatch (user )
96
96
username := userAndRole [1 ]
97
97
role := userAndRole [2 ]
98
98
@@ -126,7 +126,7 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
126
126
}
127
127
128
128
for _ , group := range groups {
129
- groupAndRole := userAndGroupRegex .FindStringSubmatch (group )
129
+ groupAndRole := nameRoleRegex .FindStringSubmatch (group )
130
130
groupName := groupAndRole [1 ]
131
131
role := groupAndRole [2 ]
132
132
@@ -203,14 +203,13 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
203
203
}
204
204
205
205
func stringToWorkspaceRole (role string ) (codersdk.WorkspaceRole , error ) {
206
- if role != "" && role != string (codersdk .WorkspaceRoleAdmin ) && role != string (codersdk .WorkspaceRoleUse ) {
207
- return "" , xerrors .Errorf ("invalid role %s. Expected %s, or %s" , role , codersdk .WorkspaceRoleAdmin , codersdk .WorkspaceRoleUse )
206
+ switch role {
207
+ case "" , string (codersdk .WorkspaceRoleUse ):
208
+ return codersdk .WorkspaceRoleUse , nil
209
+ case string (codersdk .WorkspaceRoleAdmin ):
210
+ return codersdk .WorkspaceRoleAdmin , nil
211
+ default :
212
+ return "" , xerrors .Errorf ("invalid role %q: expected %q or %q" ,
213
+ role , codersdk .WorkspaceRoleAdmin , codersdk .WorkspaceRoleUse )
208
214
}
209
-
210
- workspaceRole := codersdk .WorkspaceRoleUse
211
- if role == string (codersdk .WorkspaceRoleAdmin ) {
212
- workspaceRole = codersdk .WorkspaceRoleAdmin
213
- }
214
-
215
- return workspaceRole , nil
216
215
}
0 commit comments