-
Notifications
You must be signed in to change notification settings - Fork 0
/
grep.go
163 lines (148 loc) · 2.97 KB
/
grep.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
)
var (
//kernel32 = syscall.NewLazyDLL("kernel32.dll")
//proc = kernel32.NewProc("SetConsoleTextAttribute")
)
type FileContent struct {
Name string
Lines []Line
}
type Line struct {
Number int
Content string
}
var ResultCH chan FileContent
var pattern string
var word bool
var wg sync.WaitGroup
func init() {
ResultCH = make(chan FileContent, 10000)
}
func main() {
flag.BoolVar(&word, "w", true, "find word")
flag.Parse()
if flag.NArg() == 0 {
fmt.Println("Usage: grep content [dir]...")
os.Exit(-1)
}
pattern = flag.Arg(0)
dirs := []string{pattern, "."}
if flag.NArg() > 1 {
dirs = flag.Args()
}
//fmt.Println(dirs)
for i, v := range dirs {
if i == 0 {
continue
}
if len(v) > 1 &&
v[0] == '.' && v[1] != '.' {
continue
}
f, _ := os.Stat(v)
if f.IsDir() {
FindDir(v)
continue
}
if strings.HasSuffix(f.Name(), ".exe") ||
strings.HasSuffix(f.Name(), ".tar") ||
strings.HasSuffix(f.Name(), ".tar.gz") ||
strings.HasSuffix(f.Name(), ".zip") ||
strings.HasSuffix(f.Name(), ".rar") {
continue
}
wg.Add(1)
go FindFile(v)
}
wg.Wait()
hasFound := false
for {
select {
case file := <-ResultCH:
for _, v := range file.Lines {
fmt.Printf("%s(%d): ", file.Name, v.Number)
Color(v.Content, pattern)
}
hasFound = true
default:
if !hasFound {
fmt.Println("No found everythings")
}
return
}
}
}
func FindDir(s string) {
dir, err := os.Open(s)
if err != nil {
fmt.Println(err)
return
}
defer dir.Close()
fi, err := dir.Readdir(0)
if err != nil {
fmt.Println(err)
return
}
for _, item := range fi {
name := s + "/" + item.Name()
if len(item.Name()) > 1 &&
strings.HasPrefix(item.Name(), ".") {
continue
}
if item.IsDir() {
FindDir(name)
continue
}
if word && (strings.HasSuffix(item.Name(), ".exe") ||
strings.HasSuffix(item.Name(), ".tar") ||
strings.HasSuffix(item.Name(), ".tar.gz") ||
strings.HasSuffix(item.Name(), ".zip") ||
strings.HasSuffix(item.Name(), ".rar")) {
continue
}
wg.Add(1)
go FindFile(name)
}
}
func FindFile(file string) {
name, err := filepath.Abs(file)
data, err := ioutil.ReadFile(name)
if err != nil {
fmt.Println(err)
return
}
content := string(data)
Lines := make([]Line, 0, 10000)
for i, line := range strings.Split(content, "\n") {
if len(line) <= 0 {
continue
}
if strings.Contains(line, pattern) {
Lines = append(Lines, Line{i, strings.TrimSpace(line)})
}
}
if len(Lines) > 0 {
ResultCH <- FileContent{file, Lines}
}
wg.Done()
}
func Color(v, a string) {
idx := strings.Index(v, a)
fmt.Printf("%s", v[:idx])
//handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(12)) //12 Red light
fmt.Printf(a)
//handle, _, _ = proc.Call(uintptr(syscall.Stdout), uintptr(7)) //White dark
//CloseHandle := kernel32.NewProc("CloseHandle")
//CloseHandle.Call(handle)
fmt.Printf("%s\n", v[idx+len(a):])
}