Skip to content

Instantly share code, notes, and snippets.

@aneilbaboo
Last active September 26, 2016 18:35
Show Gist options
  • Save aneilbaboo/32feaea715f1cf4964cc to your computer and use it in GitHub Desktop.
Save aneilbaboo/32feaea715f1cf4964cc to your computer and use it in GitHub Desktop.
Heroku shortcut - looks for git remotes, infers heroku app name and issues a heroku command
#!/bin/bash
remote="$1"
app=`git remote -v | awk -v pattern="^$remote" '{ if (match($1, pattern) && match($2, "git@.*") && match($2, "\:(.*)\.git")) { print substr($2, RSTART+1, RLENGTH-5); exit 1;}}'`
if [ "${app}" == "" ]; then
# regex is written in this hack way ("\.com/.*\.git" instead of "[^/]*.git") because Apple awk doesn't support character sets
app=`git remote -v | awk -v pattern="^$remote" '{ if (match($1, pattern) && match($2, "https") && match($2, "\.com/.*\.git")) { print substr($2, RSTART+5, RLENGTH-9); exit 1;}}'`
fi
if [ "$2" ]; then
if [ "$app" ]; then
heroku "${@:2}" --app "$app"
else
echo "Unable to find Heroku remote named '$remote' in your current git repo."
fi
else
echo "usage: h {remote_name} {heroku_command} [options]"
echo "Runs heroku command on heroku instance referenced by the git remote_name"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment