Skip to content

Commit 57f15a9

Browse files
Prevented unnecessary changes to the hosts file
1 parent 15b20ca commit 57f15a9

File tree

9 files changed

+46
-20
lines changed

9 files changed

+46
-20
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ruby-nginx (1.0.0)
4+
ruby-nginx (1.0.1)
55
erb
66
thor
77
tty-command (~> 0.10, >= 0.10.1)

lib/ruby/nginx/commands/add_host_mapping.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,35 @@ def initialize(host, ip, sudo: false)
1111
@ip = ip
1212
@sudo = sudo
1313
sudo_reason = "Allow sudo elevation to add \"#{host}\" to /etc/hosts?"
14-
cmd = "echo \"#{ip} #{host}\" | #{sudoify("tee -a /etc/hosts", sudo, sudo_reason)}"
1514

16-
super(cmd:, raise: Ruby::Nginx::ConfigError)
15+
super(
16+
cmd: "echo \"#{host_config}\" | #{sudoify("tee -a /etc/hosts", sudo, sudo_reason)}",
17+
raise: Ruby::Nginx::ConfigError
18+
)
1719
end
1820

1921
def run
20-
super
22+
super unless added?
2123
rescue Ruby::Nginx::ConfigError
2224
raise if @sudo
2325

2426
# Elevate to sudo and try again.
2527
self.class.new(@host, @ip, sudo: true).run
2628
end
29+
30+
private
31+
32+
def added?
33+
hosts.include?(host_config)
34+
end
35+
36+
def hosts
37+
@hosts ||= Ruby::Nginx::System::SafeFile.read("/etc/hosts")
38+
end
39+
40+
def host_config
41+
"#{@ip} #{@host}"
42+
end
2743
end
2844
end
2945
end

lib/ruby/nginx/commands/remove_host_mapping.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,36 @@ module Ruby
77
module Nginx
88
module Commands
99
class RemoveHostMapping < TerminalCommand
10-
def initialize(host, sudo: false)
10+
def initialize(host, ignore_ip: nil, sudo: false)
1111
@host = host
12+
@ignore_ip = ignore_ip
1213
@sudo = sudo
1314
@sudo_reason = "Allow sudo elevation to remove \"#{host}\" from /etc/hosts?"
1415

1516
super(cmd: resolve_command, raise: Ruby::Nginx::ConfigError)
1617
end
1718

1819
def run
19-
super
20+
super unless removed?
2021
rescue Ruby::Nginx::ConfigError
2122
raise if @sudo
2223

2324
# Elevate to sudo and try again.
24-
self.class.new(@host, sudo: true).run
25+
self.class.new(@host, ignore_ip: @ignore_ip, sudo: true).run
2526
end
2627

2728
private
2829

30+
def removed?
31+
hosts.each_line.none? do |host_config|
32+
host_config.include?(@host) && (@ignore_ip.nil? || !host_config.include?(@ignore_ip))
33+
end
34+
end
35+
36+
def hosts
37+
@hosts ||= Ruby::Nginx::System::SafeFile.read("/etc/hosts")
38+
end
39+
2940
def darwin_command
3041
sudoify("sed -i '' '/#{@host}/d' /etc/hosts", @sudo, @sudo_reason)
3142
end

lib/ruby/nginx/commands/setup_nginx.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ class SetupNginx < TerminalCommand
2020
def initialize(sudo: false)
2121
@sudo = sudo
2222
sudo_reason = "Allow sudo elevation to add \"#{INCLUDE_STATEMENT}\" to NGINX configuration file?"
23-
cmd = "echo \"#{new_config}\" | #{sudoify("tee #{config_file_path}", sudo, sudo_reason)}"
2423

25-
super(cmd:, raise: Ruby::Nginx::SetupError)
24+
super(
25+
cmd: "echo \"#{new_config}\" | #{sudoify("tee #{config_file_path}", sudo, sudo_reason)}",
26+
raise: Ruby::Nginx::SetupError
27+
)
2628
end
2729

2830
def run

lib/ruby/nginx/commands/start_nginx.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ class StartNginx < TerminalCommand
99
def initialize(sudo: false)
1010
@sudo = sudo
1111
sudo_reason = "Allow sudo elevation to start NGINX?"
12-
cmd = sudoify("nginx", sudo, sudo_reason)
1312

14-
super(cmd:, raise: Ruby::Nginx::StartError)
13+
super(cmd: sudoify("nginx", sudo, sudo_reason), raise: Ruby::Nginx::StartError)
1514
end
1615

1716
def run

lib/ruby/nginx/commands/stop_nginx.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ class StopNginx < TerminalCommand
99
def initialize(sudo: false)
1010
@sudo = sudo
1111
sudo_reason = "Allow sudo elevation to stop NGINX?"
12-
cmd = sudoify("nginx -s stop", sudo, sudo_reason)
1312

14-
super(cmd:, raise: Ruby::Nginx::StopError)
13+
super(cmd: sudoify("nginx -s stop", sudo, sudo_reason), raise: Ruby::Nginx::StopError)
1514
end
1615

1716
def run

lib/ruby/nginx/commands/validate_nginx_config.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ class ValidateNginxConfig < TerminalCommand
99
def initialize(sudo: false)
1010
@sudo = sudo
1111
sudo_reason = "Allow sudo elevation to validate NGINX config?"
12-
cmd = sudoify("nginx -t", sudo, sudo_reason)
1312

14-
super(cmd:, raise: Ruby::Nginx::ConfigError)
13+
super(cmd: sudoify("nginx -t", sudo, sudo_reason), raise: Ruby::Nginx::ConfigError)
1514
end
1615

1716
def run

lib/ruby/nginx/system/hosts.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ module System
99
class Hosts
1010
class << self
1111
def add(host, ip)
12-
remove(host)
13-
Commands::AddHostMapping.new(host, ip).run.success?
12+
remove(host, ignore_ip: ip)
13+
Commands::AddHostMapping.new(host, ip).run&.success?
1414
end
1515

16-
def remove(host)
17-
Commands::RemoveHostMapping.new(host).run.success?
16+
def remove(host, ignore_ip: nil)
17+
Commands::RemoveHostMapping.new(host, ignore_ip:).run&.success?
1818
end
1919
end
2020
end

lib/ruby/nginx/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Ruby
44
module Nginx
5-
VERSION = "1.0.0"
5+
VERSION = "1.0.1"
66

77
Version = Gem::Version
88
end

0 commit comments

Comments
 (0)