/
output_parser.go
58 lines (50 loc) · 1.11 KB
/
output_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
package simple
import (
"bufio"
"fmt"
"io"
"strings"
)
type Output struct {
selected map[string]bool
updated map[string]string
Raw []string
}
func ParseOutput(r io.Reader) (Output, error) {
out := Output{
selected: make(map[string]bool),
updated: make(map[string]string),
}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
out.Raw = append(out.Raw, line)
split := strings.SplitN(line, ":", 2)
if len(split) < 2 {
continue
}
switch strings.TrimSpace(split[0]) {
case "selected":
out.selected[strings.TrimSpace(split[1])] = true
case "input":
split := strings.SplitN(split[1], ":", 2)
if len(split) != 2 {
continue
}
// should I really trim space from the value here?
out.updated[strings.TrimSpace(split[0])] = strings.TrimSpace(split[1])
}
}
if err := scanner.Err(); err != nil {
return out, fmt.Errorf("while reading simple stdout: %w", err)
}
return out, nil
}
func (o Output) Selected(id string) bool {
_, ok := o.selected[id]
return ok
}
func (o Output) Input(id string) (string, bool) {
u, ok := o.updated[id]
return u, ok
}