-
Notifications
You must be signed in to change notification settings - Fork 1
/
bulksearch.go
58 lines (50 loc) · 1.2 KB
/
bulksearch.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
package util
import (
"os/exec"
"path/filepath"
"sort"
"strings"
"sync"
)
var getAbs = filepath.Abs
type Result struct {
Term string
FileCount int
}
// Search searches for the given search terms in the given directories.
func Search(searchTerms []string, dirs []string) []Result {
var wg sync.WaitGroup
results := make([]Result, 0, len(searchTerms))
resultChan := make(chan Result) // what is being transmitted through the
for _, term := range searchTerms {
wg.Add(1)
go func(term string) {
defer wg.Done()
result := Result{Term: term, FileCount: 0}
for _, subTerm := range strings.Split(term, " ") {
for _, dir := range dirs {
if len(subTerm) > 0 {
result.FileCount += SearchFor(subTerm, dir)
}
}
}
// send result to channel
resultChan <- result
}(term)
}
go func() {
wg.Wait()
close(resultChan)
}()
for result := range resultChan {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i].FileCount > results[j].FileCount
})
return results
}
func SearchFor(term string, dir string) int {
out, _ := exec.Command("rg", "-IcF", term, dir).Output()
return len(strings.Split(string(out), "\n")) - 1
}