-
Notifications
You must be signed in to change notification settings - Fork 4
/
impi.go
255 lines (201 loc) · 6.06 KB
/
impi.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package impi
import (
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"github.com/kisielk/gotool"
)
// Impi is a single instance that can perform verification on a path
type Impi struct {
numWorkers int
resultChan chan interface{}
filePathsChan chan string
stopChan chan bool
verifyOptions *VerifyOptions
SkipPathRegexes []*regexp.Regexp
}
// ImportGroupVerificationScheme specifies what to check when inspecting import groups
type ImportGroupVerificationScheme int
const (
// ImportGroupVerificationSchemeSingle allows for a single, sorted group
ImportGroupVerificationSchemeSingle = ImportGroupVerificationScheme(iota)
// ImportGroupVerificationSchemeStdNonStd allows for up to two groups in the following order:
// - standard imports
// - non-standard imports
ImportGroupVerificationSchemeStdNonStd
// ImportGroupVerificationSchemeStdLocalThirdParty allows for up to three groups in the following order:
// - standard imports
// - local imports (where local prefix is specified in verification options)
// - non-standard imports
ImportGroupVerificationSchemeStdLocalThirdParty
// ImportGroupVerificationSchemeStdThirdPartyLocal allows for up to three groups in the following order:
// - standard imports
// - non-standard imports
// - local imports (where local prefix is specified in verification options)
ImportGroupVerificationSchemeStdThirdPartyLocal
)
// VerifyOptions specifies how to perform verification
type VerifyOptions struct {
SkipTests bool
Scheme ImportGroupVerificationScheme
LocalPrefix string
SkipPaths []string
IgnoreGenerated bool
}
// VerificationError holds an error and a file path on which the error occurred
type VerificationError struct {
error
FilePath string
}
// ErrorReporter receives error reports as they are detected by the workers
type ErrorReporter interface {
Report(VerificationError)
}
// NewImpi creates a new impi instance
func NewImpi(numWorkers int) (*Impi, error) {
newImpi := &Impi{
numWorkers: numWorkers,
resultChan: make(chan interface{}, 1024),
filePathsChan: make(chan string),
stopChan: make(chan bool),
}
return newImpi, nil
}
// Verify will iterate over the path and start verifying import correctness within
// all .go files in the path. Path follows go tool semantics (e.g. ./...)
func (i *Impi) Verify(rootPath string, verifyOptions *VerifyOptions, errorReporter ErrorReporter) error {
// save stuff for current session
i.verifyOptions = verifyOptions
// compile skip regex
for _, skipPath := range verifyOptions.SkipPaths {
skipPathRegex, err := regexp.Compile(skipPath)
if err != nil {
return err
}
i.SkipPathRegexes = append(i.SkipPathRegexes, skipPathRegex)
}
// spin up the workers do handle all the data in the channel. workers will die
if err := i.createWorkers(i.numWorkers); err != nil {
return err
}
// populate paths channel from path. paths channel will contain .go source file paths
if err := i.populatePathsChan(rootPath); err != nil {
return err
}
// wait for worker completion. if an error was reported, return error
if numErrors := i.waitWorkerCompletion(errorReporter); numErrors != 0 {
return fmt.Errorf("Found %d errors", numErrors)
}
return nil
}
func (i *Impi) populatePathsChan(rootPath string) error {
// TODO: this should be done in parallel
// get all the packages in the root path, following go 1.9 semantics
packagePaths := gotool.ImportPaths([]string{rootPath})
if len(packagePaths) == 0 {
return fmt.Errorf("Could not find packages in %s", packagePaths)
}
// iterate over these paths:
// - for files, just shove to paths
// - for dirs, find all go sources
for _, packagePath := range packagePaths {
if isDir(packagePath) {
// iterate over files in directory
fileInfos, err := ioutil.ReadDir(packagePath)
if err != nil {
return err
}
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
continue
}
i.addFilePathToFilePathsChan(path.Join(packagePath, fileInfo.Name()))
}
} else {
// shove path to channel if passes filter
i.addFilePathToFilePathsChan(packagePath)
}
}
// close the channel to signify we won't add any more data
close(i.filePathsChan)
return nil
}
func (i *Impi) waitWorkerCompletion(errorReporter ErrorReporter) int {
numWorkersComplete := 0
numErrorsReported := 0
for result := range i.resultChan {
switch typedResult := result.(type) {
case VerificationError:
errorReporter.Report(typedResult)
numErrorsReported++
case bool:
numWorkersComplete++
}
// if we're done, break the loop
if numWorkersComplete == i.numWorkers {
break
}
}
return numErrorsReported
}
func (i *Impi) createWorkers(numWorkers int) error {
for workerIndex := 0; workerIndex < numWorkers; workerIndex++ {
go i.verifyPathsFromChan()
}
return nil
}
func (i *Impi) verifyPathsFromChan() error {
// create a verifier with which we'll verify modules
verifier, err := newVerifier()
if err != nil {
return err
}
// while we're not done
for filePath := range i.filePathsChan {
// open the file
file, err := os.Open(filePath)
if err != nil {
return err
}
// verify the path and report an error if one is found
if err = verifier.verify(file, i.verifyOptions); err != nil {
verificationError := VerificationError{
error: err,
FilePath: filePath,
}
// write to results channel
i.resultChan <- verificationError
}
}
// a boolean in the result chan signifies that we're done
i.resultChan <- true
return nil
}
func isDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}
func (i *Impi) addFilePathToFilePathsChan(filePath string) {
// skip non-go files
if !strings.HasSuffix(filePath, ".go") {
return
}
// skip tests if not desired
if strings.HasSuffix(filePath, "_test.go") && i.verifyOptions.SkipTests {
return
}
// cmd/impi/main.go should check the patters
for _, skipPathRegex := range i.SkipPathRegexes {
if skipPathRegex.Match([]byte(filePath)) {
return
}
}
// write to paths chan
i.filePathsChan <- filePath
}