Skip to content

Instantly share code, notes, and snippets.

@jrnold
Created November 4, 2021 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrnold/eea323eb1e1482ae0638c41ad7a71307 to your computer and use it in GitHub Desktop.
Save jrnold/eea323eb1e1482ae0638c41ad7a71307 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# Add or subtract python@
zmodload zsh/pcre
brew_python function() {
__add_version () {
version="$1"
if [[ ! "$version" =~ "^[2-3]\.[0-9]+$" ]]; then
echo "Not a valid python version" 1>&2
return 1
fi
if ! brew list "python@$version" &>/dev/null; then
echo "Error: python@$version not installed" 1>&2
return 1
fi
local bin_path="$(brew --prefix python@$version)/bin"
local libexec_bin_path="$(brew --prefix python@$version)/libexec/bin"
path=($bin_path $libexec_bin_path $path)
export PATH
}
__remove_version () {
version="$1"
if [[ ! "$version" =~ "^[2-3]\.[0-9]+$" ]]; then
echo "Not a valid python version" 1>&2
return 1
fi
prefix="$(brew --prefix python@$version 2>/dev/null)" || echo "python@$version not installed" >&2 && return 1
echo "Removing ${path:#$prefix/bin}:${path:#$prefix/libexec/bin} from PATH"
path=(${path:#$prefix/bin})
path=(${path:#$prefix/libexec/bin})
}
if [[ -z "$@" ]]; then
echo "Usage: version" 1>&2
return 1
fi
typeset subcmd="$1"
case $subcmd in
remove)
__remove_version $2
;;
add)
__add_version $2
;;
*)
echo "Usage: version [remove|add] python-version" 1>&2
return 1
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment