Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions lib/linux_admin/hosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,14 @@ def save
File.write(@filename, @raw_lines.join("\n"))
end

def update_entry(address, hostname, comment = nil)
# Delete entries for this hostname first
@parsed_file.each {|i| i[:hosts].to_a.delete(hostname)}
def add_alias(address, hostname, comment = nil)
add_name(address, hostname, false, comment)
end

# Add entry
line_number = @parsed_file.find_path(address).first
alias_method :update_entry, :add_alias

if line_number.blank?
@parsed_file.push({:address => address, :hosts => [hostname], :comment => comment})
else
new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.push(hostname)
@parsed_file.store_path(line_number, :hosts, new_hosts)
@parsed_file.store_path(line_number, :comment, comment) if comment
end
def set_canonical_hostname(address, hostname, comment = nil)
add_name(address, hostname, true, comment)
end

def hostname=(name)
Expand All @@ -52,7 +46,28 @@ def hostname
result.success? ? result.output.strip : nil
end

private
private

def add_name(address, hostname, fqdn, comment)
# Delete entries for this hostname first
@parsed_file.each { |i| i[:hosts].to_a.delete(hostname) }

# Add entry
line_number = @parsed_file.find_path(address).first

if line_number.blank?
@parsed_file.push(:address => address, :hosts => [hostname], :comment => comment)
else
if fqdn
new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.unshift(hostname)
else
new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.push(hostname)
end
@parsed_file.store_path(line_number, :hosts, new_hosts)
@parsed_file.store_path(line_number, :comment, comment) if comment
end
end

def parse_file
@parsed_file = []
@raw_lines.each { |line| @parsed_file.push(parse_line(line.strip)) }
Expand Down
21 changes: 21 additions & 0 deletions spec/hosts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@
end
end

describe "#set_canonical_hostname" do
it "removes an existing entry and creates a new one" do
expected_hash = [{:blank => true},
{:comment => "Some Comment"},
{:address => "127.0.0.1", :hosts => ["localhost", "localhost.localdomain"], :comment => "with a comment"},
{:address => "127.0.1.1", :hosts => []},
{:address => "1.2.3.4", :hosts => ["my.domain.local"], :comment => nil}]
@instance.set_canonical_hostname("1.2.3.4", "my.domain.local")
expect(@instance.parsed_file).to eq(expected_hash)
end

it "adds the hostname to the start of the hosts list" do
expected_hash = [{:blank => true},
{:comment => "Some Comment"},
{:address => "127.0.0.1", :hosts => ["examplehost.example.com", "localhost", "localhost.localdomain"], :comment => "with a comment"},
{:address => "127.0.1.1", :hosts => ["my.domain.local"]}]
@instance.set_canonical_hostname("127.0.0.1", "examplehost.example.com")
expect(@instance.parsed_file).to eq(expected_hash)
end
end

describe "#save" do
before do
allow(File).to receive(:write)
Expand Down