dotfiles/shell/go
2017-05-29 11:48:00 +02:00

76 lines
1.5 KiB
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 [[ -d "$check_dir/.gopath" ]]; then
echo "$check_dir/.gopath"
return
elif [[ -f "$check_dir/.gopath" ]]; then
local gopath="$(<"$check_dir/.gopath")"
if [[ -z "$gopath" ]]; then
echo "$check_dir"
else
echo "$gopath"
fi
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
}
_go-package() {
git remote get-url origin | perl -ne '/@(.*).git/ and { $_ = "$1" and s/:/\// and print }'
}
_link-go-package() {
local pkgn="$(_go-package)"
local src="$(_within-go-project)/src"
mkdir -p "$src/$(dirname $pkgn)"
ln -sf "$PWD" "$src/$pkgn"
}
# vim: ft=sh