Class: Buildhosts::Main
- Inherits:
-
Object
- Object
- Buildhosts::Main
- Defined in:
- lib/buildhosts.rb
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize ⇒ Main
constructor
A new instance of Main.
Constructor Details
#initialize ⇒ Main
Returns a new instance of Main.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/buildhosts.rb', line 9 def initialize @path = File.('../../files/', __FILE__) @files = Hash.new ['config', 'custom', 'header', 'newhosts', 'temp'].each do |f| if (File.exist?(File.("~/.buildhosts/#{f}"))) @files[f] = File.("~/.buildhosts/#{f}").chomp else @files[f] = "#{@path}/#{f}".chomp end end end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
6 7 8 |
# File 'lib/buildhosts.rb', line 6 def files @files end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/buildhosts.rb', line 7 def path @path end |
Instance Method Details
#build ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/buildhosts.rb', line 21 def build # Prefixy strings ipv6 = '::1 ' home = '127.0.0.1 ' # Grab that parser and parse conf = Buildhosts::ConfigParser.parse @files['config'] # Write the lines to a "newhosts" file out = File.open(@files['newhosts'], 'w+') conf['hosts'].each do |host| out.write "#{ipv6}#{host}\n" out.write "#{home}#{host}\n" conf['ips'].each do |ip| out.write "#{ipv6}#{host}.#{ip}.xip.io\n" out.write "#{home}#{host}.#{ip}.xip.io\n" end end out.close system("cat #{@files['header']} #{@files['custom']} #{@files['newhosts']} > #{@files['temp']}") system("sudo cp #{@files['temp']} /etc/hosts") system("rm #{@files['newhosts']} #{@files['temp']}") end |