use more I18n and do not repeat ourselves while doing config checks
This commit is contained in:
parent
c98ddd338e
commit
3a96f5e170
|
@ -15,29 +15,45 @@ module VagrantPlugins
|
|||
|
||||
def finalize!
|
||||
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
|
||||
@aliases = [ @aliases ].flatten
|
||||
end
|
||||
|
||||
def validate(machine)
|
||||
errors = Array.new
|
||||
|
||||
# check if enabled option is either true or false
|
||||
if ![TrueClass, FalseClass].include?(enabled.class)
|
||||
errors << "A value for hostmanager.enabled can be true or false."
|
||||
end
|
||||
errors << validate_bool('hostmanager.enabled', enabled)
|
||||
|
||||
# check if ignore_private_ip option is either true or false
|
||||
if ![TrueClass, FalseClass].include?(ignore_private_ip.class) &&
|
||||
@ignore_private_ip != UNSET_VALUE
|
||||
errors << "A value for hostmanager.ignore_private_ip can be true or false."
|
||||
# check if ignore_private_ip option is either true or false (or UNSET_VALUE)
|
||||
if @ignore_private_ip != UNSET_VALUE
|
||||
errors << validate_bool('hostmanager.ignore_private_ip', ignore_private_ip)
|
||||
end
|
||||
|
||||
# check if aliases option is an Array
|
||||
if !machine.config.hostmanager.aliases.kind_of?(Array)
|
||||
errors << "A value for hostmanager.aliases must be an Array."
|
||||
if !machine.config.hostmanager.aliases.kind_of?(Array) and
|
||||
!machine.config.hostmanager.aliases.kind_of?(String)
|
||||
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
|
||||
:config_key => 'hostmanager.aliases',
|
||||
:is_class => aliases.class.to_s,
|
||||
})
|
||||
end
|
||||
|
||||
errors.compact!
|
||||
{ "HostManager configuration" => errors }
|
||||
end
|
||||
|
||||
private
|
||||
def validate_bool(key, value)
|
||||
if ![TrueClass, FalseClass].include?(value.class)
|
||||
I18n.t('vagrant_hostmanager.config.not_a_bool', {
|
||||
:config_key => key,
|
||||
:value => value.class.to_s,
|
||||
})
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,7 +32,11 @@ module VagrantPlugins
|
|||
host = machine.config.vm.hostname || name
|
||||
ip = get_ip_address.call(machine)
|
||||
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
|
||||
machine.env.ui.info "Adding /etc/hosts entry: #{ip} #{host} #{host_aliases}"
|
||||
machine.env.ui.info I18n.t('vagrant_hostmanager.action.add_host', {
|
||||
:ip => ip,
|
||||
:host => host,
|
||||
:aliases => host_aliases,
|
||||
})
|
||||
file << "#{ip}\t#{host}\s#{host_aliases}\n"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
en:
|
||||
vagrant_hostmanager:
|
||||
action:
|
||||
add_host: "Adding /etc/hosts entry: %{ip} %{host} %{aliases}"
|
||||
update: "[%{name}] Updating /etc/hosts file"
|
||||
config:
|
||||
not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'"
|
||||
not_an_array_or_string: "A value for %{config_key} must be an Array or String, not type '%{is_class}'"
|
||||
|
|
Loading…
Reference in New Issue