-
Notifications
You must be signed in to change notification settings - Fork 234
/
helpers.go
69 lines (58 loc) · 1.15 KB
/
helpers.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
package sourcecode
import (
"bufio"
"os"
"path/filepath"
"regexp"
)
func fileExists(filenames ...string) checkFn {
return func(dir string) bool {
for _, filename := range filenames {
info, err := os.Stat(filepath.Join(dir, filename))
if err != nil {
continue
}
if !info.IsDir() {
return true
}
}
return false
}
}
func fileContains(path string, pattern string) bool {
file, err := os.Open(path)
if err != nil {
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
re := regexp.MustCompile(pattern)
if re.MatchString(scanner.Text()) {
return true
}
}
return false
}
func dirContains(glob string, patterns ...string) checkFn {
return func(dir string) bool {
for _, pattern := range patterns {
filenames, _ := filepath.Glob(filepath.Join(dir, glob))
for _, filename := range filenames {
if fileContains(filename, pattern) {
return true
}
}
}
return false
}
}
type checkFn func(dir string) bool
func checksPass(sourceDir string, checks ...checkFn) bool {
for _, check := range checks {
if check(sourceDir) {
return true
}
}
return false
}