aboutsummaryrefslogtreecommitdiffstats
path: root/ves-var-add.sh
diff options
context:
space:
mode:
authorDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2026-06-06 12:27:03 -0400
committerDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2026-06-06 12:27:03 -0400
commit0ecfe53b2d271133fac36de11ecfc0f7e47840f0 (patch)
tree3ee8b5188936e350e15ff851b07a33031d366389 /ves-var-add.sh
parent04b385284a8559bde3df51bab950784a0fd28cfd (diff)
downloadsh-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-add.sh')
-rwxr-xr-xves-var-add.sh93
1 files changed, 47 insertions, 46 deletions
diff --git a/ves-var-add.sh b/ves-var-add.sh
index d36647c..01bc457 100755
--- a/ves-var-add.sh
+++ b/ves-var-add.sh
@@ -1,78 +1,79 @@
#!/bin/sh
+#
+# Prepend an entry to a path-like (:-delimited) variable within an sh-ves
+# environment, or append it with --append. Values not beginning with / or
+# ./ are expanded relative to the sh-ves data directories (see
+# _shves_expand_path). If the target environment is currently active, the
+# variable is also updated in the live shell.
+#
ves_var_add() {
- if [ "$#" -lt 2 ]; then
- printf "ERROR: Insufficient arguments provided.\n"
- return 2
- fi
-
env=""
+ append=0
while :; do
case $1 in
--env=*)
- env=$( echo $1 | cut -d "=" -f 2 )
+ env="${1#--env=}"
shift
- break
;;
- --)
+ --append)
+ append=1
shift
- break
- ;;
-
- -?*)
- printf "ERROR: Invalid option [%s]\n" $1 > /dev/stderr
- return 2
;;
-
*)
break
+ ;;
esac
- shift
done
- active_env=0
- if [ -z "$env" ]; then
- if ! [ -z "$SHVES_ENV_NM"]; then
- env="$SHVES_ENV_NM"
- active_env=1
- else
- printf "ERROR: No valid environment active or specified\n" > /dev/stderr
- return 1
- fi
+ if [ "$#" -lt 2 ]; then
+ printf "ERROR: Insufficient arguments. usage: ves var-add [--env=<name>] <variable> <value>\n" >&2
+ return 2
fi
- if ! _shves_check_env_name "$env"; then
+ var="$1"
+ value="$2"
+
+ if ! env=$(_shves_resolve_env "$env"); then
return 1
fi
- if ! _shves_check_env_exists "$env"; then
- return 1;
+ if ! _shves_check_var_name "$var"; then
+ return 1
+ fi
+
+ value=$(_shves_expand_path "$var" "$value")
+
+ # Get the current value from the environment file and prepend the new
+ # entry to it. If the variable is not stored in the environment at all,
+ # seed it from the live shell value first, so that prepending to PATH
+ # and friends never truncates them. An explicitly stored empty value
+ # (as created by ves create --override) is respected, however.
+ fname="$SHVES_ENV_DIR/$env"
+ if _shves_has_var "$fname" "$var"; then
+ var_value=$(_shves_get_var "$fname" "$var")
+ else
+ # Read the live shell value of the variable named in $var,
+ # defaulting to empty if it is unset. For $var = PATH, this
+ # expands to: var_value=${PATH:-}
+ eval "var_value=\${$var:-}"
fi
- # First, we will get the variable value from the environment file and
- # create our new version of it.
- fname="$SHVES_ENV_DIR"/"$env"
- var_value=$(grep "export_var:$var=" $SHVES_ENV_DIR/test2 | cut -d '=' -f 2)
if [ -z "$var_value" ]; then
- var_value="$2"
+ var_value="$value"
+ elif [ "$append" -eq 1 ]; then
+ var_value="$var_value:$value"
else
- var_value="$var_value:$2"
+ var_value="$value:$var_value"
fi
- # And then update the file to use the new version.
- sed -i "s/export_var:$var=.*"/export_var:$var=$var_value"/" "$fname"
+ _shves_set_var "$fname" "$var" "$var_value"
# If we are updating the currently active environment, export the
- # new value as well,
- if [ $active_env -eq 1 ]; then
- export "$var"="$var_value"
+ # new value as well.
+ if [ "$env" = "$SHVES_ENV_NM" ]; then
+ _shves_live_export "$var" "$var_value"
fi
- # Ah, bash would have made this so much nicer... alas
- # if [ -z ${!1+x} ]; then ...
-# if [ "$active_env" -eq 1 ] && eval [ -z \${$1+x} ]; then
-# printf "ERROR: Specified variable [%s] does not exist in environment.\n" $1 > /dev/stderr
-# printf "\tYou can create it with\n\t ves export %s\n " $1 > /dev/stderr
-# return 2
-# fi
+ return 0
}