-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (59 loc) · 1.15 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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"regexp"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 2 {
fmt.Println("comment usage: comment op comment_string")
fmt.Println("")
fmt.Println("to comment with ' ': comment + ' '")
fmt.Println("to remove ' ' comment: comment - ' '")
os.Exit(0)
}
op := args[0]
comment := args[1]
if op != "+" && op != "-" {
fmt.Println("op must be '+' to comment or '-' to uncomment")
os.Exit(1)
}
reader := bufio.NewReader(os.Stdin)
var commentRegex *regexp.Regexp
if op == "-" {
cR, err := regexp.CompilePOSIX(comment)
commentRegex = cR
if err != nil {
fmt.Println("Bad regex: " + comment)
os.Exit(1)
}
} else {
commentRegex = nil
}
for {
line, err := reader.ReadString('\n')
dieAfterwards := false
if err == io.EOF {
dieAfterwards = true
}
if err != nil && err != io.EOF {
fmt.Println("err: " + err.Error())
os.Exit(1)
}
if op == "+" {
fmt.Printf("%s%s", comment, line)
}
if op == "-" {
toWrite := commentRegex.ReplaceAll([]byte(line), []byte(""))
fmt.Printf("%s", string(toWrite))
}
if dieAfterwards {
break
}
}
}