Class: Sandbox
- Inherits:
-
Object
- Object
- Sandbox
- Defined in:
- lib/sandbox.rb
Constant Summary collapse
- VERSION =
'0.1.1'
Instance Method Summary collapse
- #add(domain, ip) ⇒ 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.
- #off ⇒ Object
- #on ⇒ Object
- #remove(domain) ⇒ Object
- #require_sudo ⇒ Object
- #status ⇒ Object
- #update_sandbox ⇒ 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 |
# 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].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 send command 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]\n sandbox [destination] newdefault\n sandbox [add|remove] domain destination" else "Invalid command" end end |
Instance Method Details
#add(domain, ip) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/sandbox.rb', line 179 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}" } puts "Added '#{domain} #{ip}'" status end |
#debug(message) ⇒ Object
86 87 88 |
# File 'lib/sandbox.rb', line 86 def debug puts if @debug end |
#destination(ip) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/sandbox.rb', line 172 def destination ip File.open( @destination_storage, 'w' ) do |file| file.puts ip end puts "Sandbox default destination set to #{ip}" end |
#disable ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/sandbox.rb', line 135 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/sandbox.rb', line 108 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
103 104 105 106 |
# File 'lib/sandbox.rb', line 103 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
95 96 97 |
# File 'lib/sandbox.rb', line 95 def '[ERROR] ' + end |
#exit_message(message) ⇒ Object
90 91 92 93 |
# File 'lib/sandbox.rb', line 90 def puts exit end |
#flush_dns ⇒ Object
99 100 101 |
# File 'lib/sandbox.rb', line 99 def flush_dns %x{dscacheutil -flushcache} end |
#get_destination ⇒ Object
202 203 204 205 206 207 208 209 |
# File 'lib/sandbox.rb', line 202 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
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/sandbox.rb', line 211 def get_status # do magic 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
82 83 84 |
# File 'lib/sandbox.rb', line 82 def info puts "Sandbox #{self.class::VERSION}" end |
#off ⇒ Object
153 154 155 156 157 |
# File 'lib/sandbox.rb', line 153 def off disable flush_dns puts "Turning Sandbox off" end |
#on ⇒ Object
129 130 131 132 133 |
# File 'lib/sandbox.rb', line 129 def on enable flush_dns puts "Turning sandbox on" end |
#remove(domain) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/sandbox.rb', line 191 def remove domain entry = '' update_sandbox { |entries| if entries.find { |entry| /^#{domain}/ =~ entry } entries = entries.delete entry end } puts "Removed '#{domain}'" status end |
#require_sudo ⇒ Object
76 77 78 79 80 |
# File 'lib/sandbox.rb', line 76 def require_sudo if ENV["USER"] != "root" exec("sudo #{ENV['_']} #{ARGV.join(' ')}") end end |
#status ⇒ Object
226 227 228 229 |
# File 'lib/sandbox.rb', line 226 def status puts "Sandbox is #{get_status}" puts "Current default sandbox destination is #{get_destination}" end |
#update_sandbox ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/sandbox.rb', line 159 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 |