From 0ecfe53b2d271133fac36de11ecfc0f7e47840f0 Mon Sep 17 00:00:00 2001 From: "Douglas B. Rumbaugh" Date: Sat, 6 Jun 2026 12:27:03 -0400 Subject: Initial version complete I dusted this off after years and had Claude finish it for me. caveat emptor: this is largely (though not entirely) LLM generated as of this commit --- ves-activate.sh | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) mode change 100644 => 100755 ves-activate.sh (limited to 'ves-activate.sh') diff --git a/ves-activate.sh b/ves-activate.sh old mode 100644 new mode 100755 index 1a24852..5199f5b --- 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_ +# 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" +} -- cgit v1.2.3