Raise an error if multiple matching Vagrantfiles are found [GH-588]

This commit is contained in:
Mitchell Hashimoto 2012-01-08 11:54:32 -08:00
parent 89eb256f9d
commit 8c00d1d652
3 changed files with 58 additions and 17 deletions

View File

@ -350,24 +350,22 @@ module Vagrant
# with Vagrant.
config_loader.set(:default, File.expand_path("config/default.rb", Vagrant.source_root))
vagrantfile_name.each do |rootfile|
if box
# We load the box Vagrantfile
box_vagrantfile = box.directory.join(rootfile)
config_loader.set(:box, box_vagrantfile) if box_vagrantfile.exist?
end
if box
# We load the box Vagrantfile
box_vagrantfile = find_vagrantfile(box.directory)
config_loader.set(:box, box_vagrantfile) if box_vagrantfile
end
if home_path
# Load the home Vagrantfile
home_vagrantfile = home_path.join(rootfile)
config_loader.set(:home, home_vagrantfile) if home_vagrantfile.exist?
end
if home_path
# Load the home Vagrantfile
home_vagrantfile = find_vagrantfile(home_path)
config_loader.set(:home, home_vagrantfile) if home_vagrantfile
end
if root_path
# Load the Vagrantfile in this directory
root_vagrantfile = root_path.join(rootfile)
config_loader.set(:root, root_vagrantfile) if root_vagrantfile.exist?
end
if root_path
# Load the Vagrantfile in this directory
root_vagrantfile = find_vagrantfile(root_path)
config_loader.set(:root, root_vagrantfile) if root_vagrantfile
end
if subvm
@ -477,5 +475,35 @@ module Vagrant
@default_private_key_path.chmod(0600)
end
end
# Finds the Vagrantfile in the given directory.
#
# This will raise an error if multiple matching Vagrantfiles are found.
# This can only occur on case sensitive file systems, but if there is a
# `Vagrantfile` and a `vagrantfile` it is not clear which Vagrant will
# load, so an error must be raised.
#
# @param [Pathname] path Path to search in.
# @return [Pathname]
def find_vagrantfile(search_path)
path = nil
@vagrantfile_name.each do |vagrantfile|
current_path = search_path.join(vagrantfile)
# If the path exists, then we verify we haven't found a match before,
# then we set the path we've found.
if current_path.exist?
# We also test if current_path == path because on case insensitive
# file systems, it will look like multiple exist.
if path && current_path == path
raise Errors::MultiVagrantfileFound, :directory => search_path.to_s
end
path = current_path
end
end
path
end
end
end

View File

@ -193,6 +193,11 @@ module Vagrant
error_key(:port_collision_resume)
end
class MultiVagrantfileFound < VagrantError
status_code(67)
error_key(:multi_vagrantfile_found)
end
class MultiVMEnvironmentRequired < VagrantError
status_code(5)
error_key(:multi_vm_required)

View File

@ -65,7 +65,15 @@ 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_vagrantfile_found: |-
Multiple Vagrantfiles were found in the following directory. This usually
occurs when multiple Vagrantfiles with different casing are found. For
example: Vagrantfile and vagrantfile. Because the behavior in this case
is undefined, please fix it so that there is only one matching Vagrantfile.
%{directory}
multi_vm_required: |-
A multi-vm environment is required for name specification to this command.
multi_vm_target_required: |-
This command requires a specific VM name to target in a multi-VM environment.
no_env: |-