Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Created March 4, 2020 10:38
Show Gist options
  • Save loganlinn/90be9eb5750dde776c8d559088a73fed to your computer and use it in GitHub Desktop.
Save loganlinn/90be9eb5750dde776c8d559088a73fed to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Usage: log_warning [<message> ...]
#
# Logs a warning message. Acts like echo,
# but wraps output in the standard direnv log format
# (controlled by $DIRENV_LOG_FORMAT), and directs it
# to stderr rather than stdout.
#
# Example:
#
# log_warning "Undefined thing!"
log_warning() {
local color_normal
local color_error
color_normal=$(tput sgr0)
color_error=$(tput setaf 3)
if [[ -n $DIRENV_LOG_FORMAT ]]; then
local msg=$*
# shellcheck disable=SC2059,SC1117
printf "${color_error}${DIRENV_LOG_FORMAT}${color_normal}\n" "$msg" >&2
fi
}
# Usage: layout pyenv-virtualenv [python-version] <virtualenv-name>
#
# Examples:
#
# layout pyenv-virtualenv 3.6.7 webapp
#
# layout pyenv-virtualenv webapp
#
# Uses pyenv-virtualenv to activate a virtual environment under
# $PYENV_ROOT/versions, e.g. ~/.pyenv/versions. If version doesn't
# exist, it will be created.
#
layout_pyenv-virtualenv() {
if ! (( $# == 1 || $# == 2)); then
log_error "usage: layout virtualenv [python-version] <virtualenv-name>"
return 1
fi
local py_version
local venv_name
local virtualenv_prefix
py_version=${2:+$1}
venv_name=${2:-$1}
PYENV_VERSION=$1${2:+/envs/$2}
# test if exact match of version & name
virtualenv_prefix=$(pyenv virtualenv-prefix $PYENV_VERSION 2>/dev/null)
if [[ -z "$virtualenv_prefix" ]]; then
# doesn't exist, so we'll create it.
log_warning "pyenv: creating virtualenv $venv_name ($PYENV_VERSION)"
# test if using another version
virtualenv_prefix=$(pyenv virtualenv-prefix $venv_name 2>/dev/null)
if [[ -n $virtualenv_prefix ]]; then
log_warning "pyenv: overwriting virtualenv alias $venv_name ($(basename $virtualenv_prefix) -> $py_version)"
fi
pyenv deactivate 2>/dev/null || true
log_status pyenv virtualenv $py_version $venv_name
pyenv virtualenv --force $py_version $venv_name
fi
log_status "pyenv activatae $venv_name"
[[ -n "$PYENV_VERSION" ]] && export PYENV_VERSION
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment