-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
76 lines (64 loc) · 1.65 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
package main
import (
"errors"
"fmt"
"os"
)
func showVersion() {
fmt.Println(`blook 1.0.0 (Eclipse Public License 1.0, https://github.com/abtv/blook)`)
}
func showHelp() {
fmt.Println(`Usage: blook from_pattern to_pattern file [file ...]
Makes binary search in one or more files. Returns all the lines which >= 'from_pattern' and <= 'to_pattern'.
Other options:
blook help - shows help
blook version - shows version`)
}
func filterFile(patternFrom string, patternTo string, filename string) {
file, err := openFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
defer file.ptr.Close()
start, err := blook(patternFrom, file.ptr, file.size, true)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
stop, err := blook(patternTo, file.ptr, file.size, false)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
if start != -1 && start <= stop {
maxBufferSize := int64(1024 * 1024)
_, err = writeBytes(file.ptr, start, stop, os.Stdout, maxBufferSize)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println("") //add newline symbol to the end of filter output
}
}
func filter(patternFrom string, patternTo string, filenames []string) {
for _, filename := range filenames {
filterFile(patternFrom, patternTo, filename)
}
}
func showError(err error) {
fmt.Fprintln(os.Stderr, err)
}
func main() {
cmdParams, err := getCmdParams()
if err != nil {
showError(err)
return
}
switch cmdParams.command {
case "version":
showVersion()
case "help":
showHelp()
case "filter":
filter(cmdParams.patternFrom, cmdParams.patternTo, cmdParams.filenames)
default:
showError(errors.New("Unknown command. Please use 'blook help'"))
}
}