diff options
| author | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2026-06-06 12:27:03 -0400 |
|---|---|---|
| committer | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2026-06-06 12:27:03 -0400 |
| commit | 0ecfe53b2d271133fac36de11ecfc0f7e47840f0 (patch) | |
| tree | 3ee8b5188936e350e15ff851b07a33031d366389 /ves-var-rm.sh | |
| parent | 04b385284a8559bde3df51bab950784a0fd28cfd (diff) | |
| download | sh-ves-0ecfe53b2d271133fac36de11ecfc0f7e47840f0.tar.gz | |
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
Diffstat (limited to 'ves-var-rm.sh')
| -rwxr-xr-x[-rw-r--r--] | ves-var-rm.sh | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/ves-var-rm.sh b/ves-var-rm.sh index c0b2ecd..cc0a1e1 100644..100755 --- a/ves-var-rm.sh +++ b/ves-var-rm.sh @@ -1,4 +1,72 @@ #!/bin/sh +# +# Remove an entry from a path-like (:-delimited) variable within an sh-ves +# environment. The value is expanded with the same rules as ves var-add, so +# entries can be removed using the same form they were added with. If the +# target environment is currently active, the variable is also updated in +# the live shell. +# ves_var_rm() { - echo "not implemented" + env="" + case $1 in + --env=*) + env="${1#--env=}" + shift + ;; + esac + + if [ "$#" -lt 2 ]; then + printf "ERROR: Insufficient arguments. usage: ves var-rm [--env=<name>] <variable> <value>\n" >&2 + return 2 + fi + + var="$1" + value="$2" + + if ! env=$(_shves_resolve_env "$env"); then + return 1 + fi + + if ! _shves_check_var_name "$var"; then + return 1 + fi + + value=$(_shves_expand_path "$var" "$value") + + 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 + + var_value=$(_shves_get_var "$fname" "$var") + + # Rebuild the :-delimited list, dropping every element matching the + # value to remove. + new_value="" + found=0 + _shves_split_begin + for entry in $var_value; do + if [ "$entry" = "$value" ]; then + found=1 + elif [ -z "$new_value" ]; then + new_value="$entry" + else + new_value="$new_value:$entry" + fi + done + _shves_split_end + + if [ "$found" -eq 0 ]; then + printf "ERROR: Entry [%s] not present in variable [%s].\n" "$value" "$var" >&2 + return 1 + fi + + _shves_set_var "$fname" "$var" "$new_value" + + if [ "$env" = "$SHVES_ENV_NM" ]; then + _shves_live_export "$var" "$new_value" + fi + + return 0 } |