forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
94 lines (85 loc) · 2.33 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
package userprint
import (
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/plugin/models"
)
type pluginPrinter struct {
roles []string
userLister func(spaceGuid string, role string) ([]models.UserFields, error)
users userCollection
printer func([]userWithRoles)
}
type userCollection map[string]userWithRoles
type userWithRoles struct {
models.UserFields
Roles []string
}
func NewOrgUsersPluginPrinter(
pluginModel *[]plugin_models.GetOrgUsers_Model,
userLister func(guid string, role string) ([]models.UserFields, error),
roles []string,
) *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: user.Roles,
})
}
*pluginModel = orgUsers
},
}
}
func NewSpaceUsersPluginPrinter(
pluginModel *[]plugin_models.GetSpaceUsers_Model,
userLister func(guid string, role string) ([]models.UserFields, error),
roles []string,
) *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: 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 string, 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
}