Class: Sandbox
- Inherits:
-
Object
- Object
- Sandbox
- Defined in:
- lib/sandbox.rb
Constant Summary collapse
- VERSION =
'0.1.4'
Instance Method Summary collapse
- #add(domain, ip) ⇒ Object
- #clear ⇒ Object
- #debug(message) ⇒ Object
- #destination(ip) ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
- #ensure_sandbox_exists ⇒ Object
- #exit_error_message(message) ⇒ Object
- #exit_message(message) ⇒ Object
- #flush_dns ⇒ Object
- #get_destination ⇒ Object
- #get_status ⇒ Object
- #info ⇒ Object
-
#initialize ⇒ Sandbox
constructor
A new instance of Sandbox.
- #list ⇒ Object
- #off ⇒ Object
- #on ⇒ Object
- #remove(domain) ⇒ Object
- #require_sudo ⇒ Object
- #status ⇒ Object
- #update_sandbox ⇒ Object
- #view(domain) ⇒ Object
Constructor Details
#initialize ⇒ Sandbox
Returns a new instance of Sandbox.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sandbox.rb', line 30 def initialize @debug = false @sandbox = '/etc/hosts-sandbox' @destination_storage = '/etc/hosts-sandbox-destination' @hosts = '/etc/hosts' @default_destination = '127.0.0.1' @start = '#==SANDBOX==#' @end = '#/==SANDBOX==#' if !ARGV.first.nil? && [:on, :off, :add, :remove, :destination, :clear, :list, :view].include?( ARGV.first.to_sym ) require_sudo ensure_sandbox_exists end command = ARGV.shift command = command.to_sym unless command.nil? object = ARGV.shift ip = ARGV.shift case command when :"--v", :"--version" info when :on, :off, :status, :clear, :list send command when :view send command, object when :add require_sudo object.nil? && ("'sandbox add' requires you to provide a domain. optionally you can provide an IP/destination.") ensure_sandbox_exists if ip.nil? ip = get_destination end send command, object, ip when :remove require_sudo object.nil? && ("'sandbox remove' requires you to provide a domain.") ensure_sandbox_exists send command, object when :destination require_sudo object.nil? && ("'sandbox setdefault' requires you to provide an IP/destination.") ensure_sandbox_exists send command, object when nil, '--help', '-h' "Usage: sandbox [on|off|status|clear]\n sandbox [destination] newdefault\n sandbox [add|remove] domain destination" else "Invalid command" end end |
Instance Method Details
#add(domain, ip) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/sandbox.rb', line 181 def add (domain, ip) entry = '' update_sandbox { |entries| if entries.find { |entry| /^#{domain}/ =~ entry } entries = entries.delete entry end } update_sandbox { | entries | entries << "#{domain} #{ip}" } enable if :on == get_status puts "Added '#{domain} #{ip}'" end |
#clear ⇒ Object
244 245 246 247 248 249 250 |
# File 'lib/sandbox.rb', line 244 def clear File.open( @sandbox, 'w' ) do |file| file.truncate(0) end enable puts "All sandbox entries have been removed" end |
#debug(message) ⇒ Object
88 89 90 |
# File 'lib/sandbox.rb', line 88 def debug puts if @debug end |
#destination(ip) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/sandbox.rb', line 174 def destination ip File.open( @destination_storage, 'w' ) do |file| file.puts ip end puts "Sandbox default destination set to #{ip}" end |
#disable ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/sandbox.rb', line 137 def disable hosts_content = [] File.open( @hosts, 'r' ) do |file| started = false while line = file.gets started = true if line.include? @start hosts_content << line unless started started = false if line.include? @end end end while "\n" == hosts_content.last hosts_content.pop end File.open( @hosts, 'w' ) do |file| file.puts hosts_content end end |
#enable ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sandbox.rb', line 110 def enable disable entries = [] File.open( @sandbox, 'r' ) do |file| entries = file.read.split("\n").uniq end File.open( @hosts, 'a' ) do |file| file.puts "\n" file.puts @start file.puts "# The md5 dummy entries are here so that things like MAMP Pro don't" file.puts "# discourtiously remove our entries" entries.each do |entry| pieces = entry.split( ' ' ) domain = pieces[0] ip = pieces[1] file.puts "#{ip} #{Digest::MD5.hexdigest(domain)}.#{domain} #{domain}" end file.puts @end end end |
#ensure_sandbox_exists ⇒ Object
105 106 107 108 |
# File 'lib/sandbox.rb', line 105 def ensure_sandbox_exists File.open( @sandbox, 'w' ) {|file| file.write('') } unless File.exists?( @sandbox ) File.open( @destination_storage, 'w' ) {|file| file.write('') } unless File.exists?( @destination_storage ) end |
#exit_error_message(message) ⇒ Object
97 98 99 |
# File 'lib/sandbox.rb', line 97 def '[ERROR] ' + end |
#exit_message(message) ⇒ Object
92 93 94 95 |
# File 'lib/sandbox.rb', line 92 def puts exit end |
#flush_dns ⇒ Object
101 102 103 |
# File 'lib/sandbox.rb', line 101 def flush_dns %x{dscacheutil -flushcache} end |
#get_destination ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/sandbox.rb', line 204 def get_destination current_destination = @default_destination File.open( @destination_storage, 'r' ) do |file| dest = file.read.strip current_destination = dest unless dest.empty? or dest.nil? end return current_destination end |
#get_status ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/sandbox.rb', line 213 def get_status status = :off return status unless File.readable? @hosts File.open( @hosts, 'r' ) do |file| while line = file.gets if line.include? @start status = :on break end end end return status end |
#info ⇒ Object
84 85 86 |
# File 'lib/sandbox.rb', line 84 def info puts "Sandbox #{self.class::VERSION}" end |
#list ⇒ Object
227 228 229 230 231 |
# File 'lib/sandbox.rb', line 227 def list File.open( @sandbox, 'r' ) do |file| puts file.read end end |
#off ⇒ Object
155 156 157 158 159 |
# File 'lib/sandbox.rb', line 155 def off disable flush_dns puts "Turning Sandbox off" end |
#on ⇒ Object
131 132 133 134 135 |
# File 'lib/sandbox.rb', line 131 def on enable flush_dns puts "Turning sandbox on" end |
#remove(domain) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/sandbox.rb', line 193 def remove domain entry = '' update_sandbox { |entries| if entries.find { |entry| /^#{domain}/ =~ entry } entries = entries.delete entry end } enable if :on == get_status puts "Removed '#{domain}'" end |
#require_sudo ⇒ Object
78 79 80 81 82 |
# File 'lib/sandbox.rb', line 78 def require_sudo if ENV["USER"] != "root" exec("sudo #{ENV['_']} #{ARGV.join(' ')}") end end |
#status ⇒ Object
252 253 254 255 |
# File 'lib/sandbox.rb', line 252 def status puts "Sandbox is #{get_status}" puts "Current default sandbox destination is #{get_destination}" end |
#update_sandbox ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/sandbox.rb', line 161 def update_sandbox entries = [] File.open( @sandbox, 'r' ) do |file| entries = file.read.split( "\n" ) debug entries.inspect yield entries debug entries.inspect end File.open( @sandbox, 'w' ) do |file| file.puts entries end end |
#view(domain) ⇒ Object
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/sandbox.rb', line 233 def view domain entry = nil File.open( @sandbox, 'r' ) do |entries| if entries.read.find { |entry| /^#{domain}/ =~ entry } puts "#{entry}" else puts "There is no entry matching '#{domain}'" end end end |