forked from syndbg/vagrant-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
95 lines (75 loc) · 1.95 KB
/
client.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package vagrant_go
import (
"github.com/palantir/stacktrace"
"strings"
)
type Client struct {
Config *Config
commandRunFunc func(cmd string, args ...string) ([]byte, error)
osExecutor OsExecutor
Box BoxAPI
Global GlobalAPI
}
func NewClient(
config *Config,
commandRunFunc func(cmd string, args ...string) ([]byte, error),
lookPathFunc func(file string) (string, error),
) (*Client, error) {
clientConfig := DefaultConfig()
if config != nil && len(config.BinaryName) > 0 {
clientConfig.BinaryName = config.BinaryName
}
clientLookPathFunc := realLookPathFunc
if lookPathFunc != nil {
clientLookPathFunc = lookPathFunc
}
_, err := clientLookPathFunc(clientConfig.BinaryName)
if err != nil {
return nil, stacktrace.Propagate(
err,
"`%s` not found in $PATH",
clientConfig.BinaryName,
)
}
clientCommandRunFunc := realCommandRunFunc
if commandRunFunc != nil {
clientCommandRunFunc = commandRunFunc
}
client := &Client{
Config: clientConfig,
commandRunFunc: clientCommandRunFunc,
}
client.Box = &boxAPI{
client: client,
}
client.Global = &globalAPI{
client: client,
osExecutor: &osExecutor{},
}
return client, nil
}
func (c *Client) executeVagrantCommand(args ...string) ([]*vagrantOutputLine, error) {
cmdArgs := []string{
"--machine-readable",
}
for _, arg := range args {
cmdArgs = append(cmdArgs, arg)
}
output, err := c.commandRunFunc(c.Config.BinaryName, cmdArgs...)
return c.parseMachineReadableOutput(string(output)), err
}
func (c *Client) parseMachineReadableOutput(output string) []*vagrantOutputLine {
vagrantOutputLines := []*vagrantOutputLine{}
outputLines := strings.Split(output, "\n")
for _, outputLine := range outputLines {
vagrantOutputLine := vagrantOutputLineFromString(outputLine)
if vagrantOutputLine == nil {
continue
}
vagrantOutputLines = append(
vagrantOutputLines,
vagrantOutputLine,
)
}
return vagrantOutputLines
}