-
Notifications
You must be signed in to change notification settings - Fork 3
/
expanurl
executable file
·142 lines (117 loc) · 3.13 KB
/
expanurl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/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