Invalid configuration keys are caught and shown in user friendly way

[GH-1484]
This commit is contained in:
Mitchell Hashimoto 2013-04-03 16:18:37 -07:00
parent fc708bc139
commit 4ffc2c3b74
12 changed files with 63 additions and 6 deletions

View File

@ -22,6 +22,7 @@ IMPROVEMENTS:
them altogether. [GH-1004]
- Specify the default provider with the `VAGRANT_DEFAULT_PROVIDER`
environmental variable. [GH-1478]
- Invalid settings are now caught and shown in a user-friendly way. [GH-1484]
BUG FIXES:

View File

@ -1,3 +1,5 @@
require "set"
module Vagrant
module Plugin
module V2
@ -57,9 +59,27 @@ module Vagrant
end
end
# Persist through the set of invalid methods
this_invalid = @__invalid_methods || Set.new
other_invalid = other.instance_variable_get(:"@__invalid_methods") || Set.new
result.instance_variable_set(:"@__invalid_methods", this_invalid + other_invalid)
result
end
# Capture all bad configuration calls and save them for an error
# message later during validation.
def method_missing(name, *args, &block)
name = name.to_s
name = name[0...-1] if name.end_with?("=")
@__invalid_methods ||= Set.new
@__invalid_methods.add(name)
# Return the dummy object so that anything else works
::Vagrant::Config::V2::DummyConfig.new
end
# Allows setting options from a hash. By default this simply calls
# the `#{key}=` method on the config class with the value, which is
# the expected behavior most of the time.
@ -79,6 +99,11 @@ module Vagrant
instance_variables_hash.to_json(*a)
end
# A default to_s implementation.
def to_s
self.class.to_s
end
# Returns the instance variables as a hash of key-value pairs.
def instance_variables_hash
instance_variables.inject({}) do |acc, iv|
@ -94,6 +119,16 @@ module Vagrant
# validated.
# @return [Hash]
def validate(machine)
return { self.to_s => _detected_errors }
end
# This returns any automatically detected errors.
#
# @return [Array<String>]
def _detected_errors
return [] if !@__invalid_methods || @__invalid_methods.empty?
return [I18n.t("vagrant.config.common.bad_field",
:fields => @__invalid_methods.to_a.sort.join(", "))]
end
end
end

View File

@ -5,6 +5,10 @@ module VagrantPlugins
class NFSConfig < Vagrant.plugin("2", :config)
attr_accessor :map_uid
attr_accessor :map_gid
def to_s
"NFS"
end
end
end
end

View File

@ -4,6 +4,10 @@ module VagrantPlugins
module Kernel_V2
class PackageConfig < Vagrant.plugin("2", :config)
attr_accessor :name
def to_s
"Package"
end
end
end
end

View File

@ -40,8 +40,12 @@ module VagrantPlugins
@username = nil if @username == UNSET_VALUE
end
def to_s
"SSH"
end
def validate(machine)
errors = []
errors = _detected_errors
[:max_tries, :timeout].each do |field|
value = instance_variable_get("@#{field}".to_sym)
@ -54,7 +58,7 @@ module VagrantPlugins
end
# Return the errors
{ "ssh" => errors }
{ to_s => errors }
end
end
end

View File

@ -4,6 +4,10 @@ module VagrantPlugins
module Kernel_V2
class VagrantConfig < Vagrant.plugin("2", :config)
attr_accessor :host
def to_s
"Vagrant"
end
end
end
end

View File

@ -268,7 +268,7 @@ module VagrantPlugins
end
def validate(machine)
errors = []
errors = _detected_errors
errors << I18n.t("vagrant.config.vm.box_missing") if !box
errors << I18n.t("vagrant.config.vm.box_not_found", :name => box) if \
box && !box_url && !machine.box

View File

@ -77,6 +77,10 @@ module VagrantPlugins
# The default name is just nothing, and we default it
@name = nil if @name == UNSET_VALUE
end
def to_s
"VirtualBox"
end
end
end
end

View File

@ -23,7 +23,7 @@ module VagrantPlugins
def encrypted_data_bag_secret; @encrypted_data_bag_secret || "/tmp/encrypted_data_bag_secret"; end
def validate(machine)
errors = []
errors = _detected_errors
errors << I18n.t("vagrant.config.chef.server_url_empty") if \
!chef_server_url || chef_server_url.strip == ""
errors << I18n.t("vagrant.config.chef.validation_key_path") if \

View File

@ -52,7 +52,7 @@ module VagrantPlugins
end
def validate(machine)
errors = []
errors = _detected_errors
errors << I18n.t("vagrant.config.chef.cookbooks_path_empty") if \
!cookbooks_path || [cookbooks_path].flatten.empty?
errors << I18n.t("vagrant.config.chef.run_list_empty") if \

View File

@ -21,7 +21,7 @@ module VagrantPlugins
end
def validate(machine)
errors = []
errors = _detected_errors
# Validate that the parameters are properly set
if path && inline

View File

@ -471,6 +471,7 @@ en:
#-------------------------------------------------------------------------------
config:
common:
bad_field: "The following settings don't exist: %{fields}"
error_empty: "`%{field}` must be not be empty."
chef:
cookbooks_path_empty: "Must specify a cookbooks path for chef solo."