From 04b385284a8559bde3df51bab950784a0fd28cfd Mon Sep 17 00:00:00 2001 From: Douglas Rumbaugh Date: Mon, 12 Sep 2022 22:40:03 -0400 Subject: Updates/adjustments --- ves-var-add.sh | 138 +++++++++++++++++++++++++++------------------------------ 1 file changed, 66 insertions(+), 72 deletions(-) (limited to 'ves-var-add.sh') diff --git a/ves-var-add.sh b/ves-var-add.sh index 7f0ee5a..d36647c 100755 --- a/ves-var-add.sh +++ b/ves-var-add.sh @@ -1,84 +1,78 @@ #!/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 +ves_var_add() { + if [ "$#" -lt 2 ]; then + printf "ERROR: Insufficient arguments provided.\n" + return 2 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="" -ENV="" + while :; do + case $1 in + --env=*) + env=$( echo $1 | cut -d "=" -f 2 ) + shift + break + ;; + --) + shift + break + ;; -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 + return 2 + ;; - -?*) - printf "ERROR: Invalid option [%s]\n" $1 > /dev/stderr - exit 2 - ;; + *) + break + esac + shift + done - *) - 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 + fi -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 ! _shves_check_env_name "$env"; then + return 1 + fi -if [ $# -lt 2 ]; then - printf "ERROR: Insufficient arguments provided.\n" - exit 2 -fi + if ! _shves_check_env_exists "$env"; then + return 1; + 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 + # 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" + else + var_value="$var_value:$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 + # And then update the file to use the new version. + sed -i "s/export_var:$var=.*"/export_var:$var=$var_value"/" "$fname" -# 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. + # If we are updating the currently active environment, export the + # new value as well, + if [ $active_env -eq 1 ]; then + 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 +} -- cgit v1.2.3