-
Notifications
You must be signed in to change notification settings - Fork 5
/
vagrant_client.go
50 lines (43 loc) · 1.27 KB
/
vagrant_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
package vagrant
import (
"os"
"os/exec"
"path/filepath"
)
// VagrantClient is the main entry point to the library. Users should construct
// a new VagrantClient using NewVagrantClient().
type VagrantClient struct {
// Directory where the Vagrantfile can be found.
//
// Normally this would be set by NewVagrantClient() and shouldn't
// be changed afterward.
VagrantfileDir string
executable string
preArguments []string
}
// NewVagrantClient creates a new VagrantClient.
//
// vagrantfileDir should be the path to a directory where the Vagrantfile
// exists.
func NewVagrantClient(vagrantfileDir string) (*VagrantClient, error) {
// Verify the vagrant command is in the path
path, err := exec.LookPath("vagrant")
if err != nil {
return nil, err
}
// Verify a Vagrantfile exists
vagrantfilePath := filepath.Join(vagrantfileDir, "Vagrantfile")
if _, err := os.Stat(vagrantfilePath); err != nil {
return nil, err
}
return &VagrantClient{
VagrantfileDir: vagrantfileDir,
executable: path,
}, nil
}
func (client *VagrantClient) buildArguments(subcommand string) []string {
if client.preArguments == nil || len(client.preArguments) == 0 {
return []string{subcommand, "--machine-readable"}
}
return append(client.preArguments, subcommand, "--machine-readable")
}