-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.go
100 lines (85 loc) · 1.58 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
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
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"unicode/utf8"
"github.com/cespare/xxhash/v2"
)
func isCmsRoot(root string) bool {
for _, testPath := range cmsPaths {
full := root + testPath
if pathExists(full) {
return true
}
}
return false
}
var normalizeRx = []*regexp.Regexp{
regexp.MustCompile(`'reference' => '[a-f0-9]{40}',`),
}
func normalizeLine(b []byte) []byte {
// Also strip slashes comments etc
b = bytes.TrimSpace(b)
for _, prefix := range skipLines {
if bytes.HasPrefix(b, prefix) {
return []byte{}
}
}
for _, rx := range normalizeRx {
b = rx.ReplaceAllLiteral(b, nil)
}
return b
}
func pathExists(p string) bool {
_, err := os.Stat(p)
return err == nil
}
func hasValidExt(path string) bool {
got := strings.TrimLeft(filepath.Ext(path), ".")
for _, want := range scanExts {
if got == want {
return true
}
}
return false
}
func isValidUtf8(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
defer f.Close()
bytes := make([]byte, 1024*8) // 8 KB
if _, err := f.Read(bytes); err != nil {
return false
}
valid := utf8.Valid(bytes)
if !valid {
fmt.Println("Invalid UTF-8:", path)
}
return valid
}
func logVerbose(a ...interface{}) {
if logLevel >= 3 {
fmt.Println(a...)
}
}
func hash(b []byte) uint64 {
return xxhash.Sum64(b)
}
func pathHash(p string) uint64 {
return hash([]byte("path:" + p))
}
func pathIsExcluded(p string) bool {
// Does p match any of excludePaths ?
for _, xx := range excludePaths {
if xx.Match(p) {
return true
}
}
return false
}