#!/usr/bin/env ruby begin require 'depl' rescue LoadError require 'rubygems' require 'depl' end require 'optparse' require 'highline/import' require 'colorize' options = {} options[:environment] = ARGV.reject{|a| a =~ /^-/}.first options[:rev] = ENV['REV'] optparse = OptionParser.new do |opts| opts.banner = "Usage: depl [environment] [options]" opts.on("-c", "--config=CONFIG", String, "Location of .deploy configuration file") do |c| options[:config_file] = c end opts.on("-r", "--revision=REVISION", String, "Revision to deploy (can be sha, branch, etc)") do |r| options[:rev] = r end opts.on("-f", "--force", "Do not ask for confirmation") do |f| options[:force] = f end opts.on("-q", "--quiet", "No stdout output (only use with --force)") do |q| options[:quiet] = q end opts.on("-h", "--help", "Print this help information") do |h| if h puts optparse exit 1 end end end begin optparse.parse! rescue OptionParser::InvalidOption => e puts e.message puts optparse exit 1 end unless options[:environment] puts "Error: environment required" puts optparse exit 1 end def output(*msg) puts *msg end if options[:force] and options[:quiet] def output(*msg) # no-op end end deploy = Depl::Main.new(options) if deploy.up_to_date output "Everything up-to-date (#{deploy.environment.green}: #{deploy.remote_sha.green})" exit 0 end if deploy.remote_sha.nil? output "New deployment: #{deploy.local_sha}" else output "Attempting to deploy #{deploy.local_sha.bold}", "" commits = "#{deploy.commit_count} new commit(s)".green output "Difference of #{commits} between #{deploy.remote_sha} and #{deploy.local_sha}:", "" if deploy.commit_count > 0 output deploy.diff.yellow, "" else output " There are no new commits.", "" end end if deploy.older_local_sha output "WARNING: The commit you are deploying is older than what is currently on #{deploy.environment}. Missing commits: ".bold, "" output deploy.reverse_diff.red output "" end if options[:force] confirm = 'yes' else confirm = ask("Deploy #{deploy.environment}? ([y]es / [n]o / [g]ithub) : ") { |yn| yn.limit = 1, yn.validate = /([yng]|yes|no|github)/i } end case confirm.downcase when 'y', 'yes' deploy.run! output "Deployed #{deploy.remote_sha}" when 'g', 'github' url = Depl::Remote.comparison(deploy.remote_sha, deploy.local_sha) if url if `which open`.size > 0 `open #{url}` else output " Github: #{url}" end else output "Unrecognized repository; can't open github" output "(try `git config --get remote.origin.url`)" exit 1 end when 'n', 'no' output "Bye." end