class Roast::HostsFile
Constants
- DISABLED_PATTERN
- GROUP_PATTERN
- HOST_PATTERN
Attributes
path[R]
static_lines[R]
Public Class Methods
new(path = '/etc/hosts')
click to toggle source
# File lib/roast/hosts_file.rb, line 10 def initialize(path = '/etc/hosts') @path = path @static_lines = [] @groups = {} end
Public Instance Methods
[](group)
click to toggle source
# File lib/roast/hosts_file.rb, line 24 def [](group) @groups[group] end
add(group, source, hostname)
click to toggle source
# File lib/roast/hosts_file.rb, line 82 def add(group, source, hostname) group ||= 'base' @groups[group] ||= Group.new(group) @groups[group] << Host.new(source, hostname) end
delete(entry)
click to toggle source
# File lib/roast/hosts_file.rb, line 102 def delete(entry) results = delete_host(entry) results end
delete_group(group)
click to toggle source
# File lib/roast/hosts_file.rb, line 124 def delete_group(group) return false unless @groups.has_key?(group) @groups.delete(group) true end
delete_host(entry)
click to toggle source
# File lib/roast/hosts_file.rb, line 78 def delete_host(entry) groups.map { |g| g.delete_host(entry) }.flatten end
disable(entry)
click to toggle source
# File lib/roast/hosts_file.rb, line 95 def disable(entry) results = find_host(entry) results.each { |h| h.disable! } results end
disable_group(group)
click to toggle source
# File lib/roast/hosts_file.rb, line 116 def disable_group(group) return false unless @groups.has_key?(group) @groups[group].disable! true end
enable(entry)
click to toggle source
# File lib/roast/hosts_file.rb, line 88 def enable(entry) results = find_host(entry) results.each { |h| h.enable! } results end
enable_group(group)
click to toggle source
# File lib/roast/hosts_file.rb, line 108 def enable_group(group) return false unless @groups.has_key?(group) @groups[group].enable! true end
find_host(entry)
click to toggle source
# File lib/roast/hosts_file.rb, line 74 def find_host(entry) groups.map { |g| g.find_host(entry) }.flatten end
groups()
click to toggle source
# File lib/roast/hosts_file.rb, line 16 def groups @groups.values end
hosts()
click to toggle source
# File lib/roast/hosts_file.rb, line 20 def hosts groups.map { |g| g.hosts }.flatten end
list()
click to toggle source
# File lib/roast/hosts_file.rb, line 132 def list [path, groups] end
read()
click to toggle source
# File lib/roast/hosts_file.rb, line 28 def read in_group = false group = nil File.open(path, 'r') do |file| file.each_line do |line| if group_match = line.match(GROUP_PATTERN) in_group = true group = Group.new(group_match[1]) @groups[group.name] ||= group elsif group && host_match = line.match(HOST_PATTERN) host = Host.new(host_match[1], host_match[2]) host.alias = host_match[3] host.disable! if line =~ DISABLED_PATTERN group << host else static_lines << line end end end self rescue Errno::EACCES puts "Unable to read hosts file: `#{@path}', you might need to `sudo roast'" exit 1 end
write(output_path = nil)
click to toggle source
# File lib/roast/hosts_file.rb, line 55 def write(output_path = nil) output_path = output_path || path temp_file = Tempfile.new('hosts') temp_file << static_lines.join.sub(/\n{3,}\z/, "\n\n") temp_file << groups.map { |g| g.to_hosts_file.chomp }.join("\n\n") File.chmod(0644, temp_file.path) begin FileUtils.cp(path, path + '.roast.bak') if output_path.eql?(path) FileUtils.mv(temp_file.path, output_path, :force => true) rescue Errno::EACCES puts "Unable to write to hosts file: `#{@path}', you might need to `sudo roast'" exit 1 end ensure temp_file.close temp_file.unlink end