vns_mv now commits after renaming a note; code cleanup

This commit is contained in:
Vera Lewis
2024-04-01 03:49:15 -05:00
parent fb164789b1
commit 5e4ec8f765

51
vns
View File

@@ -20,6 +20,10 @@ vns_raise (){
vns_report () { 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_BLUE_COLOR='\033[0;34m'
local -r VNS_RESET_COLOR='\033[0m' local -r VNS_RESET_COLOR='\033[0m'
@@ -30,9 +34,10 @@ vns_checkDeps (){
# checkDeps # checkDeps
# Prints a list of missing dependencies # Prints a list of missing dependencies
# Currently these dependencies are {vim, tree, git}
for DEP in 'vim' 'tree' 'git'; do 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" printf "%s " "$DEP"
fi fi
done done
@@ -51,12 +56,15 @@ vns_sanityCheck () {
# Inform the user of missing dependencies and exit on code 20 if any were found # Inform the user of missing dependencies and exit on code 20 if any were found
if [ -n "$MISSING" ]; then if [ -n "$MISSING" ]; then
vns_raise "Missing Dependencies: $MISSING" 20 vns_raise "Missing Dependencies: $MISSING" 20
fi
# --- Store --- # --- Store ---
# Verify that the note store has been initialized # 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 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 fi
} }
@@ -66,24 +74,30 @@ vns_init () {
# init (GPG recipients...) # init (GPG recipients...)
# Prepares $VNS_STORE for use # Prepares $VNS_STORE for use
# Verify that recipients were specified
if [ "$#" -lt 1 ]; then if [ "$#" -lt 1 ]; then
vns_raise "No GPG Recipients Specified" 3 vns_raise "No GPG Recipients Specified" 3
# Re-encrypt existing notes, if present
elif [ -d "$VNS_STORE/.git" ]; then elif [ -d "$VNS_STORE/.git" ]; then
# Reset list of GPG recipients # Reset list of GPG recipients
echo -n '' > "${VNS_STORE}/.gpg-id" echo -n '' > "${VNS_STORE}/.gpg-id"
vns_reencrypt "$VNS_STORE" "$@" vns_reencrypt "$@"
# No existing store found; initialize a new one
else else
# Create a new VNS Store # Create a new VNS Store
git init "$VNS_STORE" >/dev/null git init "$VNS_STORE" >/dev/null
vns_report "Initialized new VNS store" vns_report "Initialized new VNS store"
fi fi
# Store GPG Recipients for later note creation # Store GPG Recipients for later note creation whether re-encrypting, or
# whether re-encrypting, or initializing a new store # initializing a new store
for recipient in "$@"; do for recipient in "$@"; do
echo "$recipient" >> "${VNS_STORE}/.gpg-id" echo "$recipient" >> "${VNS_STORE}/.gpg-id"
done done
@@ -93,9 +107,7 @@ vns_init () {
vns_reencrypt () { vns_reencrypt () {
# reencrypt (GPG recipients...) # reencrypt (GPG recipients...)
# Re-encrypt all notes with provided gpg recipients.
# Remove script name from arguments list
shift
# Construct list of arguments from provided recipients # Construct list of arguments from provided recipients
local -a GPG_RECIPS; local -a GPG_RECIPS;
@@ -116,13 +128,14 @@ vns_reencrypt () {
vns_gpgid () { vns_gpgid () {
# gpgid # gpgid
# Echo all recipients listed in .gpg-id # Print a list of all recipients listed in .gpg-id
# shellcheck disable=SC2002 # shellcheck disable=SC2002
cat "${VNS_STORE}/.gpg-id" | while read -r recipient; do cat "${VNS_STORE}/.gpg-id" | while read -r recipient; do
echo "-r" echo "-r"
echo "$recipient" echo "$recipient"
done done
} }
vns_printHelp (){ vns_printHelp (){
@@ -153,9 +166,7 @@ vns_list () {
# list (notebook) # list (notebook)
# Prints a tree containing all notes in the notebook # Prints a tree containing all notes in the notebook
# If no notebook is specified, the entire store is used # If no notebook is specified, all notes are shown
#numNotes () { find "${VNS_STORE}" -name "*.gpg" \( ! -regex '.*/\..*' \) | wc -l; }
# Check for default behavior # Check for default behavior
if [ "$#" -lt 1 ]; then if [ "$#" -lt 1 ]; then
@@ -177,9 +188,12 @@ vns_rm () {
# rm (note) # rm (note)
# removes (note) from the store # removes (note) from the store
# Verify notes were provided
if [ "$#" -lt 1 ]; then
vns_raise "Insufficient arguments" 30
fi
while [ "$#" -gt 0 ]; do while [ "$#" -gt 0 ]; do
# Verify $1 is bound
if [ "$#" -lt 1 ]; then vns_raise "Insufficient arguments" 30; fi
# Verify the note exists # Verify the note exists
if [ ! -r "$VNS_STORE/$1.gpg" ]; then if [ ! -r "$VNS_STORE/$1.gpg" ]; then
@@ -330,7 +344,12 @@ vns_mv () {
# Refuse to overwrite existing notes # Refuse to overwrite existing notes
if [ -r "$VNS_STORE/$2.gpg" ]; then vns_raise "Note $2 already exists" 72; fi 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 mv "$VNS_STORE/$1.gpg" "$VNS_STORE/$2.gpg"
vns_git commit -am "Renamed $1 -> $2"
} }
@@ -410,7 +429,6 @@ vns_git () {
} }
vns () { vns () {
# Bypass sanity check if told to initialize store # Bypass sanity check if told to initialize store
if [ "$1" != "-I" ]; then if [ "$1" != "-I" ]; then
vns_sanityCheck; vns_sanityCheck;
@@ -471,5 +489,4 @@ vns () {
exit 0; exit 0;
} }
vns "$@" vns "$@"