-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopsfind.go
More file actions
95 lines (76 loc) · 1.67 KB
/
popsfind.go
File metadata and controls
95 lines (76 loc) · 1.67 KB
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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"io/ioutil"
)
func isIgnored(name string) bool {
dirsToIgnore := []string{"Godeps", ".svn", ".git", "vendor"}
for _, dir := range dirsToIgnore {
if name == dir {
return true
}
}
return false
}
func processFile(path string, patterns []string) {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
headerPrinted := false
lineNum := 1
scanner := bufio.NewScanner(file)
for scanner.Scan() {
for _, pattern := range patterns {
if strings.Contains(strings.ToLower(scanner.Text()), strings.ToLower(pattern)) {
if !headerPrinted {
fmt.Printf("##### %s\n", path)
headerPrinted = true
}
fmt.Printf("%5d: %s\n", lineNum, scanner.Text())
continue
}
}
lineNum++
}
if headerPrinted {
fmt.Printf("\n")
}
}
func processDirectory(path string, fileType string, patterns []string) {
files, _ := ioutil.ReadDir(path)
for _, f := range files {
if f.IsDir() {
if isIgnored(f.Name()) {
continue
}
dirName := path + "/" + f.Name()
processDirectory(dirName, fileType, patterns)
}
if fileType != "ALL" && !strings.HasSuffix(f.Name(), fileType) {
continue
}
fileName := path + "/" + f.Name()
processFile(fileName, patterns)
}
}
func main() {
if len(os.Args) < 3 {
log.Fatal("usage: pops-find <filetype> string1 [string2...stringn]")
}
var fileType string;
log.Printf(os.Args[1])
if os.Args[1] != "ALL" {
fileType = "." + os.Args[1]
} else {
fileType = "ALL"
}
patterns := os.Args[2:]
log.Printf("### Searching '%s' files for: %s\n", fileType, patterns)
processDirectory(".", fileType, patterns)
}