Last active
October 23, 2016 13:42
-
-
Save nelsam/c51cd8ce6919ccbcbba2 to your computer and use it in GitHub Desktop.
Every time the directory is changed in zsh, set $GOPATH (and additions to $PATH) by recursing up through parent directories, looking for a .gopath file
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
# Each time we change directory, look for some files that alter environment | |
# variables. | |
chpwd() { | |
emulate -L zsh | |
dir="$(pwd)" | |
while [[ "$dir" == "$HOME"* ]] | |
do | |
if [[ -f "${dir}/.gopath" ]] | |
then | |
newGOPATH="${newGOPATH}:${dir}" | |
fi | |
dir=$(dirname "${dir}") | |
done | |
if [[ "$newGOPATH" != "$GOPATH" ]] | |
then | |
pathReplace='s|\(:[^:]*\)|\1/bin|g' | |
newPathAppend=$(echo "${newGOPATH}" | sed "${pathReplace}") | |
if [[ $? -ne 0 ]] | |
then | |
echo "Replacement failed using: echo '${newGOPATH}' | sed '${pathReplace}'" | |
return | |
fi | |
oldPathAppend=$(echo "${GOPATH}" | sed "${pathReplace}") | |
if [[ $? -ne 0 ]] | |
then | |
echo "Replacement failed using: echo '${GOPATH}' | sed '${pathReplace}'" | |
return | |
fi | |
newPATH=$(echo "${PATH}" | sed "s|${oldPathAppend}\$|${newPathAppend}|") | |
if [[ $? -ne 0 ]] | |
then | |
echo "Replacement failed using: echo '${PATH}' | sed 's|${oldPathAppend}\$|${newPathAppend}|'" | |
return | |
fi | |
export PATH="${newPATH}" | |
if [[ "${newGOPATH}" != "" ]] | |
then | |
export GOPATH="${newGOPATH}" | |
else | |
unset GOPATH | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment