Skip to content

Instantly share code, notes, and snippets.

@nilium
Last active August 29, 2015 13:56
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 nilium/9240806 to your computer and use it in GitHub Desktop.
Save nilium/9240806 to your computer and use it in GitHub Desktop.
Utility to scan ascendant directories for .go-root files, add those directories to the GOPATH, then forward arguments to the next go command in the PATH.
#!/bin/sh
# Puts any directory with a .go-root file into the GOPATH then launches go.
__gopath_for_dir__() {
test_goroot="$1/.go-root"
if [[ -f "$test_goroot" ]] ; then
echo "${2:+$2:}$1"
else
echo "$2"
fi
}
GOPATH_ASCENDANT=""
test_dir="$(pwd)"
until [[ "$test_dir" == "/" ]] ; do
GOPATH_ASCENDANT="$(__gopath_for_dir__ "$test_dir" "$GOPATH_ASCENDANT")"
test_dir="$(dirname "$test_dir")"
done
GOPATH_ASCENDANT="$(__gopath_for_dir__ "$test_dir" "$GOPATH_ASCENDANT")"
export GOPATH="$GOPATH_ASCENDANT${GOPATH_ASCENDANT:+${GOPATH:+:}}$GOPATH"
GOBIN="$(which -a go | sed -e '2q;d')"
# check for gopath args
while [[ 1 ]] ; do
if [[ "$1" == "--go-bin-path" ]] ; then
GOBIN="$2"
shift; shift
continue
elif [[ "$1" == "--echo-gopath" ]] ; then
echo "GOPATH='$GOPATH'" 1>&2
shift
continue
elif [[ "$1" == "--echo-gobin" ]] ; then
echo "GOBIN='$GOBIN'" 1>&2
shift
continue
fi
break
done
if [[ -x "$GOBIN" && "$GOBIN" != "$0" ]] ; then
"$GOBIN" $*
else
echo "No valid 'go' executable in PATH -- tried '$GOBIN'" 1>&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment