Skip to content

Instantly share code, notes, and snippets.

@nilium
Created December 1, 2014 15:02
Show Gist options
  • Save nilium/6be17fa5e9bcf29c4b10 to your computer and use it in GitHub Desktop.
Save nilium/6be17fa5e9bcf29c4b10 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Forwarding utility for go.
# 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
case "$1" in
"--go-bin-path")
GOBIN="$2"
shift; shift
continue
;;
"--echo-gopath")
echo "GOPATH='$GOPATH'" 1>&2
shift
continue
;;
"--echo-gobin")
echo "GOBIN='$GOBIN'" 1>&2
shift
continue
;;
esac
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