This repository has been archived by the owner on May 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
check.go
173 lines (146 loc) · 3.69 KB
/
check.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"os"
"regexp"
"sort"
)
type UnnecessaryPackage struct {
Package string
}
func (e *UnnecessaryPackage) Error() string {
return fmt.Sprintf("unnecessary package '%s', please remove from go.cap file", e.Package)
}
type PackageNotProvided struct {
Package string
}
func (e *PackageNotProvided) Error() string {
return fmt.Sprintf("package '%s' not listed in go.cap file, please add to go.cap file", e.Package)
}
type UnnecessaryCapability struct {
Capability string
}
func (e *UnnecessaryCapability) Error() string {
return fmt.Sprintf("unnecessary capability '%s', please remove from go.cap file", e.Capability)
}
type CapabilityNotProvided struct {
Capability string
}
func (e *CapabilityNotProvided) Error() string {
return fmt.Sprintf("capability '%s' not provided by go.cap file, add to go.cap file if you want to grant the capability", e.Capability)
}
func checkCmd(path string, ignore string) {
file, err := parseGoCap(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
tree, err := NewTree(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if ignore != "" {
filterTree(tree, ignore)
file.External = filterFile(file, ignore)
}
packageErrors := check(tree, file)
if len(packageErrors) > 0 {
if rootErrs, ok := packageErrors[tree.root]; ok {
fmt.Println(tree.root)
for _, e := range rootErrs {
fmt.Println("\t" + e.Error())
}
}
for _, name := range sortedPackageErrorKeys(packageErrors) {
if name == tree.root {
continue
}
fmt.Println(name)
for _, e := range packageErrors[name] {
fmt.Println("\t" + e.Error())
}
}
os.Exit(1)
}
}
func filterTree(tree *Tree, ignore string) {
for name := range tree.nodes {
match, err := regexp.MatchString(ignore, name)
if err != nil {
continue
}
if match {
delete(tree.nodes, name)
}
}
}
func filterFile(file *File, ignore string) []Line {
var external []Line
for _, line := range file.External {
match, err := regexp.MatchString(ignore, line.Package)
if err != nil {
continue
}
if !match {
external = append(external, line)
}
}
return external
}
func sortedPackageErrorKeys(m map[string][]error) (keys []string) {
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func check(tree *Tree, file *File) map[string][]error {
packageErrors := map[string][]error{}
for name := range tree.nodes {
errors := checkPackage(name, tree, file)
if len(errors) > 0 {
packageErrors[name] = errors
}
}
for _, line := range file.External {
if _, ok := tree.nodes[line.Package]; !ok {
packageErrors[line.Package] = []error{&UnnecessaryPackage{Package: line.Package}}
}
}
return packageErrors
}
func checkPackage(name string, tree *Tree, file *File) []error {
values := tree.nodes[name].capabilities.Values()
if _, ok := file.expected[name]; ok {
return checkGoCapLine(file.expected[name], values)
}
if len(values) > 0 {
return []error{&PackageNotProvided{Package: name}}
}
return nil
}
func checkGoCapLine(expectedCapabilities []string, currentCapabilities []string) (errors []error) {
seen := map[string]struct{}{}
for _, currentCapability := range currentCapabilities {
if contains(expectedCapabilities, currentCapability) {
seen[currentCapability] = struct{}{}
} else {
errors = append(errors, &CapabilityNotProvided{Capability: currentCapability})
}
}
for _, expectedCapability := range expectedCapabilities {
if _, ok := seen[expectedCapability]; !ok {
errors = append(errors, &UnnecessaryCapability{Capability: expectedCapability})
}
}
return errors
}
func contains(list []string, element string) bool {
for _, e := range list {
if e == element {
return true
}
}
return false
}