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!
|
def finalize!
|
||||||
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
|
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
|
||||||
|
@aliases = [ @aliases ].flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate(machine)
|
def validate(machine)
|
||||||
errors = Array.new
|
errors = Array.new
|
||||||
|
|
||||||
# check if enabled option is either true or false
|
# check if enabled option is either true or false
|
||||||
if ![TrueClass, FalseClass].include?(enabled.class)
|
errors << validate_bool('hostmanager.enabled', enabled)
|
||||||
errors << "A value for hostmanager.enabled can be true or false."
|
|
||||||
end
|
|
||||||
|
|
||||||
# check if ignore_private_ip option is either true or false
|
# check if ignore_private_ip option is either true or false (or UNSET_VALUE)
|
||||||
if ![TrueClass, FalseClass].include?(ignore_private_ip.class) &&
|
if @ignore_private_ip != UNSET_VALUE
|
||||||
@ignore_private_ip != UNSET_VALUE
|
errors << validate_bool('hostmanager.ignore_private_ip', ignore_private_ip)
|
||||||
errors << "A value for hostmanager.ignore_private_ip can be true or false."
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# check if aliases option is an Array
|
# check if aliases option is an Array
|
||||||
if !machine.config.hostmanager.aliases.kind_of?(Array)
|
if !machine.config.hostmanager.aliases.kind_of?(Array) and
|
||||||
errors << "A value for hostmanager.aliases must be an Array."
|
!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
|
end
|
||||||
|
|
||||||
|
errors.compact!
|
||||||
{ "HostManager configuration" => errors }
|
{ "HostManager configuration" => errors }
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,11 @@ module VagrantPlugins
|
||||||
host = machine.config.vm.hostname || name
|
host = machine.config.vm.hostname || name
|
||||||
ip = get_ip_address.call(machine)
|
ip = get_ip_address.call(machine)
|
||||||
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
|
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"
|
file << "#{ip}\t#{host}\s#{host_aliases}\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
en:
|
en:
|
||||||
vagrant_hostmanager:
|
vagrant_hostmanager:
|
||||||
action:
|
action:
|
||||||
|
add_host: "Adding /etc/hosts entry: %{ip} %{host} %{aliases}"
|
||||||
update: "[%{name}] Updating /etc/hosts file"
|
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