-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclusions.go
102 lines (86 loc) · 1.89 KB
/
inclusions.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
package main
import (
"fmt"
"regexp"
"strings"
)
type criteria struct {
text string
pkgExpr *regexp.Regexp
typeExpr *regexp.Regexp
}
var inclusions []criteria
var exclusions []criteria
func addInclusion(s string) error {
c, err := compileCriteria(s)
if err != nil {
return err
}
inclusions = append(inclusions, c)
return nil
}
func addExclusion(s string) error {
c, err := compileCriteria(s)
if err != nil {
return err
}
exclusions = append(exclusions, c)
return nil
}
func include(pkgName string, typeName string) bool {
for _, exc := range exclusions {
if matches(exc, pkgName, typeName) {
fmt.Println("Excluded", pkgName, typeName)
return false // It's excluded
}
}
if _, found := prog.Imported[pkgName]; found {
return true
}
for _, inc := range inclusions {
if matches(inc, pkgName, typeName) {
return true // It's included
}
}
return false
}
func matches(c criteria, pkgName string, typeName string) bool {
//fmt.Printf("matches(%q, %q): %s [%v] %s [%v]\n",
// pkgName, typeName,
// c.pkgExpr, !(c.pkgExpr != nil && c.pkgExpr.FindString(pkgName) != pkgName),
// c.typeExpr, !(c.typeExpr != nil && c.typeExpr.FindString(typeName) != typeName))
if c.pkgExpr != nil && c.pkgExpr.FindString(pkgName) != pkgName {
return false
}
if c.typeExpr != nil && c.typeExpr.FindString(typeName) != typeName {
return false
}
return true
}
func compileCriteria(s string) (criteria, error) {
var (
err error
pkg, typ string
c = criteria{text: s}
)
// First split into pkgPattern:TypePattern (both parts optional).
if ind := strings.LastIndexByte(s, ':'); ind == -1 {
pkg = s
} else {
pkg = s[:ind]
typ = s[(ind + 1):]
}
if len(pkg) > 0 {
c.pkgExpr, err = regexp.Compile(pkg)
if err != nil {
return c, err
}
}
if len(typ) > 0 {
c.typeExpr, err = regexp.Compile(typ)
if err != nil {
return c, err
}
}
return c, nil
}