Skip to content

Commit

Permalink
Merge pull request #13 from vStone/feature/i18n_more_things
Browse files Browse the repository at this point in the history
use more I18n and do not repeat ourselves while doing config checks
  • Loading branch information
smdahlen committed May 7, 2013
2 parents 51dfbe4 + 3a96f5e commit 1715eca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
34 changes: 25 additions & 9 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,45 @@ def initialize

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
6 changes: 5 additions & 1 deletion lib/vagrant-hostmanager/hosts_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def generate(env, provider)
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
Expand Down
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
@@ -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}'"

0 comments on commit 1715eca

Please sign in to comment.