Skip to content

Instantly share code, notes, and snippets.

@csexton
Created April 27, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save csexton/a59e3c6b45ecd181c4ad3e7d21463258 to your computer and use it in GitHub Desktop.
Save csexton/a59e3c6b45ecd181c4ad3e7d21463258 to your computer and use it in GitHub Desktop.
Heroku Environment CLI Wrapper
#!/usr/bin/env ruby
# Heroku enviroment helper script
#
# This is to streamline using differnt heroku enviroments from one repo.
# Symlink this file to an envroment name (as set by the git-remote name), then
# when running that symlink the script will look up the name in the git config
# and use that remote as the heroku name.
#
# Setup:
#
# ln -s ~/bin/heroku-enviroments production
#
# And assuming git is configured:
#
# [remote "production"]
# url = git@heroku.com:my-app-production.git
# fetch = +refs/heads/*:refs/remotes/production/*
#
# Then running:
#
# production run rails console
#
# Actually runs:
#
# heroku run rails console --app my-app-production
trap "SIGINT" do
puts "Exiting"
exit 130
end
class String
def red; "\e[31m#{self}\e[0m" end
def green; "\e[32m#{self}\e[0m" end
def blue; "\e[34m#{self}\e[0m" end
end
def error(message)
STDERR.puts "Error: #{message}".red
exit 1
end
env_name = File.basename($0, File.extname($0))
url = `git config --get remote.#{env_name}.url`.chomp
if ARGV.empty?
puts "Heroku CLI Wrapper for #{env_name}. Try `#{env_name} --help` for more information"
exit 0
end
error "Unable to find git config file. Are you in a git repo?" if url.empty?
app_name = /([^\/:]+)\.git$/.match(url)[1]
error "Unable to find #{env_name} remote. Make sure you have `remote.production` set in your git config" if app_name.empty?
cmd = "heroku #{ARGV.join " "} --app #{app_name}"
puts cmd.blue
puts
system(cmd)
exit $?.exitstatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment