From 0432e9d86f86953e7249906a352cadb88ebb3446 Mon Sep 17 00:00:00 2001 From: Vera Lewis Date: Sun, 31 Mar 2024 01:32:50 -0500 Subject: [PATCH] Added re-encryption capability to init function and changed create to use stored gpg recipients --- vns | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 78 insertions(+), 17 deletions(-) diff --git a/vns b/vns index c2111d4..cfb0299 100644 --- a/vns +++ b/vns @@ -65,15 +65,68 @@ vns_sanityCheck () { vns_init () { - # initStore + # init (GPG recipients...) # Prepares $VNS_STORE for use - if [ -d "$VNS_STORE/.vns_git" ]; then - vns_raise "Store already initialized" 2; - fi - - git init "$VNS_STORE" + if [ "$#" -lt 1 ]; then + vns_raise "No GPG Recipients Specified" 3 + elif [ -d "$VNS_STORE/.git" ]; then + + # Reset list of GPG recipients + echo -n '' > "${VNS_STORE}/.gpg-id" + + vns_reencrypt "$VNS_STORE" "$@" + + else + # Create a new VNS Store + git init "$VNS_STORE" + + # Make initial commit + vns_git add .gpg-id + vns_git commit -am "Initialized VNS Store" + + fi + + # 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 +} + +vns_reencrypt () { + # reencrypt (GPG recipients...) + + # Remove script name from arguments list + shift + + # Construct list of arguments from provided recipients + local -a GPG_RECIPS; + for recipient in "$@"; do + GPG_RECIPS=( "${GPG_RECIPS[@]}" "-r" "$recipient" ) + done + + # Find all notes and re-encrypt them + find "${VNS_STORE}" -name "*.gpg" | while read -r FILE; do\ + gpg --batch -d "$FILE" 2>/dev/null | gpg --batch "${GPG_RECIPS[@]}" -e -o "${FILE}_new".gpg >/dev/null 2>&1 + mv "${FILE}_new.gpg" "$FILE"; + done + + vns_report "Re-encrypted existing notes with $*" + vns_git commit -am "Re-encrypted existing notes with ${GPG_RECIPS[*]}" >/dev/null 2>&1 +} + +vns_gpgid () { + + # gpgid + # Echo 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 (){ @@ -82,9 +135,9 @@ vns_printHelp (){ # Prints help information to stdout printf "%s" "usage: vns [-cedlp] " - printf "\\n%s" " vns -I" + printf "\\n%s" " vns -I GPG_RECIPIENT (GPG_RECIPIENTS...)" printf "\\n%s" " vns -h" - printf "\\n%s" " vns vns_git ..." + printf "\\n%s" " vns git ..." printf "\\n" printf "\\n%s" " -c : Create note" printf "\\n%s" " -d : Delete note(s)" @@ -151,6 +204,18 @@ vns_rm () { } +vns_header () { + # header (title) + # Print note title and date in Markdown format + + if [ "$#" -lt 1 ]; then + vns_raise "header: no note title provided" 4 + else + echo "# $1" + printf "## %s" "$(date)" + fi +} + vns_create () { # create (note) @@ -169,17 +234,13 @@ vns_create () { # If the note belongs to a new notebook, create the notebook if [ ! -d "$(dirname "$1")" ]; then mkdir -p "$VNS_STORE/$(dirname "$1")"; fi + + # Create empty note + vns_header "$(basename "$1")" | gpg --batch "$(vns_gpgid)" --encrypt -o "$VNS_STORE/$1.gpg" - # Create-Edit the note + # Edit the note vim "$VNS_STORE/$1.gpg" - # Warn the user if the note failed to encrypt - if ! file "$VNS_STORE/$1.gpg" | grep "PGP" -qs; then - printf "$VNS_RED_COLOR!$VNS_RESET_COLOR - %s\\n %s\\n"\ - "The created note was not encrypted."\ - "Check your vim-gnupg setup." - fi - # Update vns_git vns_git add "$VNS_STORE/$1.gpg" > /dev/null vns_git commit -m "Added $1" > /dev/null @@ -354,7 +415,7 @@ vns_git () { vns () { # Bypass sanity check if told to initialize store - if [ "$*" != "-I" ]; then + if [ "$1" != "-I" ]; then vns_sanityCheck; fi