sant0sk1-hoster 0.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/hoster/hosts.rb +24 -19
  2. 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
- File.open(@hosts_path,"r+") do |file|
76
- lines = file.readlines
77
- lines.each_with_index do |line,index|
78
- if line =~ /#{entry.first}/
79
- if entry_exists || entry.last.empty?
80
- lines.delete_at(index)
81
- else
82
- lines[index] = "#{entry.first} #{entry.last.join(' ')}\n"
83
- # update the state of this entry
84
- append_this = false
85
- entry_exists = true
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
- file.pos = 0
90
- file.print lines
91
- file.truncate(file.pos)
92
- end
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
- end
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sant0sk1-hoster
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jerod Santo