-
Notifications
You must be signed in to change notification settings - Fork 4
/
UserGroupHelper.go
54 lines (49 loc) · 1.14 KB
/
UserGroupHelper.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
package util
import (
"strings"
)
func ParseGroupOwnerAndGroupName(mayBeGroupReference string) (bool, string, string) {
hasGroupPrefix := strings.HasPrefix(mayBeGroupReference, "@")
indexOfDot := strings.Index(mayBeGroupReference, ".")
if hasGroupPrefix && indexOfDot > 0 {
return true, mayBeGroupReference[1:indexOfDot], mayBeGroupReference[indexOfDot+1:]
}
return false, "", ""
}
func SplitUsersAndGroups(userAndGroups []string) ([]string, []string) {
users := make([]string, 0)
groups := make([]string, 0)
for _, userOrGroup := range userAndGroups {
isGroup, _, _ := ParseGroupOwnerAndGroupName(userOrGroup)
if isGroup {
groups = append(groups, userOrGroup)
} else {
users = append(users, userOrGroup)
}
}
return users, groups
}
func Equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func Difference(a, b []string) []string {
mb := make(map[string]struct{}, len(b))
for _, x := range b {
mb[x] = struct{}{}
}
var diff []string
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
}
}
return diff
}