aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDouglas Rumbaugh <doug@douglasrumbaugh.com>2021-12-20 18:53:23 -0500
committerDouglas Rumbaugh <doug@douglasrumbaugh.com>2021-12-20 18:53:23 -0500
commit1f96ab1edf18b54578db177921f9d096c913e343 (patch)
treefa7dca90d1428881108ef835537b296fc48e320c
parent3e961e27090546e2d901e38ef00c4af077bf6308 (diff)
downloadsh-ves-1f96ab1edf18b54578db177921f9d096c913e343.tar.gz
ves-var-add.sh: Initial version
-rwxr-xr-x[-rw-r--r--]ves-var-add.sh83
1 files changed, 83 insertions, 0 deletions
diff --git a/ves-var-add.sh b/ves-var-add.sh
index 1a24852..7f0ee5a 100644..100755
--- a/ves-var-add.sh
+++ b/ves-var-add.sh
@@ -1 +1,84 @@
#!/bin/sh
+
+if [ -z ${XDG_DATA_HOME+x} ]; then
+ ENV_DIR="$HOME/.local/share/ves/envs"
+else
+ ENV_DIR="$XDG_DATA_HOME/ves/envs"
+fi
+
+check_name() {
+ if ! [ -f "$ENV_DIR/$1" ]; then
+ printf "ERROR: Environment [%s] does not exist.\n" $1 > /dev/stderr
+ exit 1
+ fi
+
+ if ! echo $1 | grep "^[[:alpha:][:digit:]_-]*$" > /dev/null; then
+ printf "ERROR: Environment [%s] is invalid. Name must contain only letters, -, and _\n" $1 > /dev/stderr
+ exit 1
+ fi
+}
+
+ENV=""
+
+while :; do
+ case $1 in
+ --env=*)
+ ENV=$( echo $1 | cut -d "=" -f 2 )
+ shift
+ break
+ ;;
+ --)
+ shift
+ break
+ ;;
+
+ -?*)
+ printf "ERROR: Invalid option [%s]\n" $1 > /dev/stderr
+ exit 2
+ ;;
+
+ *)
+ break
+ esac
+ shift
+done
+
+if ! [ -z $ENV ]; then
+ check_name $ENV
+elif ! [ -z $VES_ENV_NM ]; then
+ # this check_name shouldn't be necessary, but I suppose someone
+ # could always manually reassign the variable, so we'd better
+ # check it.
+ check_name $VES_ENV_NM
+ ENV=$VES_ENV_NM
+else
+ printf "ERROR: No valid environment active or specified\n" > /dev/stderr
+ exit 2
+fi
+
+if [ $# -lt 2 ]; then
+ printf "ERROR: Insufficient arguments provided.\n"
+ exit 2
+fi
+
+# Ah, bash would have made this so much nicer... alas
+# if [ -z ${!1+x} ]; then ...
+if 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
+ exit 2
+fi
+
+# If we get down here, all is good. We can go ahead and prepend
+# :$2 to the variable contained in $1. Again, no easy indirection,
+# so we must use eval.
+if eval [ -z \${$1} ]; then
+ export $1=$2
+else
+ eval export "$1"="$2":\$"$1"
+fi
+
+# Perhaps we could use . to run this script from the main caller
+# which could be set up as a shell function?
+# That approach seems to work to modify the parent shell. Just
+# gotta set up the main guy as a function.