Class: Hosts::Lib::Core::HostsFile
- Inherits:
-
Object
- Object
- Hosts::Lib::Core::HostsFile
- Defined in:
- lib/Hosts/Lib/Core/HostsFile.rb
Instance Method Summary collapse
-
#add_entry(dest, hostnames, comment = nil) ⇒ Object
Add an entry.
-
#flush ⇒ Object
Flush changes made to hosts file.
- #get_all_entries ⇒ Object
-
#initialize(file_path) ⇒ HostsFile
constructor
A new instance of HostsFile.
- #parse_file(file_path) ⇒ Object
-
#rm_alias(dest, hostname) ⇒ Object
Return false if the hostname was not an alias for ‘dest’.
- #rm_dest_entries(dest) ⇒ Object
Constructor Details
#initialize(file_path) ⇒ HostsFile
Returns a new instance of HostsFile.
8 9 10 11 12 |
# File 'lib/Hosts/Lib/Core/HostsFile.rb', line 8 def initialize(file_path) @entries = {} @hosts_file_path = file_path parse_file(file_path) end |
Instance Method Details
#add_entry(dest, hostnames, comment = nil) ⇒ Object
Add an entry. Expects to strings
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/Hosts/Lib/Core/HostsFile.rb', line 90 def add_entry(dest, hostnames, comment = nil) comment = "" if comment == nil hostnames = [hostnames] if (!hostnames.is_a?(Array)) if (@entries.has_key?(dest)) # Add hostnames to entries, except duplicates, dont add them @entries[dest]["hostnames"] = @entries[dest]["hostnames"] | hostnames else @entries[dest] = {"hostnames" => hostnames, "comment" => comment} end end |
#flush ⇒ Object
Flush changes made to hosts file
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/Hosts/Lib/Core/HostsFile.rb', line 114 def flush() new_hosts_file_content = '' longest_dest_name = 0 @entries.each do |dest, obj| next unless (obj.has_key?("hostnames")) if (dest.length > longest_dest_name) longest_dest_name = dest.length end end longest_dest_name += 1 @entries.each do |dest, obj| if (obj.has_key?('comment') && !obj.has_key?('hostnames')) new_hosts_file_content << obj['comment'] << "\n" elsif (obj.has_key?('newline')) new_hosts_file_content << "\n" else new_hosts_file_content << ("%-#{longest_dest_name}.#{longest_dest_name}s" % dest) new_hosts_file_content << (obj['hostnames'] * ' ') if (obj['comment'].length > 0) comment = obj['comment'] # If it for some reason is not formated # as a comment unless (/^[\s\t]*?#/ =~ comment) comment = '#' + comment end new_hosts_file_content += ("\s" + comment) end new_hosts_file_content << "\n" end end File.open(@hosts_file_path, "w+").write(new_hosts_file_content) puts puts "new content of hosts file:" puts new_hosts_file_content puts end |
#get_all_entries ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/Hosts/Lib/Core/HostsFile.rb', line 103 def get_all_entries() _entries = {} @entries.each do |dest, obj| next if (!obj.has_key?("hostnames")) _entries[dest] = obj["hostnames"] end return _entries end |
#parse_file(file_path) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/Hosts/Lib/Core/HostsFile.rb', line 14 def parse_file(file_path) newline_count = 0 comment_count = 0 File.readlines(file_path).each do |line| # Add comments to @entries, to preserve them if (/^[\s\t]*?#/ =~ line) # Add comment to entries, minus the newline @entries["__comment__#{comment_count}"] = { "comment" => line[0..-2] } comment_count += 1 next end # Add empty lines to @entries, to preserve them if (/^[\s\t]*?$/ =~ line) @entries["__newline__#{newline_count}"] = { "newline" => "newline" } newline_count += 1 next end # Remove comment, if there is one line_and_comment = line.split("#") line = line_and_comment[0] comment = line_and_comment[1] # Remove newline from comment comment = comment[0..-2] if (comment != nil) parts = line.split(/[\s\t]+/) dest = parts[0] hostnames = parts[1..-1] # Remove last item if empty hostnames.pop if (hostnames.length == 0) hostnames = [hostnames] if (!hostnames.is_a?(Array)) if (hostnames == nil) fail InvalidHostsFileException.new "Missing hostsname from entry declaration \n #{line}\n" end if (dest == nil) fail InvalidHostsFileException.new "Missing destination from entry declaration \n #{line}\n" end add_entry(dest, hostnames, comment) end end |
#rm_alias(dest, hostname) ⇒ Object
Return false if the hostname was not an alias for ‘dest’
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/Hosts/Lib/Core/HostsFile.rb', line 65 def rm_alias(dest, hostname) return false if (!@entries.has_key?(dest)) # remove hostname from dest, and return # bool to indicate if it existed in the # first place was_deleted = @entries[dest]["hostnames"].delete(hostname) != nil; # remove dest from @entries # if it is now empty if (@entries[dest]["hostnames"].length == 0) @entries.delete(dest) end return was_deleted end |
#rm_dest_entries(dest) ⇒ Object
82 83 84 85 86 |
# File 'lib/Hosts/Lib/Core/HostsFile.rb', line 82 def rm_dest_entries(dest) if (@entries.has_key?(dest)) @entries.delete(dest) end end |