-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
82 lines (70 loc) · 1.59 KB
/
parser.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
package parser
import (
"bufio"
"bytes"
"fmt"
"regexp"
"strings"
)
type (
Parser struct {
Message string
Mode string
}
)
var modes = []string{"summary", "simple", "full"}
func Parse(p *Parser) (string, error) {
var b bytes.Buffer
if !contains(modes, p.Mode) {
return "", fmt.Errorf("Mode is invalid, required one of [%s]", strings.Join(modes, ","))
}
r := strings.NewReader(p.Message)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
rHash, _ := regexp.Compile("^#")
rSum, _ := regexp.Compile("^Plan:")
rSymbol, _ := regexp.Compile("^\\s{2}[\\+\\-\\~#]")
rNothing, _ := regexp.Compile("This plan does nothing.")
s := strings.TrimLeft(scanner.Text(), " ")
switch p.Mode {
case "full":
if rSymbol.MatchString(scanner.Text()) {
_, _ = b.WriteString(fmt.Sprintf("%s\n", s))
} else {
_, _ = b.WriteString(fmt.Sprintf("%s\n", scanner.Text()))
}
break
case "simple":
if rHash.MatchString(s) {
_, _ = b.WriteString(fmt.Sprintf("%s\n", s))
}
if rSum.MatchString(s) {
_, _ = b.WriteString(fmt.Sprintf("\n%s\n", s))
}
if rNothing.MatchString(s) {
_, _ = b.WriteString(fmt.Sprintf("%s\n", scanner.Text()))
}
break
default:
if rSum.MatchString(s) {
_, _ = b.WriteString(fmt.Sprintf("%s\n", s))
}
if rNothing.MatchString(s) {
_, _ = b.WriteString(fmt.Sprintf("%s\n", scanner.Text()))
}
}
continue
}
if err := scanner.Err(); err != nil {
return "", err
}
return b.String(), nil
}
func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}