-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
133 lines (111 loc) · 2.75 KB
/
Copy pathmain.go
File metadata and controls
133 lines (111 loc) · 2.75 KB
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
package main
import (
"bufio"
"fmt"
"io/fs"
"path/filepath"
"strings"
"sync"
"time"
"github.com/MonkyMars/ccheck/handling"
"github.com/MonkyMars/ccheck/terminal"
"github.com/MonkyMars/ccheck/validate"
)
var (
blacklistedDirs = map[string]bool{
".git": true,
"node_modules": true,
"target": true,
}
results = make(chan string)
resultsLen = 0
)
func main() {
patterns, root, extList, outputFile := handling.ParseArgs()
// Start timer
startTime := time.Now()
var wg sync.WaitGroup
var outputWg sync.WaitGroup
// Start a goroutine to handle output
outputWg.Go(func() {
handling.OutputToFile(outputFile, results)
})
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
fmt.Println(handling.PrintError(err.Error(), "file should be accessible"))
return nil
}
if d.IsDir() {
if !validate.IsValidDir(path, blacklistedDirs) {
return filepath.SkipDir
}
return nil
}
matches := false
for e := range extList {
if validate.IsValidExtension(d.Name(), e) {
matches = true
break
}
}
if !matches {
return nil
}
// Avoid processing the output file for an infinite loop
if outputFile != nil && path == outputFile.Name() {
return nil
}
// Ensure path is inside root
rel, err := filepath.Rel(root, path)
if err != nil || strings.HasPrefix(rel, "..") {
return nil // skip paths outside root
}
wg.Add(1)
go func(path string) {
defer wg.Done()
file, err := handling.OpenFile(filepath.Clean(path))
// Check for errors and nil file
if err != nil || file == nil {
if err.Error() != handling.ErrorBinaryFile.Error() {
fmt.Println(handling.PrintError(err.Error(), "file should be accessible"))
}
return
}
defer file.Close()
// Read file line by line
scanner := bufio.NewScanner(file)
lineNum := 1
for scanner.Scan() {
line := scanner.Text()
for _, p := range patterns {
if p.MatchString(line) {
if outputFile != nil {
message := fmt.Sprintf("%s:%d: %s\n", path, lineNum, line)
results <- message
resultsLen++
} else {
message := terminal.FormatMatchLine(path, lineNum, line, p)
results <- message + "\n"
resultsLen++
}
// Do not break for loop; another pattern might also match
}
}
lineNum++
}
if err := scanner.Err(); err != nil {
fmt.Println(handling.PrintError(err.Error(), "error reading file"))
}
}(path)
return nil
})
wg.Wait()
close(results)
outputWg.Wait()
elapsed := time.Since(startTime)
fmt.Println("ccheck 2.3.1")
fmt.Printf("Found %d results in %s\n", resultsLen, elapsed)
if err != nil {
fmt.Println(handling.PrintError(err.Error(), "error walking the directory"))
}
}