Skip to content

Instantly share code, notes, and snippets.

@brayhoward
Forked from rafmagana/README.md
Created September 18, 2017 15:58
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 brayhoward/3c1253e80042429adb07529d96323cb6 to your computer and use it in GitHub Desktop.
Save brayhoward/3c1253e80042429adb07529d96323cb6 to your computer and use it in GitHub Desktop.
This is a way to call the Heroku command-line utility adding the name of the Heroku App automatically.

Heroku command-line wrapper with app name

This is a kind of second version of https://gist.github.com/4208338, but in this version you don't need to call projectName_production or projecName_staging only production or staging.

How to make it work

If you have git remotes called production, staging and development already, then then the only thing you have to do is to source heroku_command_line_wrapper_with_app_name.sh in your .bash_profile or .bashrc:

source /path/to/heroku_command_line_wrapper_with_app_name.sh

or

. heroku_command_line_wrapper_with_app_name.sh

If you want to use your existing git remote names, then create a .herokurc file and put the following variables on it:

heroku_production_git_remote=name
heroku_staging_git_remote=name
heroku_development_git_remote=name

Usage

$ cd /dir/with/a/heroku/app

$ staging logs 
$ staging logs -t
$ staging config:get MONGOHQ_URL
$ staging run console

$ production ps
$ production logs -n 3 -p worker.4 -s app -t

You should know the production, staging and development bash functions will be available all over the place, but if you call one of them in directory not containing a heroku application you'll get an error like ! Resource not found or something like that.

#!/usr/bin/env bash
## Heroku Command-line Wrapper with Application Name
function hclw_execute_heroku_command_with_app
{
declare herokurc_file="./.herokurc"
declare app=''
if [ -f "$herokurc_file" ];
then
source $herokurc_file
declare git_remote=heroku_$1_git_remote
app="${!git_remote}"
else
app=`hclw_current_heroku_app_name $1`
fi
heroku ${@:2} --app=$app
}
function hclw_current_heroku_app_name
{
declare regex_pattern="git@heroku\.com:(.+)\.git"
declare git_remote=`git config --get remote.$1.url`
declare heroku_app_name=''
if [[ $git_remote =~ $regex_pattern ]]
then
heroku_app_name="${BASH_REMATCH[1]}"
fi
echo $heroku_app_name
}
function production
{
hclw_execute_heroku_command_with_app production $@
}
function staging
{
hclw_execute_heroku_command_with_app staging $@
}
function development
{
hclw_execute_heroku_command_with_app development $@
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment