-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththosts
More file actions
executable file
·43 lines (35 loc) · 1.08 KB
/
thosts
File metadata and controls
executable file
·43 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
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
#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
require_relative './lib/hosts_file'
DEFAULT_PATH = '/etc/hosts'
options = OpenStruct.new
OptionParser.new do |opts|
opts.banner = 'Usage: thosts [options] [section_name]'
opts.on('-l', '--list', 'list sections and their status') { options.action = :list }
opts.on('-p', '--print', 'print all enabled lines (effective hosts file)') { options.action = :print }
opts.on('-o', '--out FILE', 'write to specified FILE instead of input file') do |file|
options.out = file
end
opts.on('-i', '--in FILE', 'read from specified FILE instead of system hosts file') do |file|
options.in = file
end
end.parse!
options.action ||= (ARGV.empty? ? :list : :toggle)
options.in ||= DEFAULT_PATH
options.out ||= options.in
hosts = HostsFile.new(options.in)
def save(hosts, file)
puts "Enabled sections:\n#{hosts.enabled_sections.join("\n")}"
hosts.save file
end
case options.action
when :list
puts hosts.summary
when :print
puts hosts.effective_hosts
when :toggle
name = ARGV[0]
hosts.toggle(name)
save hosts, options.out
end