-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (70 loc) · 1.6 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
)
var basepath = flag.String("path", ".", "the path to scan")
var exclude = flag.String("exclude", ".git", "path prefixes to exclude from the scan")
func processFile(path string) {
file, err := os.Open(filepath.Join(*basepath, path))
if err != nil {
log.Printf("error reading %s: %s", path, err)
}
scanner := bufio.NewScanner(file)
lineno := 0
for scanner.Scan() {
lineno++
line := scanner.Text()
for k, v := range dictionary {
if strings.Contains(line, k) {
fmt.Printf("%s:%d: %s: '%s' should be spelled '%s'\n", path, lineno, v.ID, k, v.CorrectSpelling)
}
}
}
}
func main() {
flag.Parse()
log.SetPrefix("")
ch := make(chan string)
wg := sync.WaitGroup{}
// Read through all the paths in the channel
// and process them to look for matching terms
go func() {
for fpath := range ch {
go func(fpath string) {
processFile(fpath)
wg.Done()
}(fpath)
}
}()
prefixes := strings.Split(*exclude, ",")
// Walk the filesystem and place each path into the channel
filepath.Walk(*basepath, func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
relpath, err := filepath.Rel(*basepath, path)
if err != nil {
panic(err)
}
for _, prefix := range prefixes {
if strings.HasPrefix(relpath, prefix) {
return nil
}
}
wg.Add(1)
ch <- relpath
return nil
})
// Close the channel to signal that there are no more
// paths that will be added
close(ch)
// Wait until all the paths have been processed
wg.Wait()
}