-
Notifications
You must be signed in to change notification settings - Fork 62
/
vbcmd.go
93 lines (81 loc) · 2.4 KB
/
vbcmd.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
// DEPRECATED: Use Virtualbox and other interfaces
//go:generate go run github.com/golang/mock/mockgen@latest -source=vbcmd.go -destination=vbcmd.mock.go -package=virtualbox -mock_names=Command=MockCommand
package virtualbox
import (
"bytes"
"errors"
"os/exec"
"runtime"
)
type option func(Command)
// Command is the mock-able interface to run VirtualBox commands
// such as VBoxManage (host side) or VBoxControl (guest side)
type Command interface {
setOpts(opts ...option) Command
isGuest() bool
path() string
run(args ...string) (string, string, error)
}
var (
// Verbose toggles the library in verbose execution mode.
Verbose bool
// ErrMachineExist holds the error message when the machine already exists.
ErrMachineExist = errors.New("machine already exists")
// ErrMachineNotExist holds the error message when the machine does not exist.
ErrMachineNotExist = errors.New("machine does not exist")
// ErrCommandNotFound holds the error message when the VBoxManage commands was not found.
ErrCommandNotFound = errors.New("command not found")
)
type command struct {
program string
sudoer bool // Is current user a sudoer?
sudo bool // Is current command expected to be run under sudo?
guest bool
}
func (vbcmd command) setOpts(opts ...option) Command {
var cmd Command = &vbcmd
for _, opt := range opts {
opt(cmd)
}
return cmd
}
func sudo(sudo bool) option {
return func(cmd Command) {
vbcmd := cmd.(*command)
vbcmd.sudo = sudo
Debug("Next sudo: %v", vbcmd.sudo)
}
}
func (vbcmd command) isGuest() bool {
return vbcmd.guest
}
func (vbcmd command) path() string {
return vbcmd.program
}
func (vbcmd command) prepare(args []string) *exec.Cmd {
program := vbcmd.program
argv := []string{}
Debug("Command: '%+v', runtime.GOOS: '%s'", vbcmd, runtime.GOOS)
if vbcmd.sudoer && vbcmd.sudo && runtime.GOOS != osWindows {
program = "sudo"
argv = append(argv, vbcmd.program)
}
argv = append(argv, args...)
Debug("executing: %v %v", program, argv)
return exec.Command(program, argv...) // #nosec
}
func (vbcmd command) run(args ...string) (string, string, error) {
defer vbcmd.setOpts(sudo(false))
cmd := vbcmd.prepare(args)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
if ee, ok := err.(*exec.Error); ok && ee == exec.ErrNotFound {
err = ErrCommandNotFound
}
}
return stdout.String(), stderr.String(), err
}