Skip to content

Instantly share code, notes, and snippets.

@brentkirby
Created July 20, 2011 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentkirby/1094248 to your computer and use it in GitHub Desktop.
Save brentkirby/1094248 to your computer and use it in GitHub Desktop.
Command line vhost switcher for apache and nginx. (vhost folder -rails = symlink nginx root to "folder/public")
#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
require 'date'
class Vhost
VERSION = '0.0.1'
attr_reader :options
def initialize(arguments, stdin)
@arguments = arguments
@stdin = stdin
# Defaults
@options = OpenStruct.new
@options.rails = false
@options.user = `whoami`.chomp
@options.path = "/Users/#{@options.user}/Sites"
@options.apache_root = "/Library/WebServer/Documents"
@options.nginx_root = "/usr/local/etc/nginx/root"
@options.nginx = false
@options.apache = true
end
# Parse options, check arguments, then process the command
def run
if options?
check_valid_path!
puts "Switching vhost to: #{@options.path}/#{@options.folder}"
make_symlink!
puts in_green("Donezo.")
else
puts in_red('fizail')
end
end
private
def check_valid_path!
if @arguments.empty?
puts in_red("You must include a root folder.")
exit 0
end
@options.folder = @arguments.first.strip
unless Dir.exists?(link_path)
puts in_red("Folder does not exist: #{link_path.inspect}")
exit 0
end
end
def make_symlink!
#puts "I'll make a symlink to: #{link_path} within #{root_path}"
File.delete(root_path) if File.exists?(root_path)
File.symlink(link_path, root_path)
end
def link_path
path = [@options.path, @options.folder].collect do |str|
str.strip.gsub(/[\n]+/, "\n")
end.join("/")
( path << "/public" ) if @options.rails
path
end
def root_path
return @options.nginx_root if @options.nginx
@options.apache_root
end
protected
def in_red(str)
"\e[1;31m#{str}\e[0m"
end
def in_green(str)
"\e[2;31m#{str}\e[0m"
end
def options
@options
end
def options?
opts = OptionParser.new do |opts|
opts.banner = "Usage: vhost [options]"
opts.separator ""
opts.separator "Options:"
opts.on("-r", "--rails", "Configures path to root/public folder. Defaults host to nginx") do |rails|
@options.rails = true
@options.nginx = true
end
opts.on("-x", "--nginx", "Configure host for nginx") do |rails|
@options.nginx = true
end
opts.on("-a", "--apache", "Configure host for apache (default)") do |rails|
@options.nginx = true
end
opts.on("-p", "--path", "The base path where document roots exist ( default: #{@options.path} )") do |path|
@options.path = path
end
opts.on("--nginx-root", "The nginx document root folder ( default: #{@options.nginx_root} ) ") do |path|
@options.nginx_root = path
end
opts.on("--apache-root", "The apache document root folder ( default: #{@options.apache_root} ) ") do |path|
@options.apache_root = path
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit 0
end
opts.on_tail("--version", "Show version") do
puts VHost::VERSION
exit 0
end
end
opts.parse!(@arguments) rescue return false
true
end
def show_help
puts "VHost #{VERSION}: Switch apache/nginx/rails virtual host"
end
end
app = Vhost.new(ARGV, STDIN)
app.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment