From 3a96f5e170844011deeb24fcb1dc993a322f9d32 Mon Sep 17 00:00:00 2001 From: Jan Vansteenkiste Date: Thu, 2 May 2013 21:26:05 +0200 Subject: [PATCH] use more I18n and do not repeat ourselves while doing config checks --- lib/vagrant-hostmanager/config.rb | 34 ++++++++++++++++++++------- lib/vagrant-hostmanager/hosts_file.rb | 6 ++++- locales/en.yml | 4 ++++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index feeeeaa..20be3c8 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -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 diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index e7b4ce2..d45d770 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -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 diff --git a/locales/en.yml b/locales/en.yml index f8d2ea2..337d096 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -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}'"