1
1
package cli
2
2
3
3
import (
4
+ "fmt"
4
5
"slices"
5
6
"sort"
6
7
"strings"
7
8
8
9
"golang.org/x/xerrors"
9
10
10
11
"github.com/coder/coder/v2/cli/cliui"
12
+ "github.com/coder/coder/v2/coderd/rbac"
11
13
"github.com/coder/coder/v2/codersdk"
12
14
"github.com/coder/serpent"
13
15
)
14
16
15
17
func (r * RootCmd ) userEditRoles () * serpent.Command {
16
18
client := new (codersdk.Client )
17
19
20
+ roles := rbac .SiteRoles ()
21
+
22
+ siteRoles := make ([]string , 0 )
23
+ for _ , role := range roles {
24
+ siteRoles = append (siteRoles , role .Identifier .Name )
25
+ }
26
+ sort .Strings (siteRoles )
27
+
18
28
var givenRoles []string
19
29
20
30
cmd := & serpent.Command {
@@ -24,7 +34,7 @@ func (r *RootCmd) userEditRoles() *serpent.Command {
24
34
cliui .SkipPromptOption (),
25
35
{
26
36
Name : "roles" ,
27
- Description : "A list of roles to give to the user. This removes any existing roles the user may have." ,
37
+ Description : fmt . Sprintf ( "A list of roles to give to the user. This removes any existing roles the user may have. The available roles are: %s." , strings . Join ( siteRoles , ", " )) ,
28
38
Flag : "roles" ,
29
39
Value : serpent .StringArrayOf (& givenRoles )},
30
40
},
@@ -37,42 +47,22 @@ func (r *RootCmd) userEditRoles() *serpent.Command {
37
47
return xerrors .Errorf ("fetch user: %w" , err )
38
48
}
39
49
40
- roles , err := client .ListSiteRoles (ctx )
41
- if err != nil {
42
- return xerrors .Errorf ("fetch site roles: %w" , err )
43
- }
44
-
45
- siteRoles := make ([]string , 0 )
46
- for _ , role := range roles {
47
- if role .Assignable {
48
- siteRoles = append (siteRoles , role .Name )
49
- }
50
- }
51
- sort .Strings (siteRoles )
52
-
53
50
userRoles , err := client .UserRoles (ctx , user .Username )
54
51
if err != nil {
55
52
return xerrors .Errorf ("fetch user roles: %w" , err )
56
53
}
57
54
58
55
var selectedRoles []string
59
56
if len (givenRoles ) > 0 {
60
- // If the none role is present ignore all other roles.
61
- // This is so there is a way to clear roles from the CLI without making a
62
- // new command.
63
- if slices .Contains (givenRoles , "none" ) {
64
- selectedRoles = []string {}
65
- } else {
66
- // Make sure all of the given roles are valid site roles
67
- for _ , givenRole := range givenRoles {
68
- if ! slices .Contains (siteRoles , givenRole ) {
69
- siteRolesPretty := strings .Join (siteRoles , ", " )
70
- return xerrors .Errorf ("The role %s is not valid. Please use one or more of the following roles: %s, or none\n " , givenRole , siteRolesPretty )
71
- }
57
+ // Make sure all of the given roles are valid site roles
58
+ for _ , givenRole := range givenRoles {
59
+ if ! slices .Contains (siteRoles , givenRole ) {
60
+ siteRolesPretty := strings .Join (siteRoles , ", " )
61
+ return xerrors .Errorf ("The role %s is not valid. Please use one or more of the following roles: %s\n " , givenRole , siteRolesPretty )
72
62
}
73
-
74
- selectedRoles = givenRoles
75
63
}
64
+
65
+ selectedRoles = givenRoles
76
66
} else {
77
67
selectedRoles , err = cliui .MultiSelect (inv , cliui.MultiSelectOptions {
78
68
Message : "Select the roles you'd like to assign to the user" ,
0 commit comments