automatic GOPATH

This commit is contained in:
Loic Nageleisen 2013-12-14 22:29:41 +01:00
parent 04a1b20792
commit 85d27c709e
2 changed files with 36 additions and 0 deletions

35
zsh/go Normal file
View file

@ -0,0 +1,35 @@
go_commands=(go)
_go-installed() {
which go > /dev/null 2>&1
}
_within-go-project() {
local check_dir=$PWD
while [ "$(dirname $check_dir)" != "/" ]; do
if [ -f "$check_dir/.gopath" ]; then
echo "$check_dir"
return
fi
check_dir="$(dirname $check_dir)"
done
false
}
_run-with-gopath() {
local go_path
if _go-installed && go_path=$(_within-go-project); then
GOPATH="$go_path" $@
else
$@
fi
}
for cmd in $go_commands; do
eval "function gopath_$cmd () { _run-with-gopath $cmd \$@}"
alias $cmd=gopath_$cmd
if which _$cmd > /dev/null 2>&1; then
compdef _$cmd gopath_$cmd=$cmd
fi
done