#!/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_ # shell variables (with SHVES_SAVEDSET_ 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" }