simonmenke-hosts 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/bin/hosts +28 -40
- data/lib/hosts/version.rb +1 -1
- data/lib/hosts.rb +99 -0
- metadata +2 -2
data/History.txt
CHANGED
data/bin/hosts
CHANGED
@@ -2,38 +2,46 @@
|
|
2
2
|
|
3
3
|
require "rubygems"
|
4
4
|
require "thor"
|
5
|
+
require "hosts"
|
5
6
|
|
6
|
-
class
|
7
|
+
class HostsCLI < Thor
|
7
8
|
|
8
9
|
desc "list [SEARCH]", "List host names"
|
9
10
|
def list(search="")
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
Hosts.read
|
12
|
+
results = Hosts.search(search)
|
13
|
+
width_col_1 = results.collect{|entry| entry[0].size}.max
|
14
|
+
width_col_2 = results.collect{|entry| entry[1].size}.max
|
15
|
+
results.each do |entry|
|
16
|
+
puts "%s%s %s%s %s" % [
|
17
|
+
entry[0], " "*(width_col_1-entry[0].size),
|
18
|
+
entry[1], " "*(width_col_2-entry[1].size),
|
19
|
+
(entry[2] ? "" : "(locked)")]
|
13
20
|
end
|
14
21
|
end
|
15
22
|
|
16
23
|
desc "add HOSTNAME [ADDRESS]", "Add a hostname"
|
17
24
|
def add(hostname, address="127.0.0.1")
|
18
|
-
|
19
|
-
|
20
|
-
|
25
|
+
Hosts.read
|
26
|
+
if Hosts.editable?(hostname, address)
|
27
|
+
Hosts.add(hostname, address)
|
28
|
+
Hosts.write
|
29
|
+
puts "Added: #{address} #{hostname}"
|
30
|
+
else
|
31
|
+
puts "#{address} #{hostname} is locked!"
|
21
32
|
end
|
22
|
-
@lines.reject! { |line| line =~ Regexp.new("\s+#{hostname}$") }
|
23
|
-
@lines << [address, hostname].join(" ")
|
24
|
-
dump
|
25
|
-
puts "Added: #{hostname} - #{address}"
|
26
33
|
end
|
27
34
|
|
28
|
-
desc "remove HOSTNAME", "Remove a hostname"
|
29
|
-
def remove(hostname)
|
30
|
-
|
31
|
-
|
32
|
-
|
35
|
+
desc "remove HOSTNAME [ADDRESS]", "Remove a hostname"
|
36
|
+
def remove(hostname, address="127.0.0.1")
|
37
|
+
Hosts.read
|
38
|
+
if Hosts.editable?(hostname, address)
|
39
|
+
Hosts.remove(hostname, address)
|
40
|
+
Hosts.write
|
41
|
+
puts "Removed: #{address} #{hostname}"
|
42
|
+
else
|
43
|
+
puts "#{address} #{hostname} is locked!"
|
33
44
|
end
|
34
|
-
@lines.reject! { |line| line =~ Regexp.new("\s+#{hostname}$") }
|
35
|
-
dump
|
36
|
-
puts "Removed: #{host.last} - #{host.first}"
|
37
45
|
end
|
38
46
|
|
39
47
|
alias_method :l, :list
|
@@ -41,26 +49,6 @@ class Hosts < Thor
|
|
41
49
|
alias_method :r, :remove
|
42
50
|
alias_method :h, :help
|
43
51
|
|
44
|
-
protected
|
45
|
-
|
46
|
-
def hosts(search="")
|
47
|
-
@lines ||= File.readlines('/etc/hosts').collect{ |line| line.strip }
|
48
|
-
hosts = @lines.reject do |line|
|
49
|
-
line =~ /^\s+|#.*$/
|
50
|
-
end.collect { |line| line.split(/\s+/) }
|
51
|
-
hosts.select { |host| host.last =~ Regexp.new("^#{search}") }.sort { |a,b| a.last <=> b.last}
|
52
|
-
end
|
53
|
-
|
54
|
-
def dump
|
55
|
-
hosts # preload
|
56
|
-
begin
|
57
|
-
File.open('/etc/hosts', 'w+') { |f| @lines.each { |l| f.puts l } }
|
58
|
-
rescue => e
|
59
|
-
puts e
|
60
|
-
exit(1)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
52
|
end
|
65
53
|
|
66
54
|
begin
|
@@ -71,4 +59,4 @@ rescue
|
|
71
59
|
end
|
72
60
|
|
73
61
|
ARGV << "list" if ARGV.empty?
|
74
|
-
|
62
|
+
HostsCLI.start
|
data/lib/hosts/version.rb
CHANGED
data/lib/hosts.rb
CHANGED
@@ -3,4 +3,103 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
3
3
|
|
4
4
|
module Hosts
|
5
5
|
|
6
|
+
def self.search(pattern='')
|
7
|
+
regex = Regexp.new(pattern)
|
8
|
+
results = []
|
9
|
+
addresses.each do |address, entries|
|
10
|
+
if address =~ regex
|
11
|
+
entries.each do |hostname, editable|
|
12
|
+
results << [address, hostname, editable]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
hostnames.each do |hostname, entries|
|
17
|
+
if hostname =~ regex
|
18
|
+
entries.each do |address, editable|
|
19
|
+
results << [address, hostname, editable]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
results.uniq.sort do |a,b|
|
24
|
+
r = 0
|
25
|
+
r = a[0] <=> b[0] if r == 0
|
26
|
+
r = a[1] <=> b[1] if r == 0
|
27
|
+
r
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.hostnames
|
32
|
+
@hostnames ||= {}
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.addresses
|
36
|
+
@addresses ||= {}
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.hostname(hostname)
|
40
|
+
hostnames[hostname] ||= {}
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.address(address)
|
44
|
+
addresses[address] ||= {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.editable?(hostname, address)
|
48
|
+
return true if hostname(hostname).nil? or hostname(hostname)[address].nil?
|
49
|
+
return hostname(hostname)[address]
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.add(hostname, address, editable=true)
|
53
|
+
if editable?(hostname, address)
|
54
|
+
address(address)[hostname] = editable
|
55
|
+
hostname(hostname)[address] = editable
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.remove(hostname, address)
|
60
|
+
if editable?(hostname, address)
|
61
|
+
hostname(hostname).delete(address)
|
62
|
+
address(address).delete(hostname)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.lines
|
67
|
+
@lines ||= []
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.read
|
71
|
+
editable = false
|
72
|
+
lines.clear
|
73
|
+
File.readlines('/etc/hosts').collect{ |line| line.strip }.each do |line|
|
74
|
+
lines << line unless editable
|
75
|
+
editable = true if line == '### AUTOGENERATED HOST ENTRIES ###'
|
76
|
+
next if line =~ /^\s+|#.*$/
|
77
|
+
|
78
|
+
segements = line.split(/\s+/)
|
79
|
+
address = segements.shift
|
80
|
+
segements.each do |segement|
|
81
|
+
add(segement, address, editable)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
lines << '### AUTOGENERATED HOST ENTRIES ###' unless editable
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.write
|
88
|
+
begin
|
89
|
+
File.open('/etc/hosts', 'w+') do |f|
|
90
|
+
lines.each { |l| f.puts l }
|
91
|
+
addresses.each do |address, hostnames|
|
92
|
+
editable_hostnames = []
|
93
|
+
hostnames.each do |hostname, editable|
|
94
|
+
editable_hostnames << hostname if editable
|
95
|
+
end
|
96
|
+
f.puts "#{address} #{editable_hostnames.join(' ')}" unless editable_hostnames.empty?
|
97
|
+
end
|
98
|
+
end
|
99
|
+
rescue => e
|
100
|
+
puts e
|
101
|
+
exit(1)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
6
105
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simonmenke-hosts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Menke
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-05-
|
12
|
+
date: 2008-05-26 00:00:00 -07:00
|
13
13
|
default_executable: hosts
|
14
14
|
dependencies: []
|
15
15
|
|