dotfiles/shell/go

41 lines
814 B
Bash

go_commands=(go vim)
_go-installed() {
which go > /dev/null 2>&1
}
_within-go-project() {
local check_dir=$PWD
local next_check_dir=${check_dir%/*}
while [ "$next_check_dir" != "" ]; do
if [ -f "$check_dir/.gopath" ]; then
echo "$check_dir"
return
fi
check_dir="$next_check_dir"
next_check_dir=${check_dir%/*}
done
false
}
_run-with-gopath() {
local go_path
if _go-installed && go_path=$(_within-go-project); then
env PATH="$PATH:$go_path/bin" GOPATH="$go_path" "$@"
else
"$@"
fi
}
for cmd in "${go_commands[@]}"; do
eval "function gopath_$cmd () { _run-with-gopath $cmd \$@; }"
alias $cmd=gopath_$cmd
if [[ -n $ZSH_VERSION ]]; then
if which _$cmd > /dev/null 2>&1; then
compdef _$cmd gopath_$cmd=$cmd
fi
fi
done
# vim: ft=sh