Skip to content

Instantly share code, notes, and snippets.

@mem
Created July 13, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mem/21b0db6e6f672ba7c46cab457c34ae2b to your computer and use it in GitHub Desktop.
Save mem/21b0db6e6f672ba7c46cab457c34ae2b to your computer and use it in GitHub Desktop.
go wrapper to automatically set GOPATH
#!/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 "$@"
@broady
Copy link

broady commented Jul 18, 2016

stat -c doesn't work on Mac OS. I think it needs to be stat -f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment