-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
55 lines (44 loc) · 1.14 KB
/
scope.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
package docker
import (
"fmt"
"regexp"
"strings"
)
var typeRegexp = regexp.MustCompile(`^([a-z0-9]+)(\([a-z0-9]+\))?$`)
type Scope struct {
Type string `json:"type"`
Class string `json:"class,omitempty"`
Name string `json:"name"`
Actions []string `json:"actions"`
}
func ParseScope(raw string) (scopes []Scope, err error) {
rawRequests := strings.Split(raw, " ")
for _, r := range rawRequests {
parts := strings.Split(r, ":")
if len(parts) != 3 {
return nil, fmt.Errorf("invalid scope: %s", r)
}
resourceType, resourceName, actions := parts[0], parts[1], parts[2]
resourceType, resourceClass := splitResourceClass(resourceType)
if resourceType == "" {
continue
}
scopes = append(scopes, Scope{
Type: resourceType,
Class: resourceClass,
Name: resourceName,
Actions: strings.Split(actions, ","),
})
}
return scopes, err
}
func splitResourceClass(t string) (string, string) {
matches := typeRegexp.FindStringSubmatch(t)
if len(matches) < 2 {
return "", ""
}
if len(matches) == 2 || len(matches[2]) < 2 {
return matches[1], ""
}
return matches[1], matches[2][1 : len(matches[2])-1]
}