-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.go
61 lines (56 loc) · 1.2 KB
/
action.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"
"io"
"strings"
)
type rawAction struct {
Action string
ID string
Message string
Description string
}
func extractRawActions(in string) (out []*rawAction, err error) {
r := bufio.NewReader(strings.NewReader(in))
var current *rawAction
for {
line, err := r.ReadString('\n')
if err != nil {
if err == io.EOF {
break
} else {
return nil, err
}
}
line = strings.TrimSpace(line)
if ra := extractRawAction(line); ra != nil {
if current != nil {
current.Description = strings.TrimSpace(current.Description)
}
out = append(out, ra)
current = ra
} else if current != nil {
if isComment(line) {
continue
}
if len(current.Description) > 0 {
current.Description += "\n"
}
current.Description += line
}
}
if current != nil {
current.Description = strings.TrimSpace(current.Description)
}
return out, nil
}
func extractRawAction(in string) (out *rawAction) {
fields := strings.Fields(in)
if len(fields) < 3 {
return nil
}
if len(fields[0]) != 1 { // replace this with something smarter
return nil
}
return &rawAction{Action: fields[0], ID: fields[1], Message: strings.Join(fields[2:], " ")}
}