Make requiring a single VM target in a command more DRY

This commit is contained in:
Mitchell Hashimoto 2011-12-26 17:59:18 -08:00
parent 0887a18079
commit ad7f4c79ad
5 changed files with 11 additions and 16 deletions

View File

@ -59,7 +59,9 @@ module Vagrant
# specific VM name is specified.
#
# @param [String] name The name of the VM. Nil if every VM.
def with_target_vms(name=nil)
# @param [Boolean] single_target If true, then an exception will be
# raised if more than one target is found.
def with_target_vms(name=nil, single_target=false)
# Using VMs requires a Vagrant environment to be properly setup
raise Errors::NoEnvironmentError if !@env.root_path
@ -73,6 +75,9 @@ module Vagrant
vms = @env.vms_ordered
end
# Make sure we're only working with one VM if single target
raise Errors::MultiVMTargetRequired if single_target && vms.length != 1
# Go through each VM and yield it!
vms.each do |old_vm|
# We get a new VM from the environment here to avoid potentially

View File

@ -51,12 +51,7 @@ module Vagrant
end
def package_target(name, options)
if @env.multivm? && name.nil?
# In a multi-VM environment, a name is required.
raise Errors::MultiVMTargetRequired, :command => "package"
end
with_target_vms(name) do |vm|
with_target_vms(name, true) do |vm|
raise Errors::VMNotCreatedError if !vm.created?
@logger.debug("Packaging VM: #{vm.name}")
package_vm(vm, options)

View File

@ -19,11 +19,8 @@ module Vagrant
argv = parse_options(opts)
return if !argv
# SSH always requires a target VM
raise Errors::MultiVMTargetRequired, :command => "ssh" if @env.multivm? && !argv[0]
# Execute the actual SSH
with_target_vms(argv[0]) do |vm|
with_target_vms(argv[0], true) do |vm|
# Basic checks that are required for proper SSH
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.state == :inaccessible

View File

@ -19,10 +19,7 @@ module Vagrant
argv = parse_options(opts)
return if !argv
# SSH-config always requires a target VM
raise Errors::MultiVMTargetRequired, :command => "ssh_config" if @env.multivm? && !argv[0]
with_target_vms(argv[0]) do |vm|
with_target_vms(argv[0], true) do |vm|
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.vm.accessible?

View File

@ -67,7 +67,8 @@ en:
You specified: %{home_path}
interrupted: "Vagrant exited after cleanup due to external interrupt."
multi_vm_required: "A multi-vm environment is required for name specification to this command."
multi_vm_target_required: "`vagrant %{command}` requires a specific VM name to target in a multi-VM environment."
multi_vm_target_required: |-
This command requires a specific VM name to target in a multi-VM environment.
no_env: |-
A Vagrant environment is required to run this command. Run `vagrant init`
to set one up.