Skip to content

Instantly share code, notes, and snippets.

@stormbrew
Created May 7, 2014 10:03
Show Gist options
  • Save stormbrew/e12003f8bb50bb774a75 to your computer and use it in GitHub Desktop.
Save stormbrew/e12003f8bb50bb774a75 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -o pipefail
# Find the repo, fail if we're not in a git repo or the origin has a weird name.
# TODO: Add other kinds of version control
# TODO: Make the remote name configurable with git config
repo_dir=$(git rev-parse --show-toplevel)
[[ $? != 0 ]] && { echo "Not in a git directory!"; exit $?; }
origin=$(git remote -v | egrep '^origin.+\(fetch\)' | awk '{print $2}')
[[ $? != 0 ]] && { echo "Couldn't find an origin."; exit $?; }
# Convert the remote to a format go likes.
# TODO: Deal with other kinds of remotes (ie. bitbucket)
case ${origin} in
[email protected]:*)
project_path=github.com/${origin#[email protected]:}
;;
https://github.com/*)
project_path=${origin#https://}
;;
*)
echo "Unknown origin type: ${origin}"
exit 1
;;
esac
# No matter what, remove the trailing .git
project_path=${project_path%.git}
# TODO: Configurable gopath directory name, for if you want to vendor it or something like that?
gopath=${repo_dir}/.gopath
[[ -d ${gopath}/src/$(dirname ${project_path}) ]] || mkdir -p ${gopath}/src/$(dirname ${project_path})
[[ -L ${gopath}/src/${project_path} ]] || ln -s ${repo_dir} ${gopath}/src/${project_path}
# Double check the symlink goes to the right place.
[[ $(readlink ${gopath}/src/${project_path}) == ${repo_dir} ]] || { echo "Incorrect symlink in ${gopath}/src/${project_path}"; exit $?; }
# make sure go isn't aliased in this context
unalias go >/dev/null 2>&1
export GOPATH=${gopath}:${GOPATH}
# TODO: This should probably be configurable, it might not make sense for all projects.
export GOBIN=${repo_dir}/bin
go "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment