-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathparse.go
52 lines (42 loc) · 1.01 KB
/
parse.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
package vagrantutil
import (
"bufio"
"fmt"
"strings"
)
// parseData parses the given vagrant type field from the machine readable
// output (records).
func parseData(records [][]string, typeName string) (string, error) {
data := ""
for _, record := range records {
// first three are defined, after that data is variadic, it contains
// zero or more information. We should have a data, otherwise it's
// useless.
if len(record) < 4 {
continue
}
if typeName == record[2] && record[3] != "" {
data = record[3]
break
}
}
if data == "" {
return "", fmt.Errorf("couldn't parse data for vagrant type: %q", typeName)
}
return data, nil
}
func parseRecords(out string) (recs [][]string, err error) {
scanner := bufio.NewScanner(strings.NewReader(out))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
row := strings.Split(line, ",")
recs = append(recs, row)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return recs, nil
}