Skip to content

Instantly share code, notes, and snippets.

@nelsam
Last active October 23, 2016 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsam/c51cd8ce6919ccbcbba2 to your computer and use it in GitHub Desktop.
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
# 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