-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.go
46 lines (39 loc) · 1.1 KB
/
eval.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
package main
import (
"fmt"
"strings"
)
func Evaluate(login string, entity string, userRole string, roles []string, friendly bool) (err error) {
// If no roles were specified, print the role
if len(roles) == 0 {
_succeed(login, entity, userRole, friendly)
return
}
// Otherwise, check if the user has one of the specified roles
for _, role := range roles {
if strings.EqualFold(userRole, role) {
_succeed(login, entity, userRole, friendly)
return
}
}
// If we got here, the user doesn't have any of the specified roles
return _fail(login, entity, userRole, roles)
}
func _succeed(login string, entity string, userRole string, friendly bool) {
roleString := strings.ToLower(userRole)
if friendly {
fmt.Printf("%s has %s role in %s.\n", login, roleString, entity)
} else {
fmt.Println(roleString)
}
}
func _fail(login string, entity string, userRole string, checkedRoles []string) error {
s := ""
if len(checkedRoles) > 1 {
s = "s"
}
return fmt.Errorf(
"%s does not have role%s in %s: %s; found %s",
login, s, entity, strings.Join(checkedRoles, ", "), strings.ToLower(userRole),
)
}