forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
102 lines (92 loc) · 2.56 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package userprint
import (
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/plugin/models"
)
type pluginPrinter struct {
roles []models.Role
userLister func(spaceGUID string, role models.Role) ([]models.UserFields, error)
users userCollection
printer func([]userWithRoles)
}
type userCollection map[string]userWithRoles
type userWithRoles struct {
models.UserFields
Roles []models.Role
}
func NewOrgUsersPluginPrinter(
pluginModel *[]plugin_models.GetOrgUsers_Model,
userLister func(guid string, role models.Role) ([]models.UserFields, error),
roles []models.Role,
) *pluginPrinter {
return &pluginPrinter{
users: make(userCollection),
userLister: userLister,
roles: roles,
printer: func(users []userWithRoles) {
var orgUsers []plugin_models.GetOrgUsers_Model
for _, user := range users {
orgUsers = append(orgUsers, plugin_models.GetOrgUsers_Model{
Guid: user.GUID,
Username: user.Username,
IsAdmin: user.IsAdmin,
Roles: rolesToString(user.Roles),
})
}
*pluginModel = orgUsers
},
}
}
func NewSpaceUsersPluginPrinter(
pluginModel *[]plugin_models.GetSpaceUsers_Model,
userLister func(guid string, role models.Role) ([]models.UserFields, error),
roles []models.Role,
) *pluginPrinter {
return &pluginPrinter{
users: make(userCollection),
userLister: userLister,
roles: roles,
printer: func(users []userWithRoles) {
var spaceUsers []plugin_models.GetSpaceUsers_Model
for _, user := range users {
spaceUsers = append(spaceUsers, plugin_models.GetSpaceUsers_Model{
Guid: user.GUID,
Username: user.Username,
IsAdmin: user.IsAdmin,
Roles: rolesToString(user.Roles),
})
}
*pluginModel = spaceUsers
},
}
}
func (p *pluginPrinter) PrintUsers(guid string, username string) {
for _, role := range p.roles {
users, _ := p.userLister(guid, role)
for _, user := range users {
p.users.storeAppendingRole(role, user.Username, user.GUID, user.IsAdmin)
}
}
p.printer(p.users.all())
}
func (coll userCollection) storeAppendingRole(role models.Role, username string, guid string, isAdmin bool) {
u := coll[username]
u.Roles = append(u.Roles, role)
u.Username = username
u.GUID = guid
u.IsAdmin = isAdmin
coll[username] = u
}
func (coll userCollection) all() (output []userWithRoles) {
for _, u := range coll {
output = append(output, u)
}
return output
}
func rolesToString(roles []models.Role) []string {
var rolesStr []string
for _, role := range roles {
rolesStr = append(rolesStr, role.ToString())
}
return rolesStr
}