dotfiles/shell/go

57 lines
982 B
Bash

_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
}
_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
_set-gopath "$go_path"
_append-go-path "$GOPATH"
elif [[ -n $GOPATH ]]; then
_strip-go-path "$GOPATH"
_unset-gopath
fi
}
# vim: ft=sh