proper automatic go workspace path

This commit is contained in:
Loic Nageleisen 2014-11-17 16:09:12 +01:00
parent f5d7ca34b8
commit b34f8bcd04
2 changed files with 34 additions and 17 deletions

View file

@ -1,5 +1,3 @@
go_commands=(go vim)
_go-installed() { _go-installed() {
which go > /dev/null 2>&1 which go > /dev/null 2>&1
} }
@ -7,35 +5,53 @@ _go-installed() {
_within-go-project() { _within-go-project() {
local check_dir=$PWD local check_dir=$PWD
local next_check_dir=${check_dir%/*} local next_check_dir=${check_dir%/*}
while [ "$next_check_dir" != "" ]; do while [ "$next_check_dir" != "" ]; do
if [ -f "$check_dir/.gopath" ]; then if [ -f "$check_dir/.gopath" ]; then
echo "$check_dir" echo "$check_dir"
return return
fi fi
check_dir="$next_check_dir" check_dir="$next_check_dir"
next_check_dir=${check_dir%/*} next_check_dir=${check_dir%/*}
done done
false false
} }
_run-with-gopath() { _append-go-path() {
local bin_path="$*/bin"
[[ $PATH == *:"$*"* ]] || export PATH="$PATH:$bin_path"
}
_strip-go-path() {
local bin_path="$*/bin"
[[ $PATH == *":$bin_path"* ]] && export PATH=$(
echo -n "$PATH" |
awk -v RS=: -v ORS=: '$0=="'"$bin_path"'" {next} {print}' |
sed 's/:$//'
)
}
_set-gopath() {
export GOPATH="$*"
}
_unset-gopath() {
unset GOPATH
}
_gopath() {
local go_path local go_path
if _go-installed && go_path=$(_within-go-project); then if _go-installed && go_path=$(_within-go-project); then
env PATH="$PATH:$go_path/bin" GOPATH="$go_path" "$@" _set-gopath "$go_path"
else _append-go-path "$GOPATH"
"$@" elif [[ -n $GOPATH ]]; then
_strip-go-path "$GOPATH"
_unset-gopath
fi 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 # vim: ft=sh

1
zsh/rc
View file

@ -32,6 +32,7 @@ preexec() {
chpwd() { chpwd() {
__git_ps1_gitdir __git_ps1_gitdir
_gopath
} }
chpwd chpwd