-
-
Save mem/21b0db6e6f672ba7c46cab457c34ae2b to your computer and use it in GitHub Desktop.
go wrapper to automatically set GOPATH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
get_inode() { | |
stat -c '%i' "$1" | |
} | |
find_go() { | |
self=`get_inode $(readlink -m "$1")` | |
local IFS=: | |
for d in $PATH ; do | |
fn="$d/go" | |
if test -x $d/go ; then | |
inode=`get_inode "$fn"` | |
if test "$inode" != "$self" ; then | |
echo "$fn" | |
return | |
fi | |
fi | |
done | |
} | |
find_gopath() { | |
local OLDPWD=$PWD | |
local gopath="" | |
while true ; do | |
if test -d src ; then | |
gopath=$PWD | |
break | |
fi | |
if test "$PWD" = / ; then | |
break | |
fi | |
cd .. | |
done | |
cd "$OLDPWD" | |
echo "$gopath" | |
} | |
go=`find_go "$0"` | |
if test -z "$go" ; then | |
echo "E: go program not found. Stop." | |
exit 1 | |
fi | |
if test -z "$GOPATH" ; then | |
GOPATH=`find_gopath` | |
if test -z "$GOPATH" ; then | |
echo "W: GOPATH not found." | |
else | |
export GOPATH | |
fi | |
fi | |
exec $go "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
stat -c
doesn't work on Mac OS. I think it needs to bestat -f