From 04b385284a8559bde3df51bab950784a0fd28cfd Mon Sep 17 00:00:00 2001 From: Douglas Rumbaugh Date: Mon, 12 Sep 2022 22:40:03 -0400 Subject: Updates/adjustments --- Makefile | 4 +- ves-create.sh | 74 +++++++++++-------------------- ves-delete.sh | 36 ++++++--------- ves-export.sh | 0 ves-init.sh | 65 +++++++++++++++++++++++++++ ves-list.sh | 9 +--- ves-prompt.sh | 8 ++-- ves-var-add.sh | 138 +++++++++++++++++++++++++++------------------------------ ves-var-rm.sh | 3 ++ ves.sh | 26 +++++------ 10 files changed, 192 insertions(+), 171 deletions(-) create mode 100644 ves-export.sh create mode 100644 ves-init.sh diff --git a/Makefile b/Makefile index 6239d48..9262db1 100644 --- a/Makefile +++ b/Makefile @@ -22,9 +22,7 @@ install: - test -d $(DATA_DIR)/ves/envs || mkdir -p $(DATA_DIR)/ves/envs - test -d $(CONF_DIR)/ves || mkdir -p $(CONF_DIR)/ves - test -d $(BIN_DIR)/ves_scripts || mkdir -p $(BIN_DIR)/ves_scripts - cp ./ves.sh $(BIN_DIR)/ves - chmod u+x $(BIN_DIR)/ves - cp ./ves-*.sh $(BIN_DIR)/ves_scripts + cp ./*.sh $(BIN_DIR)/ves_scripts chmod u+x $(BIN_DIR)/ves_scripts/* diff --git a/ves-create.sh b/ves-create.sh index 36027f6..b4f6467 100644 --- a/ves-create.sh +++ b/ves-create.sh @@ -1,60 +1,36 @@ #!/bin/sh +_shves_create_env() { + file="$SHVES_ENV_DIR/$1" + printf "name: $1\n" > $file -OVERRIDE_VARS="PATH LDPATH" - -if [ -z $XDG_DATA_HOME ]; 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] already exists.\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 + if [ "$2" -eq "1" ]; then + for var in $SHVES_OVERRIDE_VARS; do + printf "export_var:%s=\n" "$var" >> $file + done fi } +ves_create() { + if [ "$#" -lt 1 ]; then + printf "ERROR: No environment name specified.\n" > /dev/stderr + return 1 + fi -create_env() { - FILE="$ENV_DIR/$1" - printf "name: $1\n" > $FILE + override=0 + env_name="$1" + test "$#" -eq "2" && override=1 - if [ "$OVERRIDE" -eq "1" ]; then - for VAR in $OVERRIDE_VARS; do - printf "export_var:%s=%s\n" $VAR '""' >> $FILE - done + if ! _shves_check_env_name "$env_name"; then + return 1; fi -} -OVERRIDE=0 -while :; do - case $1 in - --override) - OVERRIDE=1 - ;; - --) - shift - break - ;; - -?*) - printf "ERROR: Invalid option [%s]\n" $1 > /dev/stderr - exit 2 - ;; - *) - break - esac - shift -done -ENV_NAME="$1" + if _shves_check_env_exists "$env_name" 1; then + printf "ERROR: Environment [%s] already exists.\n" "$env_name" + return 1; + fi -check_name "$1" -create_env "$1" "$OVERRIDE" + _shves_create_env "$env_name" "$override" -printf "Environment [%s] created successfully\n" $ENV_NAME -printf "To activate, execute\nves activate %s\n" $ENV_NAME + printf "Environment [%s] created successfully\n" "$env_name" + printf "To activate, execute\n\$ ves activate %s\n" "$env_name" +} diff --git a/ves-delete.sh b/ves-delete.sh index d7e6fc6..6d1557b 100644 --- a/ves-delete.sh +++ b/ves-delete.sh @@ -1,29 +1,21 @@ #!/bin/sh - -OVERRIDE_VARS="PATH LDPATH" - -if [ -z $XDG_DATA_HOME ]; 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_delete() { + if [ "$#" -lt 1 ]; then + printf "ERROR: No environment name specified.\n" > /dev/stderr + return 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 + env_name="$1" + if ! _shves_check_env_name "$env_name"; then + return 1 fi -} + if ! _shves_check_env_exists "$env_name"; then + return 1; + fi -ENV_NAME="$1" - -check_name "$1" -rm -f "$ENV_DIR/$ENV_NAME" + rm -f "$SHVES_ENV_DIR/$env_name" + printf "Environment [%s] deleted successfully\n" $env_name -printf "Environment [%s] deleted successfully\n" $ENV_NAME + return 0 +} diff --git a/ves-export.sh b/ves-export.sh new file mode 100644 index 0000000..e69de29 diff --git a/ves-init.sh b/ves-init.sh new file mode 100644 index 0000000..e323aaa --- /dev/null +++ b/ves-init.sh @@ -0,0 +1,65 @@ +#! /bin/sh +# +# Initialize the sh-ves system within a shell session by sourcing the relevant +# files to add function definitions for functionality that cannot be +# implemented via scripts, as well as initialing some local variables used by +# the system. This file should be sourced in your shell's rc or profile file. +# +# Note, the correct functioning of other sh-ves functions requires that this +# file be sourced prior to calling them. As such, they will check for +# SH_VES_INIT to be created prior to doing anything. If is not suggested to try +# to set this variable manually to get around sourcing this file. +# +# This file also provides default values for several sh-ves configuration +# variables. You may override these settings by setting the variables +# prior to sourcing this file, in which case they will not be modified, +# or by overwriting their values after sourcing the file. + +if [ -z "${XDG_DATA_HOME+x}" ]; then + SHVES_ENV_DIR="$HOME/.local/share/ves/envs" +else + SHVES_ENV_DIR="$XDG_DATA_HOME/ves/envs" +fi + +if [ -z "${SHVES_OVERRIDE_VARS+x}" ]; then + SHVES_OVERRIDE_VARS="PATH LDPATH LD_LIBRARY_PATH CPATH" +fi + +if [ -z "${SHVES_SCRIPTS_DIR+x}" ]; then + SHVES_BIN="$HOME/.local/bin/ves_scripts" +else + SHVES_BIN="$SHVES_SCRIPTS_DIR" +fi + +_shves_check_env_name() { + 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 + return 1 + fi + + return 0 +} + + +_shves_check_env_exists() { + if [ ! -f "$SHVES_ENV_DIR/$1" ]; then + if [ "$#" -lt 2 ]; then + printf "ERROR: Environment [%s] does not exist.\n" $1 > /dev/stderr + fi + + return 1 + fi + + return 0 +} + +. "$SHVES_BIN"/ves.sh +. "$SHVES_BIN"/ves-export.sh +. "$SHVES_BIN"/ves-activate.sh +. "$SHVES_BIN"/ves-deactivate.sh +. "$SHVES_BIN"/ves-var-add.sh +. "$SHVES_BIN"/ves-var-rm.sh +. "$SHVES_BIN"/ves-create.sh +. "$SHVES_BIN"/ves-delete.sh + + diff --git a/ves-list.sh b/ves-list.sh index 29844bf..1b90703 100644 --- a/ves-list.sh +++ b/ves-list.sh @@ -1,13 +1,6 @@ #!/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 - -basename -a $ENV_DIR/* - +basename -a "$SHVES_ENV_DIR"/* #for i in "$ENV_DIR/*"; do # printf "%s\n" $(basename "$i") #done diff --git a/ves-prompt.sh b/ves-prompt.sh index edc7133..1d79373 100644 --- a/ves-prompt.sh +++ b/ves-prompt.sh @@ -1,11 +1,11 @@ #!/bin/sh if [ $# -lt 1 ]; then - SYM="VENV" + sym="VENV" else - SYM="$1" + sym="$1" fi -if [ ! -z $VES_ENV_NM ]; then - printf "(%s %s)" $SYM $VES_ENV_NM +if [ ! -z "$SHVES_ENV_NM" ]; then + printf "(%s %s)" "$sym" "$VES_ENV_NM" fi 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 +} diff --git a/ves-var-rm.sh b/ves-var-rm.sh index 1a24852..c0b2ecd 100644 --- a/ves-var-rm.sh +++ b/ves-var-rm.sh @@ -1 +1,4 @@ #!/bin/sh +ves_var_rm() { + echo "not implemented" +} diff --git a/ves.sh b/ves.sh index e9755cd..28325db 100644 --- a/ves.sh +++ b/ves.sh @@ -1,9 +1,8 @@ #!/bin/sh ves() { - if [ -z "${VES_SCRIPTS_DIR+x}" ]; then - VES_BIN="$HOME/.local/bin/ves_scripts" - else - VES_BIN="$VES_SCRIPTS_DIR" + if [ "$#" -lt 1 ]; then + printf "ERROR: Please specify a command.\n" + return fi @@ -11,50 +10,51 @@ ves() { case $1 in create) shift - . $VES_BIN/ves-create.sh $@ + ves_create $@ break ;; delete) shift - . $VES_BIN/ves-delete.sh $@ + ves_delete $@ break ;; var-add) shift - . $VES_BIN/ves-addvar.sh $@ + ves_var_add $@ break ;; var-rm) shift - . $VES_BIN/ves-rmvar.sh $@ + ves_var_rm $@ break ;; export) shift - . $VES_BIN/ves-export.sh $@ + ves_export $@ break ;; list) shift - . $VES_BIN/ves-list.sh $@ + "$SHVES_BIN"/ves-list.sh $@ break ;; activate) shift - . $VES_BIN/ves-activate.sh $@ + ves_activate $@ break ;; deactivate) shift - . $VES_BIN/ves-deactivate.sh $@ + ves_deactivate $@ break ;; prompt) shift - . $VES_BIN/ves-prompt.sh $@ + . $SHVES_BIN/ves-prompt.sh $@ break ;; *) printf "ERROR: Invalid command [%s]\n" $1 > /dev/stderr + return esac } -- cgit v1.2.3