Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 143 lines (117 sloc) 3.13 KB
#!/usr/bin/env ruby
# Author: Joseph Pecoraro
# Date: Saturday December 13, 2008
# Updated: Friday June 26, 2009
# Description: When I'm using ExpanDrive and I'm
# remotely logged into a server, I can use this
# script to "open filename" and it will open using
# the server's associated URL.
# For URL Escaping and Stored Mappings
require 'uri'
require 'yaml'
require 'optparse'
# Parse potential command line arguments
$options = { :url => false }
OptionParser.new do |opts|
opts.banner = 'usage: expanurl [option] [files]'
opts.on("-u", "--url", "Print the URL that would have been used.") { |u| $options[:url] = u }
opts.on_tail("-h", "--help", "Show this message") { puts opts }
end.parse!
# Properly open/print a url
def handle_url(url)
if $options[:url]
puts url
else
`open #{url}`
end
end
# Simple Class to handle the mappings
class UrlMap
MAP_PATH = File.expand_path('~/.expanurl')
def initialize
@hash = load
end
def load
if File.exists?(MAP_PATH)
YAML::load_file( File.expand_path(MAP_PATH) )
else
Hash.new
end
end
def add_mapping(server, mapto)
@hash[server] = mapto
File.open(MAP_PATH, 'w') do |file|
file.write(@hash.to_yaml)
end
end
def is_mapping?(server)
@hash.has_key?(server)
end
def get_mapping(server)
@hash[server]
end
def path
MAP_PATH
end
end
# Local Variables
PWD = Dir.pwd
mapping = UrlMap.new
url_prefix = nil
server = nil
volume = nil
# Process Each File, Assume pwd if No File Given
files = ARGV
files << PWD if ARGV.empty?
files.each do |filename|
# Expand the filename
filename = File.expand_path(filename)
# Check the if the current directory is an
# ExpanDrive Volume and a public_html folder
match = filename.match(/^\/Volumes\/([^\/]+)/)
if match.nil?
puts "'#{filename}' is not inside an ExpanDrive Volume"
next
elsif !filename.match(/\/public_html\/?/)
puts "'#{filename}' is not inside a public_html directory."
next
else
volume = match[1]
defaults = `defaults read com.magnetk.ExpanDrive Drives`
defaults.gsub!(/\n/, '')
props = defaults.match(/\{[^\}]+driveName\s+=\s+#{volume}[^\}]+server\s+=\s+"([^"]+)"[^\}]+\}/)
if props
server = props[1]
else
puts "This Volume (#{volume}) is not an ExpanDrive Volume"
next
end
end
# Check if a mapping exists
# Otherwise create and store one
if mapping.is_mapping?(server)
url_prefix = mapping.get_mapping(server)
else
# Prompt
puts
puts "This is the first time you've used expanurl for #{volume}"
puts "Please Provide us with a mapping for #{server}"
puts "Mappings are stored in #{mapping.path}"
puts "Example: http://bogojoker.com/"
print ">> "
# Store user input and proceed
url_prefix = gets.chomp
url_prefix += '/' unless url_prefix.match(/\/$/)
mapping.add_mapping(server, url_prefix)
# Display Result
puts
puts "Server: #{server}"
puts "Maps to: #{url_prefix}"
puts
end
# Build the URL subpath
subpath = filename.match(/public_html\/?(.*)/)[1]
subpath += '/' if !subpath.empty? && File.directory?(filename) && subpath !~ /^\/$/
# Open the file/directory
handle_url( url_prefix + subpath )
end