Class: Buildhosts::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/buildhosts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

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.expand_path('../../files/', __FILE__)
    @files = Hash.new
    ['config', 'custom', 'header', 'newhosts', 'temp'].each do |f|
        if (File.exist?(File.expand_path("~/.buildhosts/#{f}")))
            @files[f] = File.expand_path("~/.buildhosts/#{f}").chomp
        else
            @files[f] = "#{@path}/#{f}".chomp
        end
    end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/buildhosts.rb', line 6

def files
  @files
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/buildhosts.rb', line 7

def path
  @path
end

Instance Method Details

#buildObject



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