diff options
Diffstat (limited to 'ves-activate.sh')
| -rwxr-xr-x[-rw-r--r--] | ves-activate.sh | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/ves-activate.sh b/ves-activate.sh index 1a24852..5199f5b 100644..100755 --- a/ves-activate.sh +++ b/ves-activate.sh @@ -1 +1,82 @@ #!/bin/sh +# +# Activate an sh-ves environment in the current shell. The prior values of +# all variables the environment overrides are saved in SHVES_SAVED_<var> +# shell variables (with SHVES_SAVEDSET_<var> recording whether the variable +# existed at all), so that ves_deactivate can restore them exactly. +# +ves_activate() { + if [ "$#" -lt 1 ]; then + printf "ERROR: No environment name specified.\n" >&2 + return 1 + fi + + if [ -n "$SHVES_ENV_NM" ]; then + printf "ERROR: Environment [%s] is already active. Deactivate it first.\n" "$SHVES_ENV_NM" >&2 + return 1 + fi + + env_name="$1" + if ! _shves_check_env_name "$env_name"; then + return 1 + fi + + if ! _shves_check_env_exists "$env_name"; then + return 1 + fi + + SHVES_SAVED_VARS="" + + while IFS= read -r line; do + case $line in + export_var:*) + ;; + *) + continue + ;; + esac + + entry="${line#export_var:}" + var="${entry%%=*}" + value="${entry#*=}" + + if ! _shves_check_var_name "$var"; then + continue + fi + + # save the prior value (and whether the variable was set at all) + # so that deactivation can restore the environment exactly + _shves_live_export "$var" "$value" + done < "$SHVES_ENV_DIR/$env_name" + + SHVES_ENV_NM="$env_name" + export SHVES_ENV_NM + + return 0 +} + +# +# Switch to another environment: deactivate the current one (if any) and +# activate the named one. Pure convenience; environments still do not +# compose. +# +ves_switch() { + if [ "$#" -lt 1 ]; then + printf "ERROR: No environment name specified.\n" >&2 + return 1 + fi + + if ! _shves_check_env_name "$1"; then + return 1 + fi + + if ! _shves_check_env_exists "$1"; then + return 1 + fi + + if [ -n "$SHVES_ENV_NM" ]; then + ves_deactivate || return 1 + fi + + ves_activate "$1" +} |