-
Notifications
You must be signed in to change notification settings - Fork 0
/
gofind.go
221 lines (203 loc) · 5.09 KB
/
gofind.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Package gofind includes methods and structures to search by file name and file content
package gofind
import (
"compress/gzip"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"sync"
"sync/atomic"
)
var (
fileMux = &sync.Mutex{}
dirMux = &sync.Mutex{}
wg = &sync.WaitGroup{}
results = []string{}
counter int32
)
// Config struct of find functionality
// StartPath - root directory to start search
// SearchPatter - regular expression with patter to search
// PrintErrors - print errors about opening files, access denied etc
// PrintStatistics - print stats how long search was made
type Config struct {
StartPath string
SearchNamePattern *regexp.Regexp
SearchContentPattern *regexp.Regexp
SearchByName bool
SearchByContent bool
Quiet bool
ShowContext bool
ContextBuffer int
ShowLine bool
IncludeSubdirs bool
IncludeGzip bool
}
//
// NewConfig create new instance of Config
//
func NewConfig(startDir, fileNamePattern, contentPattern string, quiet bool, contextBuffer int, showLine bool, includeSubdirs bool, includeGzip bool) Config {
var (
snpRe, scpRe *regexp.Regexp
searchByName, searchByContent bool
)
if searchByName = fileNamePattern != ""; searchByName {
snpRe = regexp.MustCompile(fileNamePattern)
}
if searchByContent = contentPattern != ""; searchByContent {
scpRe = regexp.MustCompile(contentPattern)
}
return Config{StartPath: startDir,
SearchNamePattern: snpRe,
SearchContentPattern: scpRe,
SearchByName: searchByName,
SearchByContent: searchByContent,
Quiet: quiet,
ShowContext: contextBuffer > 0,
ContextBuffer: contextBuffer,
ShowLine: showLine,
IncludeSubdirs: includeSubdirs,
IncludeGzip: includeGzip,
}
}
// Find returns list of files which maches to patterns from conf
func Find(conf Config) ([]string, int32) {
wg.Add(1)
go searchDir(conf, conf.StartPath)
wg.Wait()
return results, counter
}
func searchDir(conf Config, dirPath string) {
var (
filesInfos []os.FileInfo
err error
fileName string
filePath string
)
defer wg.Done()
incCounter()
if filesInfos, err = readDir(dirPath); err != nil {
if conf.Quiet && os.IsPermission(err) {
return
}
fmt.Printf("Error reading file: %s (%v)\n", dirPath, err)
}
for _, fileInfo := range filesInfos {
fileName = fileInfo.Name()
filePath = path.Join(dirPath, fileName)
fileNameMatch := false
if conf.SearchByName {
fileNameMatch = conf.SearchNamePattern.MatchString(fileName)
}
if conf.SearchByName && fileNameMatch && !conf.SearchByContent {
printRes(filePath)
}
if fileInfo.IsDir() {
if conf.IncludeSubdirs {
wg.Add(1)
go searchDir(conf, filePath)
}
} else {
incCounter()
if conf.SearchByName && fileNameMatch && conf.SearchByContent {
err = searchFile(conf, filePath)
}
if !conf.SearchByName && conf.SearchByContent {
err = searchFile(conf, filePath)
}
if err != nil && !conf.Quiet && !os.IsPermission(err) {
fmt.Printf("Error reading file %s (%v)\n", filePath, err)
}
}
}
}
func readDir(dirPath string) (filesInfos []os.FileInfo, err error) {
var (
file *os.File
)
defer dirMux.Unlock()
dirMux.Lock()
if file, err = os.Open(dirPath); err != nil {
return
}
filesInfos, err = file.Readdir(-1)
file.Close()
return
}
func searchFile(conf Config, filePath string) (err error) {
var (
fileCnt []byte
gzFile *os.File
gzReader *gzip.Reader
)
fileMux.Lock()
defer fileMux.Unlock()
if strings.HasSuffix(filePath, ".gz") && conf.IncludeGzip {
if gzFile, err = os.Open(filePath); err != nil {
return
}
defer gzFile.Close()
if gzReader, err = gzip.NewReader(gzFile); err != nil {
return
}
defer gzReader.Close()
if fileCnt, err = ioutil.ReadAll(gzReader); err != nil {
return
}
} else {
if fileCnt, err = ioutil.ReadFile(filePath); err != nil {
return
}
}
if conf.SearchContentPattern.Match(fileCnt) {
printRes(filePath)
if conf.ShowContext {
printMatchContext(fileCnt, conf.SearchContentPattern, conf.ContextBuffer)
}
if conf.ShowLine {
printMatchLine(fileCnt, conf.SearchContentPattern)
}
}
return
}
func printMatchContext(content []byte, re *regexp.Regexp, bufferSize int) {
indexes := re.FindAllIndex(content, -1)
for _, index := range indexes {
start := index[0]
if start > bufferSize {
start = start - bufferSize
} else {
start = 0
}
end := index[1]
if end+bufferSize < len(content) {
end = end + bufferSize
} else {
end = len(content)
}
fmt.Printf("------\n%s\n------\n", content[start:end])
}
}
func printMatchLine(content []byte, re *regexp.Regexp) {
indexes := re.FindAllIndex(content, -1)
if len(indexes) == 0 {
return
}
strContent := string(content)
lines := strings.Split(strContent, "\n")
for _, line := range lines {
if re.Match([]byte(line)) {
fmt.Printf("%s\n", line)
}
}
}
func printRes(fileName string) {
fmt.Println(fileName)
results = append(results, fileName)
}
func incCounter() {
atomic.AddInt32(&counter, 1)
}