forked from pitstopcloud/virtualbox-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
43 lines (35 loc) · 823 Bytes
/
utils.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
package virtualbox
import (
"bufio"
"regexp"
"strings"
)
func parseKeyValues(text string, regexp *regexp.Regexp, callback func(key, val string) error) error {
return tryParseKeyValues(text, regexp, func(key, val string, ok bool) error {
if ok {
return callback(key, val)
}
return nil
})
}
func tryParseKeyValues(stdOut string, regexp *regexp.Regexp, callback func(key, val string, ok bool) error) error {
r := strings.NewReader(stdOut)
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
if strings.TrimSpace(line) == "" {
callback("", "", false)
continue
}
res := regexp.FindStringSubmatch(line)
if res == nil {
callback("", line, false)
continue
}
key, val := res[1], res[2]
if err := callback(key, val, true); err != nil {
return err
}
}
return s.Err()
}