Added re-encryption capability to init function and changed create to use stored gpg recipients

This commit is contained in:
Vera Lewis
2024-03-31 01:32:50 -05:00
parent 3d933a6831
commit 0432e9d86f

91
vns
View File

@@ -65,15 +65,68 @@ vns_sanityCheck () {
vns_init () { vns_init () {
# initStore # init (GPG recipients...)
# Prepares $VNS_STORE for use # Prepares $VNS_STORE for use
if [ -d "$VNS_STORE/.vns_git" ]; then if [ "$#" -lt 1 ]; then
vns_raise "Store already initialized" 2; vns_raise "No GPG Recipients Specified" 3
fi
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" 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 (){ vns_printHelp (){
@@ -82,9 +135,9 @@ vns_printHelp (){
# Prints help information to stdout # Prints help information to stdout
printf "%s" "usage: vns [-cedlp] <notebook/section/name>" printf "%s" "usage: vns [-cedlp] <notebook/section/name>"
printf "\\n%s" " vns -I" printf "\\n%s" " vns -I GPG_RECIPIENT (GPG_RECIPIENTS...)"
printf "\\n%s" " vns -h" printf "\\n%s" " vns -h"
printf "\\n%s" " vns vns_git ..." printf "\\n%s" " vns git ..."
printf "\\n" printf "\\n"
printf "\\n%s" " -c : Create note" printf "\\n%s" " -c : Create note"
printf "\\n%s" " -d : Delete note(s)" 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 () { vns_create () {
# create (note) # create (note)
@@ -170,15 +235,11 @@ vns_create () {
# If the note belongs to a new notebook, create the notebook # If the note belongs to a new notebook, create the notebook
if [ ! -d "$(dirname "$1")" ]; then mkdir -p "$VNS_STORE/$(dirname "$1")"; fi if [ ! -d "$(dirname "$1")" ]; then mkdir -p "$VNS_STORE/$(dirname "$1")"; fi
# Create-Edit the note # Create empty note
vim "$VNS_STORE/$1.gpg" vns_header "$(basename "$1")" | gpg --batch "$(vns_gpgid)" --encrypt -o "$VNS_STORE/$1.gpg"
# Warn the user if the note failed to encrypt # Edit the note
if ! file "$VNS_STORE/$1.gpg" | grep "PGP" -qs; then vim "$VNS_STORE/$1.gpg"
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 # Update vns_git
vns_git add "$VNS_STORE/$1.gpg" > /dev/null vns_git add "$VNS_STORE/$1.gpg" > /dev/null
@@ -354,7 +415,7 @@ vns_git () {
vns () { vns () {
# Bypass sanity check if told to initialize store # Bypass sanity check if told to initialize store
if [ "$*" != "-I" ]; then if [ "$1" != "-I" ]; then
vns_sanityCheck; vns_sanityCheck;
fi fi