forked from syndbg/vagrant-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvagrant_output.go
50 lines (39 loc) · 1.04 KB
/
vagrant_output.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
package vagrant_go
import (
"strings"
)
var ignoredOutputLines = []string{"metadata", "ui", "action"}
// NOTE: A Vagrant line is in format of `timestamp,target,type,data`.
// ref: https://www.vagrantup.com/docs/cli/machine-readable.html
type vagrantOutputLine struct {
timestamp string
target string
kind string
data []string
}
func vagrantOutputLineFromString(str string) *vagrantOutputLine {
trimmedStr := strings.TrimSpace(str)
splitLines := strings.SplitN(trimmedStr, ",", 4)
//noinspection GoPreferNilSlice
vagrantLines := []string{}
for _, line := range splitLines {
if contains(ignoredOutputLines, line) {
continue
}
vagrantLines = append(vagrantLines, line)
}
if len(vagrantLines) < 4 {
return nil
}
//noinspection GoPreferNilSlice
dataLines := []string{}
for _, line := range strings.Split(vagrantLines[3], ",") {
dataLines = append(dataLines, line)
}
return &vagrantOutputLine{
timestamp: vagrantLines[0],
target: vagrantLines[1],
kind: vagrantLines[2],
data: dataLines,
}
}