Last active
October 28, 2019 20:01
-
-
Save piersy/0570c7eaf3e8141eb7759647743e8790 to your computer and use it in GitHub Desktop.
Automatic gopath selection, and godoc web server execution with zshell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List Custom gopaths | |
gopaths=('/home/piers/projects/ethdenver' '/home/piers/projects/test_gopath') | |
# Always available gopath | |
fallback_gopath=$HOME/go | |
# Used to detect when the gopath changes | |
previous_gopath='' | |
#setgopath sets the gopath based on the current working dierectory. If the | |
#current working dierectory is a gopath or a subdirectory of a gopath then | |
#export that gopath, otherwise choose the first gopath as the default gopath | |
#and export that. This allows for shell navigation through many gopaths without | |
#having manually set the gopath. | |
function setgopath() { | |
# Remove bin paths from PATH | |
binpath=:$fallback_gopath/bin | |
export PATH="${PATH/$binpath/}" | |
if [[ ! -z $previous_gopath ]]; then | |
binpath=:$previous_gopath/bin | |
# ${X/Y/Z} Replace first occurrence of Y in X with Z. | |
export PATH="${PATH/$binpath/}" | |
fi | |
# Set current_path | |
current_path=`pwd` | |
# Set empty current_gopath. | |
current_gopath='' | |
for p in $gopaths; do | |
# if we are in a gopath then set the current_gopath. | |
if [[ $current_path == $p* ]]; then | |
current_gopath=$p | |
break | |
fi | |
done | |
unset GOPATH | |
if [[ ! -z $current_gopath ]]; then | |
export GOPATH="$current_gopath:$fallback_gopath" | |
export PATH="$PATH:$current_gopath/bin:$fallback_gopath/bin" | |
else | |
export GOPATH="$fallback_gopath" | |
export PATH="$PATH:$fallback_gopath/bin" | |
fi | |
# Restart godoc server if path has changed | |
if [[ $current_gopath != $previous_gopath ]]; then | |
# restart godoc server | |
killall godoc &> /dev/null | |
&> /dev/null (godoc -http=:6060 &) | |
fi | |
previous_gopath=$current_gopath | |
} | |
# Upadte the prompt with the time taken from the timer. | |
function precmd() { | |
# set the gopath based on our current path. | |
setgopath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment