mirror of
https://github.com/lloeki/dotfiles.git
synced 2025-12-06 07:24:39 +01:00
proper automatic go workspace path
This commit is contained in:
parent
f5d7ca34b8
commit
b34f8bcd04
2 changed files with 34 additions and 17 deletions
50
shell/go
50
shell/go
|
|
@ -1,5 +1,3 @@
|
|||
go_commands=(go vim)
|
||||
|
||||
_go-installed() {
|
||||
which go > /dev/null 2>&1
|
||||
}
|
||||
|
|
@ -7,35 +5,53 @@ _go-installed() {
|
|||
_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() {
|
||||
_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
|
||||
if _go-installed && go_path=$(_within-go-project); then
|
||||
env PATH="$PATH:$go_path/bin" GOPATH="$go_path" "$@"
|
||||
else
|
||||
"$@"
|
||||
_set-gopath "$go_path"
|
||||
_append-go-path "$GOPATH"
|
||||
elif [[ -n $GOPATH ]]; then
|
||||
_strip-go-path "$GOPATH"
|
||||
_unset-gopath
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue