-
Notifications
You must be signed in to change notification settings - Fork 0
/
gospector.go
147 lines (130 loc) · 3.33 KB
/
gospector.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
package main
import (
"bufio"
"io"
"os"
"path/filepath"
"strings"
"fmt"
"github.com/syndtr/goleveldb/leveldb/errors"
)
type gospector struct {
dir string
config gospectorConf
extToWords map[string]([]string)
extToTrailing map[string](bool)
}
func createGospector(dir string, config gospectorConf) gospector {
g := gospector{
dir: dir,
config: config,
extToWords: make(map[string]([]string), len(config.Rules)),
extToTrailing: make(map[string](bool), len(config.Rules)),
}
for _, rule := range g.config.Rules {
for _, ext := range rule.Extensions {
if words, ok := g.extToWords[ext]; ok {
g.extToWords[ext] = append(words, rule.Words...)
} else {
g.extToWords[ext] = rule.Words
}
g.extToTrailing[ext] = rule.Trailing
}
}
return g
}
func (g *gospector) execute() []error {
return g.executeDir(g.dir, true)
}
func (g *gospector) executeDir(dir string, checkFilesCurrentDir bool) []error {
errArr := []error{}
files, err := filepath.Glob(dir + "/*")
if err != nil {
return append(errArr, err)
}
for _, fileName := range files {
fileStat, err := os.Stat(fileName)
if err != nil {
continue
}
if fileStat.IsDir() {
shouldGoDir, checkFilesIterDir := g.shouldExecuteDir(fileName)
if shouldGoDir {
errRet := g.executeDir(fileName, checkFilesIterDir)
errArr = append(errArr, errRet...)
}
} else {
if checkFilesCurrentDir && g.shouldExecuteFile(fileName) {
errRet := g.executeFile(fileName)
errArr = append(errArr, errRet...)
}
}
}
return errArr
}
func (g *gospector) executeFile(file string) []error {
errArr := []error{}
fileOpened, err := os.Open(file)
if err != nil {
return append(errArr, err)
}
defer fileOpened.Close()
fileExt := filepath.Ext(file)
words := g.extToWords[fileExt]
trailing := g.extToTrailing[fileExt]
reader := bufio.NewReader(fileOpened)
lineNumber := 0
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
lineNumber++
if trailing && strings.HasSuffix(line, " \n") {
errorMessage := fmt.Sprintf("%s => trailing space found %d", file, lineNumber)
errArr = append(errArr, errors.New(errorMessage))
}
for _, word := range words {
if strings.Contains(line, word) {
errorMessage := fmt.Sprintf("%s => %s found on line %d", file, word, lineNumber)
errArr = append(errArr, errors.New(errorMessage))
}
}
}
return errArr
}
func (g *gospector) shouldExecuteFile(file string) bool {
fileExt := filepath.Ext(file)
for _, rule := range g.config.Rules {
for _, ext := range rule.Extensions {
if fileExt == ext {
return true
}
}
}
return false
}
/*shouldExecuteDir the first return means if should go deeper in the
directory and the second return means if you should exec the checker
on files in this directory*/
func (g *gospector) shouldExecuteDir(dir string) (bool, bool) {
if len(g.config.Subdirs) == 0 && len(g.config.Excluded) == 0 {
return true, true
}
for _, ex := range g.config.Excluded {
fullSubdir := g.dir + "/" + ex
if strings.LastIndex(dir, fullSubdir) == 0 {
return false, false
}
}
for _, subdir := range g.config.Subdirs {
fullSubdir := g.dir + "/" + subdir
if strings.LastIndex(dir, fullSubdir) == 0 {
return true, true
}
if strings.LastIndex(fullSubdir, dir) == 0 {
return true, false
}
}
return false, false
}