-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
35 lines (33 loc) · 891 Bytes
/
walk.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
package gofind
import (
"fmt"
"os"
"path/filepath"
)
// WalkFind search using path.Walk function
func WalkFind(conf Config) ([]string, int32) {
filepath.Walk(conf.StartPath, func(filePath string, fi os.FileInfo, e error) (err error) {
incCounter()
fileNameMatch := false
fileName := fi.Name()
if conf.SearchByName {
fileNameMatch = conf.SearchNamePattern.MatchString(fileName)
}
if conf.SearchByName && fileNameMatch {
if conf.SearchByContent && !fi.IsDir() {
err = searchFile(conf, filePath)
}
}
if conf.SearchByName && fileNameMatch && !conf.SearchByContent {
printRes(filePath)
}
if !conf.SearchByName && conf.SearchByContent && !fi.IsDir() {
err = searchFile(conf, filePath)
}
if err != nil && !conf.Quiet && !os.IsPermission(err) {
fmt.Printf("Error reading file %s (%v)\n", filePath, err)
}
return
})
return results, counter
}