-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (106 loc) · 2.57 KB
/
main.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
package main
import (
"bufio"
"container/heap"
"fmt"
"log"
"os"
"strings"
"github.com/loganstone/kpick/conf"
"github.com/loganstone/kpick/dir"
"github.com/loganstone/kpick/file"
"github.com/loganstone/kpick/profile"
)
func confirm(question, ok, cancel string) (bool, error) {
var input string
reader := bufio.NewReader(os.Stdin)
fmt.Printf(question)
input, err := reader.ReadString('\n')
if err != nil {
return false, err
}
input = strings.Trim(input, " \n")
if input != ok && input != cancel {
return confirm(question, ok, cancel)
}
if input == cancel {
return false, nil
}
return true, nil
}
func summary(totalCnt, errorsCnt, containedFilesCnt int) {
fmt.Printf("[%d] scanning files\n", totalCnt)
fmt.Printf("[%d] error \n", errorsCnt)
fmt.Printf("[%d] success \n", totalCnt-errorsCnt)
fmt.Printf("[%d] files containing Korean\n", containedFilesCnt)
}
func main() {
opts := conf.Opts()
profile.CPU(opts.Cpuprofile)
skipPaths, err := opts.SkipPathsRegex()
if err != nil {
log.Fatal(err)
}
finder, err := dir.NewFinder(opts.DirPathToFind, opts.FileExtToScan, skipPaths)
if err != nil {
log.Fatal(err)
}
fmt.Printf("find [*.%s] files in [%s] directory\n", opts.FileExtToScan, opts.DirPathToFind)
if finder.Find() != nil {
log.Fatal(err)
}
if finder.ResultCount() == 0 {
fmt.Printf("[*.%s] file not found in [%s] directory\n", opts.FileExtToScan, opts.DirPathToFind)
os.Exit(0)
}
if opts.Interactive {
q := fmt.Sprintf("found [%d] files. scan it? (y/n): ", finder.ResultCount())
ok, err := confirm(q, "y", "n")
if err != nil {
log.Fatal(err)
}
if !ok {
os.Exit(0)
}
}
match, err := opts.Match()
if err != nil {
log.Fatal(err)
}
ignore, err := opts.Ignore()
if err != nil {
log.Fatal(err)
}
containKorean := &file.Heap{}
heap.Init(containKorean)
beforeFn := func(filePath string) {
if opts.Verbose {
fmt.Printf("[%s] scanning \"%s\"\n", filePath, match.String())
}
}
afterFn := func(filePath string) {
if opts.Verbose {
fmt.Printf("[%s] scanning done\n", filePath)
}
}
var scanErrorsCnt int
for _, paths := range finder.Chunk() {
for f := range file.ScanFiles(paths, match, ignore, beforeFn, afterFn) {
if err := f.Error(); err != nil {
scanErrorsCnt++
if opts.Verbose || opts.ErrorOnly {
fmt.Printf("[%s] scanning error - %s\n", f.Path(), err)
}
continue
}
if len(f.MatchedLines()) > 0 {
heap.Push(containKorean, f)
}
}
}
if !opts.ErrorOnly {
containKorean.Print()
}
summary(finder.ResultCount(), scanErrorsCnt, containKorean.Len())
profile.Mem(opts.Memprofile)
}