Class: Hostman::Executor

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

Instance Method Summary collapse

Instance Method Details

#execObject



8
9
10
11
12
13
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
62
# File 'lib/hostman.rb', line 8

def exec

  opts = Slop.parse do |o|
    o.string '-b', '--block', 'a hostname'
    o.string '-u', '--unblock', 'a hostname'
    o.string '-f', '--hosts-file', 'override hosts file path', default: '/etc/hosts'
    o.bool '-o', '--output-hosts', 'output hosts file contents after execution', default: false
    o.on '--version', 'print the version' do
      puts Hostman::VERSION
      exit
    end
  end

  opts.inspect
  begin
    raise ArgumentError, ::Hostman::BLOCK_UNBLOCK_ERROR if !opts[:block].nil? and !opts[:unblock].nil?
  rescue ArgumentError => err
    puts err
    exit
  end

  if !opts[:block].nil?
    not_blocked = true

    File.open(opts["hosts-file"], "r") do |f|
      f.each_line do |line|
        not_blocked = false if line == "127.0.0.1\t#{opts[:block]}\n"
      end
    end

    if not_blocked
      File.open(opts["hosts-file"], "a") do |f|
        f.puts "127.0.0.1\t#{opts[:block]}"
      end
    end
  end

  if !opts[:unblock].nil?
    contents = ""
    File.open(opts["hosts-file"], "r") do |f|
      f.each_line do |line|
        contents += line unless line == "127.0.0.1\t#{opts[:unblock]}\n"
      end
    end

    File.open(opts["hosts-file"], "w") do |f|
      f.puts contents
    end
  end

  if opts["output-hosts"]
    puts File.read(opts["hosts-file"])
  end

end