Last active
May 18, 2017 20:01
-
-
Save loderunner/b69c7f3ee221d32061ef277cc522d795 to your computer and use it in GitHub Desktop.
Set environment variables for Go development to a given workspace.
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
# Set environment for Go development. Inspired by Python virtualenv | |
# This file must be used with "source activate_goenv.sh" *from bash* | |
# you cannot run it directly | |
deactivate () { | |
# reset old environment variables | |
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all | |
if ! [ -z "${_OLD_GOENV_GOPATH+_}" ] ; then | |
GOPATH="$_OLD_GOENV_GOPATH" | |
export GOPATH | |
unset _OLD_GOENV_GOPATH | |
fi | |
if ! [ -z "${_OLD_GOENV_PATH+_}" ] ; then | |
PATH="$_OLD_GOENV_PATH" | |
export PATH | |
unset _OLD_GOENV_PATH | |
fi | |
# This should detect bash and zsh, which have a hash command that must | |
# be called to get it to forget past commands. Without forgetting | |
# past commands the $PATH changes we made may not be respected | |
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then | |
hash -r 2>/dev/null | |
fi | |
if ! [ -z "${_OLD_GOENV_PS1+_}" ] ; then | |
PS1="$_OLD_GOENV_PS1" | |
export PS1 | |
unset _OLD_GOENV_PS1 | |
fi | |
unset GOENV | |
if [ ! "${1-}" = "nondestructive" ] ; then | |
# Self destruct! | |
unset -f deactivate | |
fi | |
} | |
# unset irrelevant variables | |
deactivate nondestructive | |
if ! [ -z "${1+_}" ] ; then | |
if [ -d "$1" ] ; then | |
GOENV="$1" | |
else | |
echo "$1: No such directory" && exit 2 | |
fi | |
else | |
GOENV="$PWD" | |
fi | |
export GOENV | |
# set GOPATH | |
_OLD_GOENV_GOPATH="$GOPATH" | |
GOPATH="$GOENV" | |
export GOPATH | |
_OLD_GOENV_PATH="$PATH" | |
PATH="$GOENV/bin:$PATH" | |
export PATH | |
_OLD_GOENV_PS1="$PS1" | |
PS1="($(basename "$GOENV")) $PS1" | |
export PS1 | |
# This should detect bash and zsh, which have a hash command that must | |
# be called to get it to forget past commands. Without forgetting | |
# past commands the $PATH changes we made may not be respected | |
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then | |
hash -r 2>/dev/null | |
fi |
Actually, since it's supposed to be source
d into the shell, it's not supposed to be executable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You probably want to start your script with a proper shebang:
#!/bin/bash