lib/vagrant-betterhosts/BetterHosts.rb in vagrant-betterhosts-0.1.0 vs lib/vagrant-betterhosts/BetterHosts.rb in vagrant-betterhosts-0.2.0

- old
+ new

@@ -69,24 +69,28 @@ def getHostnames(ips) hostnames = Hash.new { |h, k| h[k] = [] } case @machine.config.betterhosts.aliases when Array - # simple list of aliases to link to all ips - ips.each do |ip| - hostnames[ip] += @machine.config.betterhosts.aliases - end + hostnames[ips[0]] += @machine.config.betterhosts.aliases when Hash # complex definition of aliases for various ips @machine.config.betterhosts.aliases.each do |ip, hosts| hostnames[ip] += Array(hosts) end end - return hostnames + # handle default hostname(s) if not already specified in the aliases + Array(@machine.config.vm.hostname).each do |host| + next unless hostnames.none? { |_, v| v.include?(host) } + + hostnames[ips[0]].unshift host + end + + hostnames end - + def disableClean(ip_address) unless ip_address.nil? return @machine.config.betterhosts.disable_clean end return true @@ -103,10 +107,11 @@ hostnames_by_ips.each do |ip_address, hostnames| if ip_address.nil? @ui.error "[vagrant-betterhosts] Error adding some hosts, no IP was provided for the following hostnames: #{hostnames}" next end + @ui.info "[vagrant-betterhosts] Adding #{hostnames} for address #{ip_address}" if cli.include? ".exe" clean = "\"--clean\"," if disableClean(ip_address) clean = '' end @@ -173,40 +178,19 @@ end end end def generateHostnamesByIps() - hostnames_by_ips = [] ips = getIps - if ips.count() < 1 - return hostnames_by_ips - end + return [] unless ips.any? + + hostnames_by_ips = {} hostnames = getHostnames(ips) - if ips.count() > 1 - ips.each do |ip| - ip_address = ip - if hostnames[ip].count() > 0 - hostnames[ip].each do |hostname| - if !ip_address.nil? - @ui.info "[vagrant-betterhosts] - found entry for: #{ip_address} #{hostname}" - end - end - hostnames_by_ips = { ip_address => hostnames[ip].join(" ") } - end - end - else - ip_address = ips[0] - if hostnames[ip_address].count() > 0 - hostnames[ip_address].each do |hostname| - if !ip_address.nil? - @ui.info "[vagrant-betterhosts] - found entry for: #{ip_address} #{hostname}" - end - end - hostnames_by_ips = { ip_address => hostnames[ip_address].join(" ") } - end + ips.each do |ip| + hostnames_by_ips[ip] = hostnames[ip].join(' ') if hostnames[ip].any? end - return hostnames_by_ips + hostnames_by_ips end end end end