Module: PortMap::Hosts

Defined in:
lib/port_map/hosts.rb

Constant Summary collapse

PORT_MAP_TRACK_COMMENT =
'#port_map'.freeze
HOSTS_FILENAME =
'/etc/hosts'.freeze

Class Method Summary collapse

Class Method Details

.contentsObject



21
22
23
24
25
# File 'lib/port_map/hosts.rb', line 21

def self.contents
  File.readlines(HOSTS_FILENAME).reject do |line|
    line.strip.match(/#{PORT_MAP_TRACK_COMMENT}$/)
  end.join.strip
end

.save(new_contents) ⇒ Object



17
18
19
# File 'lib/port_map/hosts.rb', line 17

def self.save(new_contents)
  File.open(HOSTS_FILENAME, 'w+') { |f| f.write(new_contents) }
end

.updateObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/port_map/hosts.rb', line 6

def self.update
  new_contents = contents
  port_maps = JSON.parse(`list_port_maps`)

  unless port_maps.empty?
    new_contents += "\n127.0.0.1 #{port_maps.map { |port_map| port_map['server_name'] }.join(' ')} #{PORT_MAP_TRACK_COMMENT}"
  end

  save(new_contents)
end