ghost 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/bin/ghost CHANGED
@@ -8,12 +8,12 @@ begin
8
8
  rescue LoadError
9
9
  # no rubygems to load, so we fail silently
10
10
  end
11
- require 'ghost'
11
+ require 'lib/ghost'
12
12
 
13
13
  def help_text(exit_code = 0)
14
14
  script_name = File.basename $0
15
15
  puts """USAGE: #{script_name} add <hostname> [<ip=127.0.1.1>]
16
- #{script_name} modify <hostname> <ip>
16
+ #{script_name} modify <hostname> <ip> OR <hostname> (will lookup ip)
17
17
  #{script_name} delete <hostname>
18
18
  #{script_name} delete_matching <pattern>
19
19
  #{script_name} list
@@ -35,7 +35,10 @@ else
35
35
  host = Host.add(*ARGV)
36
36
  puts " [Adding] #{host.name} -> #{host.ip}"
37
37
  exit 0
38
- rescue
38
+ rescue SocketError
39
+ $stderr.puts "Cannot find ip for host"
40
+ exit 3
41
+ rescue StandardError
39
42
  $stderr.puts "Cannot overwrite an existing entry. Use the modify subcommand"
40
43
  exit 3
41
44
  end
@@ -37,6 +37,11 @@ class Host
37
37
  def add(host, ip = "127.0.0.1", force = false)
38
38
  if find_by_host(host).nil? || force
39
39
  delete(host)
40
+
41
+ unless ip[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/]
42
+ ip = Socket.gethostbyname(ip)[3].bytes.to_a.join('.')
43
+ end
44
+
40
45
  new_host = Host.new(host, ip)
41
46
 
42
47
  hosts = list
@@ -1,3 +1,5 @@
1
+ require 'socket'
2
+
1
3
  class Host
2
4
  ListCmd = "dscl localhost -list /Local/Default/Hosts 2>&1"
3
5
  ReadCmd = "dscl localhost -read /Local/Default/Hosts/%s 2>&1"
@@ -15,6 +17,10 @@ class Host
15
17
 
16
18
  def add(host, ip = "127.0.0.1", force = false)
17
19
  if find_by_host(host).nil? || force
20
+ unless ip[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/]
21
+ ip = Socket.gethostbyname(ip)[3].bytes.to_a.join('.')
22
+ end
23
+
18
24
  `#{CreateCmd % [host, ip]}`
19
25
  flush!
20
26
  find_by_host(host)
@@ -114,6 +114,23 @@ describe Host do
114
114
 
115
115
  Host.list.should have(1).thing
116
116
  end
117
+
118
+ it "should add a hostname using second hostname's ip" do
119
+ hostname = 'ghost-test-hostname.local'
120
+ alias_hostname = 'ghost-test-alias-hostname.local'
121
+
122
+ Host.empty!
123
+
124
+ Host.add(hostname)
125
+ Host.add(alias_hostname, hostname)
126
+
127
+ Host.list.last.ip.should eql(Host.list.first.ip)
128
+ end
129
+
130
+ it "should raise SocketError if it can't find hostname's ip" do
131
+ Host.empty!
132
+ lambda { Host.add('ghost-test-alias-hostname.google', 'google') }.should raise_error(SocketError)
133
+ end
117
134
  end
118
135
 
119
136
  describe ".empty!" do
data/spec/ghost_spec.rb CHANGED
@@ -105,6 +105,23 @@ describe Host, ".add" do
105
105
 
106
106
  Host.list.should have(1).thing
107
107
  end
108
+
109
+ it "should add a hostname using second hostname's ip" do
110
+ hostname = 'ghost-test-hostname.local'
111
+ alias_hostname = 'ghost-test-alias-hostname.local'
112
+
113
+ Host.empty!
114
+
115
+ Host.add(hostname)
116
+ Host.add(alias_hostname, hostname)
117
+
118
+ Host.list.last.ip.should eql(Host.list.first.ip)
119
+ end
120
+
121
+ it "should raise SocketError if it can't find hostname's ip" do
122
+ Host.empty!
123
+ lambda { Host.add('ghost-test-alias-hostname.google', 'google') }.should raise_error(SocketError)
124
+ end
108
125
  end
109
126
 
110
127
  describe Host, ".empty!" do
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 6
9
+ version: 0.2.6
5
10
  platform: ruby
6
11
  authors:
7
12
  - Bodaniel Jeanes
@@ -9,7 +14,7 @@ autorequire: ghost
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-11 00:00:00 +10:00
17
+ date: 2010-04-04 01:00:00 +11:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -49,18 +54,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
54
  requirements:
50
55
  - - ">="
51
56
  - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
52
59
  version: "0"
53
- version:
54
60
  required_rubygems_version: !ruby/object:Gem::Requirement
55
61
  requirements:
56
62
  - - ">="
57
63
  - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
58
66
  version: "0"
59
- version:
60
67
  requirements: []
61
68
 
62
69
  rubyforge_project: ghost
63
- rubygems_version: 1.3.5
70
+ rubygems_version: 1.3.6
64
71
  signing_key:
65
72
  specification_version: 3
66
73
  summary: Allows you to create, list, and modify local hostnames