-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
164 lines (140 loc) · 3.35 KB
/
find.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
package dowork
import (
"bufio"
"errors"
"os"
"path/filepath"
"strings"
"sync"
)
type Result interface {
unwrap() (Result, error)
}
type SearchWorkList struct {
WorkList
Results *chan Result
}
type FileContentResult struct {
Path string
Line int
Text string
}
func (r *FileContentResult) unwrap() (Result, error) {
return r, nil
}
func NewFilecontentResult(text string, line int, path string) *FileContentResult {
return &FileContentResult{Text: text, Line: line, Path: path}
}
type SearchFileContentJob struct {
path string
find string
results chan<- Result
}
func newFileContentSearch(path, find string, results chan<- Result) *SearchFileContentJob {
return &SearchFileContentJob{path: path, find: find, results: results}
}
// Do method for InFileJob to perform the search
func (job *SearchFileContentJob) Do() error {
file, err := os.Open(job.path)
if err != nil {
return err
}
defer func() {
if r := recover(); r != nil {
panic(r)
}
}()
defer func(file *os.File) {
closErr := file.Close()
if closErr != nil {
// log.Errorf("Error closing file %s: %v\n", job.path, closErr)
panic(closErr)
}
}(file)
scanner := bufio.NewScanner(file)
lineNum := 1
for scanner.Scan() {
if strings.Contains(scanner.Text(), job.find) {
result := NewFilecontentResult(scanner.Text(), lineNum, job.path)
job.results <- result
}
lineNum++
}
return nil
}
// func numberOfFilesInDir(path string) int {
// entries, err := os.ReadDir(path)
// if err != nil {
// fmt.Println("ReadDir error:", err)
// return 0
// }
// return len(entries)
// }
func numberOfFilesInDirRecursive(path string) int {
entries, err := os.ReadDir(path)
if err != nil {
// fmt.Println("ReadDir error:", err)
return 0
}
count := 0
for _, entry := range entries {
if entry.IsDir() {
count += numberOfFilesInDirRecursive(filepath.Join(path, entry.Name()))
} else {
count++
}
}
return count
}
func addFileEntryToJob(entry os.DirEntry, wl *WorkList, results chan<- Result, path, find string) {
path = filepath.Join(path, entry.Name())
if entry.IsDir() {
discoverFileContentSearchDirs(wl, path, find, results)
} else {
job := newFileContentSearch(path, find, results)
wl.Add(job)
}
}
func discoverFileContentSearchDirs(wl *WorkList, path string, find string, results chan<- Result) {
entries, err := os.ReadDir(path)
if err != nil {
// fmt.Println("ReadDir error:", err)
return
}
for _, entry := range entries {
addFileEntryToJob(entry, wl, results, path, find)
}
}
func SearchFileContent(searchTerm, searchDir string) <-chan Result {
var wg sync.WaitGroup
numWorkers := numberOfFilesInDirRecursive(searchDir)
results := make(chan Result, numWorkers) // Adjust buffer size as necessary
wl := NewWorkList(numWorkers)
workerFunc := func() {
defer wg.Done()
for job := range wl.Jobs() { // Assuming Jobs() provides a channel of Job interface
err := job.Do()
var jobsDoneErr *WorkIsDoneError
if errors.As(err, &jobsDoneErr) {
return // work is done
}
// } else if err != nil {
// log.Errorf("Error processing job: %v", err)
// }
}
}
wg.Add(numWorkers + 1)
go func() {
defer wg.Done()
discoverFileContentSearchDirs(wl, searchDir, searchTerm, results)
wl.Finalize(numWorkers)
}()
for i := 0; i < numWorkers; i++ {
go workerFunc()
}
go func() {
wg.Wait()
close(results)
}()
return results
}