-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
203 lines (178 loc) · 5.23 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
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
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"github.com/fatih/color"
"github.com/karrick/godirwalk"
"github.com/spf13/pflag"
)
var root, query, filenameRegEx, ignoreRegEx string
var cFiles, goFiles, rustFiles, rubyFiles, jsFiles, pyFiles, gitCommit, removeCode, removeColor bool
var ignore *regexp.Regexp
var qExpr *regexp.Regexp
var fExprs = make([]*regexp.Regexp, 0)
var found = 0
var wg sync.WaitGroup
func init() {
pflag.StringVarP(&query, "query", "q", "", "regexp to match source content")
pflag.StringVarP(&root, "root", "r", ".", "root to start your hunt")
pflag.StringVarP(&filenameRegEx, "name-regexp", "n", "", "regexp to match the filename")
pflag.StringVarP(&ignoreRegEx, "ignore-regexp", "i", "^\\.git|^vendor", "regexp to ignore matching the filename")
pflag.BoolVarP(&cFiles, "c-files", "c", false, "search for c/c++ files")
pflag.BoolVarP(&goFiles, "go-files", "g", false, "search for Go files")
pflag.BoolVarP(&rustFiles, "rust-files", "s", false, "search for rust files")
pflag.BoolVarP(&rubyFiles, "ruby-files", "b", false, "search for ruby files")
pflag.BoolVarP(&jsFiles, "js-files", "j", false, "search for JavaScript files")
pflag.BoolVarP(&pyFiles, "py-files", "p", false, "search for python files")
pflag.BoolVarP(&gitCommit, "git-commit", "h", false, "git the git commit details for the found line")
pflag.BoolVarP(&removeCode, "remove-code", "v", false, "do not show the matching line of code")
pflag.BoolVarP(&removeColor, "remove-color", "x", false, "do not color the code")
pflag.Parse()
if pflag.NFlag() == 0 || root == "" {
fmt.Println("hunt - a simple way to hunt for content in source files\n")
fmt.Println("usage: hunt -query \"foo bar\" -root .\n")
fmt.Println("flags:")
pflag.PrintDefaults()
os.Exit(-1)
}
if removeColor {
color.NoColor = true
}
}
func readFile(wg *sync.WaitGroup, path string) {
defer wg.Done()
file, err := os.Open(path)
defer file.Close()
if err != nil {
return
}
scanner := bufio.NewScanner(file)
for i := 1; scanner.Scan(); i++ {
code := strings.TrimSpace(scanner.Text())
matches := qExpr.FindAllStringSubmatchIndex(code, -1)
if len(matches) > 0 {
found++
blue := color.New(color.FgBlue).SprintFunc()
white := color.New(color.FgWhite).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
yellow := color.New(color.FgYellow).SprintfFunc()
output := fmt.Sprintf("%s:%s ", blue(path), green(fmt.Sprintf("%d", i)))
idx := 0
if !removeCode {
for _, match := range matches {
output = fmt.Sprintf("%s%s%s", output, white(code[idx:match[0]]), red(code[match[0]:match[1]]))
idx = match[1]
}
output = fmt.Sprintf("%s%s", output, code[idx:])
}
if gitCommit {
gitLog, err := exec.Command("git", "log", fmt.Sprintf("-L%d,+1:%s", i, path), "--pretty=format:\"%h - %an, %ar : %s\"").CombinedOutput()
if err != nil {
log.Fatal(err)
}
logLines := strings.Split(string(gitLog), "\n")
if len(logLines) == 0 {
log.Fatal("error getting git log")
}
output = fmt.Sprintf("%s %s %s", output, green("->"), yellow("%s", logLines[0]))
}
fmt.Printf("%s\n", output)
}
}
}
func compExp(exp string) (*regexp.Regexp, error) {
expr, err := regexp.Compile(exp)
if err != nil {
return nil, fmt.Errorf("error compiling file name regexp: %s", err)
}
return expr, nil
}
func checkErr(err error) {
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func main() {
expr, err := compExp(query)
checkErr(err)
qExpr = expr
if filenameRegEx != "" {
expr, err := compExp(filenameRegEx)
checkErr(err)
fExprs = append(fExprs, expr)
}
if cFiles || goFiles || rustFiles || rubyFiles || jsFiles || pyFiles {
if cFiles {
expr, err := compExp(".c$|.h$|.cpp$|.hpp$")
checkErr(err)
fExprs = append(fExprs, expr)
}
if goFiles {
expr, err := compExp(".go$")
checkErr(err)
fExprs = append(fExprs, expr)
}
if rustFiles {
expr, err := compExp(".rs$")
checkErr(err)
fExprs = append(fExprs, expr)
}
if rubyFiles {
expr, err := compExp(".rb$")
checkErr(err)
fExprs = append(fExprs, expr)
}
if jsFiles {
expr, err := compExp(".js$|.ts$|.jsx$|.tsx$")
checkErr(err)
fExprs = append(fExprs, expr)
}
if pyFiles {
expr, err := compExp(".py$")
checkErr(err)
fExprs = append(fExprs, expr)
}
}
red := color.New(color.FgRed).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
ignoreExpr, err := compExp(ignoreRegEx)
checkErr(err)
fmt.Printf("Hunting for -> '%s' in '%s'\n\n", red(query), blue(root))
godirwalk.Walk(root, &godirwalk.Options{
Unsorted: true,
Callback: func(path string, de *godirwalk.Dirent) error {
if !de.IsDir() && !ignoreExpr.MatchString(path) {
match := false
if len(fExprs) == 0 {
match = true
} else {
for _, expr := range fExprs {
if expr.Match([]byte(de.Name())) {
match = true
}
}
}
if !match {
return nil
}
wg.Add(1)
go readFile(&wg, path)
}
return nil
},
ErrorCallback: func(path string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
})
wg.Wait()
fmt.Printf("\n%s occurrences found\n", red(fmt.Sprintf("%d", found)))
os.Exit(found)
}