-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
61 lines (53 loc) · 1.03 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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/fatih/color"
"github.com/pkg/errors"
)
// main
func _main() (int, error) {
var err error
if len(os.Args) < 2 {
return 1, fmt.Errorf("Keyword is not specified.")
}
keyword := os.Args[1]
var fp *os.File
if len(os.Args) > 2 {
fp, err = os.Open(os.Args[2])
if err != nil {
return 2, errors.Wrap(err, "Can not open file.")
}
defer fp.Close()
} else {
fp = os.Stdin
}
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
text := scanner.Text()
for index := strings.Index(text, keyword); index >= 0; index = strings.Index(text, keyword) {
fmt.Print(text[0:index])
color.Red(keyword)
text = text[index+len(keyword) : len(text)]
}
if len(text) > 0 {
fmt.Print(text)
}
fmt.Println("")
}
err = scanner.Err()
if err != nil {
return 3, errors.Wrap(err, "Can not read input.")
}
return 0, nil
}
// Entry point
func main() {
status, err := _main()
if err != nil {
fmt.Fprintf(os.Stderr, "[Error] %v\n", err)
os.Exit(status)
}
}