-
Notifications
You must be signed in to change notification settings - Fork 0
/
pug.go
95 lines (80 loc) · 1.82 KB
/
pug.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
package main
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"sync"
"github.com/CrowdSurge/banner"
"github.com/fatih/color"
)
var wg sync.WaitGroup
var search_string string = ""
var cyan = color.New(color.FgCyan)
var boldCyan = cyan.Add(color.Bold)
func walk_r(dir string) {
f, err := os.Open(dir)
if err != nil {
fmt.Println(err)
}
list, err := f.Readdir(-1)
f.Close()
if err != nil {
fmt.Println(err)
}
for _, v := range list {
if strings.HasPrefix(v.Name(), ".") == false &&
strings.HasSuffix(v.Name(), ".log") == false {
path := fmt.Sprintf("%s%s%s", dir, string(os.PathSeparator), v.Name())
if v.IsDir() {
wg.Add(1)
go walk_r(path)
} else {
file, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
}
str := string(file)
line := 0
temp := strings.Split(str, "\n")
matched_lines := []string{}
for _, item := range temp {
if strings.Contains(item, search_string) {
white := color.New(color.BgRed, color.FgYellow, color.Bold).SprintFunc()
h_item := strings.Replace(item, search_string, white(search_string), -1)
t1 := fmt.Sprintf("%d \t %s", line, h_item)
t2 := len(t1)
if len(t1) > 255 {
t2 = 255
}
matched_lines = append(matched_lines, t1[:t2])
}
line++
}
if len(matched_lines) > 0 {
boldCyan.Println(path)
for _, str := range matched_lines {
color.Yellow(str)
}
}
}
}
}
wg.Done()
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
args := os.Args[1:]
banner.Print(" pug ")
color.Red("\n- by Siddhartha Mukherjee <[email protected]>")
color.Yellow("\nUsage: pug [search string] [optional: /path/to/search]\n")
dir := "."
search_string = args[0]
if len(args) > 1 {
dir = args[1]
}
wg.Add(1)
go walk_r(dir)
wg.Wait()
}