diff options
Diffstat (limited to 'ves-unset.sh')
| -rw-r--r-- | ves-unset.sh | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/ves-unset.sh b/ves-unset.sh new file mode 100644 index 0000000..d25a178 --- /dev/null +++ b/ves-unset.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# +# Remove a variable from an sh-ves environment entirely; the inverse of +# ves export. If the target environment is currently active, the live +# variable is restored to its pre-activation value, just as ves deactivate +# would do. +# +ves_unset() { + env="" + case $1 in + --env=*) + env="${1#--env=}" + shift + ;; + esac + + if [ "$#" -lt 1 ]; then + printf "ERROR: No variable specified. usage: ves unset [--env=<name>] <variable>\n" >&2 + return 2 + fi + + var="$1" + + if ! env=$(_shves_resolve_env "$env"); then + return 1 + fi + + if ! _shves_check_var_name "$var"; then + return 1 + fi + + fname="$SHVES_ENV_DIR/$env" + if ! _shves_has_var "$fname" "$var"; then + printf "ERROR: Variable [%s] does not exist in environment [%s].\n" "$var" "$env" >&2 + return 1 + fi + + _shves_del_var "$fname" "$var" + + # If the target environment is active and the variable was part of the + # activation save-state, restore the pre-activation value in the live + # shell and drop the variable from the bookkeeping. + if [ "$env" = "$SHVES_ENV_NM" ] && _shves_is_saved "$var"; then + _shves_restore_var "$var" + + new_saved="" + for v in $SHVES_SAVED_VARS; do + if [ "$v" != "$var" ]; then + new_saved="$new_saved $v" + fi + done + SHVES_SAVED_VARS="$new_saved" + export SHVES_SAVED_VARS + fi + + return 0 +} |