sant0sk1-hoster 0.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hoster/hosts.rb +24 -19
- metadata +1 -1
data/lib/hoster/hosts.rb
CHANGED
@@ -72,29 +72,34 @@ class Hosts
|
|
72
72
|
append_this = true
|
73
73
|
entry_exists = false
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
if
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
75
|
+
begin
|
76
|
+
File.open(@hosts_path,"r+") do |file|
|
77
|
+
lines = file.readlines
|
78
|
+
lines.each_with_index do |line,index|
|
79
|
+
if line =~ /#{entry.first}/
|
80
|
+
if entry_exists || entry.last.empty?
|
81
|
+
lines.delete_at(index)
|
82
|
+
else
|
83
|
+
lines[index] = "#{entry.first} #{entry.last.join(' ')}\n"
|
84
|
+
# update the state of this entry
|
85
|
+
append_this = false
|
86
|
+
entry_exists = true
|
87
|
+
end
|
86
88
|
end
|
87
89
|
end
|
90
|
+
file.pos = 0
|
91
|
+
file.print lines
|
92
|
+
file.truncate(file.pos)
|
88
93
|
end
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
if append_this && !entry.last.empty?
|
94
|
-
File.open(@hosts_path,"a") do |file|
|
95
|
-
file.print "#{entry.first} #{entry.last.join(' ')}\n"
|
94
|
+
if append_this && !entry.last.empty?
|
95
|
+
File.open(@hosts_path,"a") do |file|
|
96
|
+
file.print "#{entry.first} #{entry.last.join(' ')}\n"
|
97
|
+
end
|
96
98
|
end
|
97
|
-
|
99
|
+
rescue Errno::EACCES
|
100
|
+
puts "You do not have permission to edit #{@hosts_path}. Run as root or use 'sudo'"
|
101
|
+
exit 1
|
102
|
+
end
|
98
103
|
end
|
99
104
|
end
|
100
105
|
|