Skip to content

Commit 4611215

Browse files
committed
add MachineName option to the commands that support it
1 parent d2e19d2 commit 4611215

13 files changed

+77
-15
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ Options:
9595
complain that there's no attached TTY.
9696
* **Parallel** (default: `false`) - Enable or disable parallelism if provider
9797
supports it (automatically enables Force).
98+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to destroy.
99+
If unspecified (default), all VMs in the current directory will be destroyed.
98100

99101
Response:
100102
* **Error** - Set if an error occurred.
@@ -127,6 +129,8 @@ func (*VagrantClient) Halt() *HaltCommand
127129
Options:
128130
* **Force** (default: `false`) - Force shutdown (equivalent to pulling the
129131
power of the VM)
132+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to halt.
133+
If unspecified (default), all VMs in the current directory will be halted.
130134

131135
Response:
132136
* **Error** - Set if an error occurred.
@@ -163,6 +167,9 @@ func (*VagrantClient) Provision() *ProvisionCommand
163167
Options:
164168
* **Provisioners** (default: `nil`) - Enable only certain provisioners, by type
165169
or name as an array of strings.
170+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to provision.
171+
If unspecified (default), all VMs in the current directory will be
172+
provisioned.
166173

167174
Response:
168175
* **Error** - Set if an error occurred.
@@ -182,6 +189,8 @@ Options:
182189
provisioners will be disabled.
183190
* **Provisioners** (default: `nil`) - Enable only certain provisioners, by type
184191
or name as an array of strings. Implies ForceProvisioning.
192+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to reload.
193+
If unspecified (default), all VMs in the current directory will be reloaded.
185194

186195
Response:
187196
* **Error** - Set if an error occurred.
@@ -201,6 +210,8 @@ Options:
201210
provisioners will be disabled.
202211
* **Provisioners** (default: `nil`) - Enable only certain provisioners, by type
203212
or name as an array of strings. Implies ForceProvisioning.
213+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to resume.
214+
If unspecified (default), all VMs in the current directory will be resumed.
204215

205216
Response:
206217
* **Error** - Set if an error occurred.
@@ -215,6 +226,9 @@ func (*VagrantClient) SSHConfig() *SSHConfigCommand
215226

216227
Options:
217228
* **Host** (default: `""`) - Name the host for the config
229+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to retrieve
230+
the configuration for. If unspecified (default), the configuration of all VMs
231+
in the current directory will be returned.
218232

219233
Response:
220234
* **Configs** - a map of vagrant machine names to `SSHConfig` objects. Each
@@ -229,6 +243,11 @@ Get the status of the vagrant machine.
229243
func (*VagrantClient) Status() *StatusCommand
230244
```
231245

246+
Options:
247+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to retrieve
248+
the status for. If unspecified (default), the status of all VMs in the
249+
current directory will be returned.
250+
232251
Response:
233252
* **Status** - a map of vagrant machine names to a string describing the
234253
status of the VM.
@@ -242,6 +261,10 @@ Suspends the vagrant machine.
242261
func (*VagrantClient) Suspend() *SuspendCommand
243262
```
244263

264+
Options:
265+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to suspend.
266+
If unspecified (default), all VMs in the current directory will be suspended.
267+
245268
Response:
246269
* **Error** - Set if an error occurred.
247270

@@ -267,6 +290,9 @@ Options:
267290
* **Provider** (default: `""`) - Back the machine with a specific provider.
268291
* **InstallProvider** (default: `true`) - If possible, install the provider if
269292
it isn't installed.
293+
* **MachineName** (default: `""`) - The name or ID of a vagrant VM to bring up.
294+
If unspecified (default), all VMs in the current directory will be brought
295+
up.
270296

271297
Response:
272298
* **VMInfo** - a map of vagrant machine names to `VMInfo` objects. Each VMInfo

command_destroy.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// A DestroyCommand specifies the options and output of vagrant destroy.
44
type DestroyCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
ErrorResponse
78

89
// Destroy without confirmation (defaults to true because, when false,
@@ -34,7 +35,7 @@ func (cmd *DestroyCommand) buildArguments() []string {
3435
if cmd.Parallel {
3536
args = append(args, "--parallel")
3637
}
37-
return args
38+
return cmd.appendMachineName(args)
3839
}
3940

4041
func (cmd *DestroyCommand) init() error {

command_halt.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// A HaltCommand specifies the options and output of vagrant halt.
44
type HaltCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
ErrorResponse
78

89
// Force shutdown (equivalent to pulling the power from the machine, default:
@@ -25,7 +26,7 @@ func (cmd *HaltCommand) buildArguments() []string {
2526
if cmd.Force {
2627
args = append(args, "--force")
2728
}
28-
return args
29+
return cmd.appendMachineName(args)
2930
}
3031

3132
func (cmd *HaltCommand) init() error {

command_port.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package vagrant
22

3-
// PortCommand specifies the options and output of vagrant port.
3+
// PortCommand specifies the options and output of vagrant port. Note that
4+
// the port command is unique in that the MachineName option is required if
5+
// your Vagrantfile defines more than one VM!
46
type PortCommand struct {
57
BaseCommand
8+
MachineNameArgument
69
PortResponse
7-
8-
// MachineName is the vagrant machine you are interested in. If your
9-
// Vagrantfile only brings up a single machine, you do not need to specify
10-
// this. However, if your Vagrantfile brings up multiple machines, you MUST
11-
// specify this! For some reason, this is the only vagrant command that
12-
// cannot handle multiple machines.
13-
MachineName string
1410
}
1511

1612
// Port will return information about ports forwarded from the host to the

command_provision.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// ProvisionCommand specifies the options and output of vagrant provision
44
type ProvisionCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
ErrorResponse
78
ProvisionersArgument
89
}
@@ -18,7 +19,7 @@ func (client *VagrantClient) Provision() *ProvisionCommand {
1819
}
1920

2021
func (cmd *ProvisionCommand) init() error {
21-
args := cmd.buildArguments()
22+
args := cmd.appendMachineName(cmd.buildArguments())
2223
return cmd.BaseCommand.init(&cmd.ErrorResponse, "provision", args...)
2324
}
2425

command_reload.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// ReloadCommand specifies the options and output of vagrant reload
44
type ReloadCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
ErrorResponse
78
ProvisioningArgument
89
}
@@ -18,7 +19,7 @@ func (client *VagrantClient) Reload() *ReloadCommand {
1819
}
1920

2021
func (cmd *ReloadCommand) init() error {
21-
args := cmd.buildArguments()
22+
args := cmd.appendMachineName(cmd.buildArguments())
2223
return cmd.BaseCommand.init(&cmd.ErrorResponse, "reload", args...)
2324
}
2425

command_resume.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// A ResumeCommand specifies the options and output of vagrant resume.
44
type ResumeCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
ErrorResponse
78
ProvisioningArgument
89
}
@@ -18,7 +19,7 @@ func (client *VagrantClient) Resume() *ResumeCommand {
1819
}
1920

2021
func (cmd *ResumeCommand) init() error {
21-
args := cmd.buildArguments()
22+
args := cmd.appendMachineName(cmd.buildArguments())
2223
return cmd.BaseCommand.init(&cmd.ErrorResponse, "resume", args...)
2324
}
2425

command_sshconfig.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// SSHConfigCommand specifies options and output from vagrant ssh-config
44
type SSHConfigCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
SSHConfigResponse
78

89
// Name of a specific host to get SSH config info for. If not set, info for
@@ -26,7 +27,7 @@ func (cmd *SSHConfigCommand) buildArguments() []string {
2627
if cmd.Host != "" {
2728
args = append(args, "--host", cmd.Host)
2829
}
29-
return args
30+
return cmd.appendMachineName(args)
3031
}
3132

3233
func (cmd *SSHConfigCommand) init() error {

command_status.go

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// StatusCommand specifies options and output from vagrant status
44
type StatusCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
StatusResponse
78
}
89

@@ -17,6 +18,9 @@ func (client *VagrantClient) Status() *StatusCommand {
1718
}
1819

1920
func (cmd *StatusCommand) init() error {
21+
if cmd.MachineName != "" {
22+
return cmd.BaseCommand.init(&cmd.StatusResponse, "status", cmd.MachineName)
23+
}
2024
return cmd.BaseCommand.init(&cmd.StatusResponse, "status")
2125
}
2226

command_suspend.go

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// SuspendCommand specifies options and output from vagrant suspend
44
type SuspendCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
ErrorResponse
78
}
89

@@ -17,6 +18,9 @@ func (client *VagrantClient) Suspend() *SuspendCommand {
1718
}
1819

1920
func (cmd *SuspendCommand) init() error {
21+
if cmd.MachineName != "" {
22+
return cmd.BaseCommand.init(&cmd.ErrorResponse, "suspend", cmd.MachineName)
23+
}
2024
return cmd.BaseCommand.init(&cmd.ErrorResponse, "suspend")
2125
}
2226

command_up.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vagrant
33
// UpCommand specifies options and output from vagrant up.
44
type UpCommand struct {
55
BaseCommand
6+
MachineNameArgument
67
UpResponse
78
ProvisioningArgument
89

@@ -47,7 +48,7 @@ func (cmd *UpCommand) buildArguments() []string {
4748
if !cmd.InstallProvider {
4849
args = append(args, "--no-install-provider")
4950
}
50-
return args
51+
return cmd.appendMachineName(args)
5152
}
5253

5354
func (cmd *UpCommand) init() error {

machine_name_argument.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package vagrant
2+
3+
type MachineNameArgument struct {
4+
// MachineName is the name or ID of a vagrant VM to act on. If unspecified
5+
// (the default), vagrant will act upon all VMs in the current directory.
6+
MachineName string
7+
}
8+
9+
func (cmd *MachineNameArgument) appendMachineName(args []string) []string {
10+
if cmd.MachineName == "" {
11+
return args
12+
}
13+
return append(args, cmd.MachineName)
14+
}

machine_name_argument_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package vagrant
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMachineNameCommand_buildArguments(t *testing.T) {
8+
cmd := MachineNameArgument{MachineName: "test"}
9+
args := cmd.appendMachineName([]string{})
10+
assertArguments(t, args, "test")
11+
}

0 commit comments

Comments
 (0)