diff options
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 } |