From 5e4ec8f765512c77b6095fe0cf95194db6f14823 Mon Sep 17 00:00:00 2001 From: Vera Lewis Date: Mon, 1 Apr 2024 03:49:15 -0500 Subject: [PATCH] vns_mv now commits after renaming a note; code cleanup --- vns | 67 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/vns b/vns index b21119b..cb4d2a8 100644 --- a/vns +++ b/vns @@ -20,6 +20,10 @@ vns_raise (){ vns_report () { + # report (message) + # Prints message to the console, formatted with a blue asterisk to indicate + # some successful operation has completed + local -r VNS_BLUE_COLOR='\033[0;34m' local -r VNS_RESET_COLOR='\033[0m' @@ -30,9 +34,10 @@ vns_checkDeps (){ # checkDeps # Prints a list of missing dependencies + # Currently these dependencies are {vim, tree, git} for DEP in 'vim' 'tree' 'git'; do - if test ! -e "$(command -v "$DEP" 2>/dev/null)"; then + if ! command -v "$DEP" >/dev/null 2>&1; then printf "%s " "$DEP" fi done @@ -51,12 +56,15 @@ vns_sanityCheck () { # Inform the user of missing dependencies and exit on code 20 if any were found if [ -n "$MISSING" ]; then vns_raise "Missing Dependencies: $MISSING" 20 - fi # --- Store --- # Verify that the note store has been initialized - if [ ! -d "$VNS_STORE" ]; then + elif [ ! -d "$VNS_STORE" ]; then vns_raise "The vns store needs to be initialized (hint: -I)" 21 + + # Verify that intended GPG recipients are stored in compliance with v2.0 + elif [ ! -r "$VNS_STORE/.gpg-id" ]; then + vns_raise "Re-initialize (-I) with gpg recipients. Existing notes will be re-encrypted" 21 fi } @@ -66,24 +74,30 @@ vns_init () { # init (GPG recipients...) # Prepares $VNS_STORE for use + # Verify that recipients were specified if [ "$#" -lt 1 ]; then vns_raise "No GPG Recipients Specified" 3 - + + # Re-encrypt existing notes, if present elif [ -d "$VNS_STORE/.git" ]; then # Reset list of GPG recipients echo -n '' > "${VNS_STORE}/.gpg-id" - vns_reencrypt "$VNS_STORE" "$@" + vns_reencrypt "$@" + + # No existing store found; initialize a new one else + # Create a new VNS Store git init "$VNS_STORE" >/dev/null vns_report "Initialized new VNS store" fi - # Store GPG Recipients for later note creation - # whether re-encrypting, or initializing a new store + # Store GPG Recipients for later note creation whether re-encrypting, or + # initializing a new store + for recipient in "$@"; do echo "$recipient" >> "${VNS_STORE}/.gpg-id" done @@ -93,9 +107,7 @@ vns_init () { vns_reencrypt () { # reencrypt (GPG recipients...) - - # Remove script name from arguments list - shift + # Re-encrypt all notes with provided gpg recipients. # Construct list of arguments from provided recipients local -a GPG_RECIPS; @@ -116,13 +128,14 @@ vns_reencrypt () { vns_gpgid () { # gpgid - # Echo all recipients listed in .gpg-id + # Print a list of all recipients listed in .gpg-id # shellcheck disable=SC2002 cat "${VNS_STORE}/.gpg-id" | while read -r recipient; do echo "-r" echo "$recipient" done + } vns_printHelp (){ @@ -153,22 +166,20 @@ vns_list () { # list (notebook) # Prints a tree containing all notes in the notebook - # If no notebook is specified, the entire store is used - - #numNotes () { find "${VNS_STORE}" -name "*.gpg" \( ! -regex '.*/\..*' \) | wc -l; } + # If no notebook is specified, all notes are shown # Check for default behavior if [ "$#" -lt 1 ]; then local -r "NOTEBOOK"="" - printf "%s\\n" "vns" + printf "%s\\n" "vns" else readonly NOTEBOOK="$1" - printf "%s\\n" "$1" - fi + printf "%s\\n" "$1" + fi - tree --noreport --prune "$VNS_STORE/$NOTEBOOK"\ - | tail -n +2\ - | sed s/\.gpg//g + tree --noreport --prune "$VNS_STORE/$NOTEBOOK"\ + | tail -n +2\ + | sed s/\.gpg//g } @@ -176,11 +187,14 @@ vns_rm () { # rm (note) # removes (note) from the store + + # Verify notes were provided + if [ "$#" -lt 1 ]; then + vns_raise "Insufficient arguments" 30 + fi while [ "$#" -gt 0 ]; do - # Verify $1 is bound - if [ "$#" -lt 1 ]; then vns_raise "Insufficient arguments" 30; fi - + # Verify the note exists if [ ! -r "$VNS_STORE/$1.gpg" ]; then vns_raise "Note does not exist" 31; @@ -329,8 +343,13 @@ vns_mv () { # Refuse to overwrite existing notes if [ -r "$VNS_STORE/$2.gpg" ]; then vns_raise "Note $2 already exists" 72; fi + + # Create any necessary directories + mkdir -p "$VNS_STORE/$(dirname "$2.gpg")" + # Rename the note vns_git mv "$VNS_STORE/$1.gpg" "$VNS_STORE/$2.gpg" + vns_git commit -am "Renamed $1 -> $2" } @@ -410,7 +429,6 @@ vns_git () { } vns () { - # Bypass sanity check if told to initialize store if [ "$1" != "-I" ]; then vns_sanityCheck; @@ -471,5 +489,4 @@ vns () { exit 0; } - vns "$@"