Version 2b; adds "Vim Mode"

This commit is contained in:
Jon-William Lewis
2017-05-09 07:37:53 -05:00
parent fce26bf573
commit 7577a682ae
2 changed files with 73 additions and 39 deletions

105
sns.sh
View File

@@ -1,11 +1,10 @@
#!/bin/bash
XL_PRODUCT="Simple Note System"
XL_VER="v2a"
XL_VER="v2b"
# Environment Constants
readonly SNS_STORE="$HOME/.local/sns"
readonly SNS_KEYFILE="$SNS_STORE/.pubkey"
readonly SNS_DEPS=("gpg2" "tree" "git")
readonly IFS=$'\n\t'
set -euo pipefail
@@ -114,44 +113,7 @@ function sns_list(){
# Print the tree
tree -C --noreport --prune "$(echo "$@" | awk '{print $1}')"
}
function sns_create(){
# Make sure the note doesn't already exist
if [ -r "$SNS_STORE/$(echo "$@" | awk '{print $1}')" ]; then
sns_printError "Note already exists."
return
fi
# Print the standard header to a temporary note
sns_NoteHeader > /tmp/"$SNS_SID"
# Edit the new note
sns_edit "$(echo "$@" | awk '{print $1}')"
}
function sns_edit(){
# Make the function more readable
local readonly SNS_NOTE="$SNS_STORE/$(echo "$@" | awk '{print $1}')"
# Test if edit was called from create
if [ ! -r /tmp/"$SNS_SID" ]; then gpg2 -d -o /tmp/"$SNS_SID" "$SNS_NOTE"; fi
# Edit the note
vim /tmp/"$SNS_SID"
# Make sure the notebook/section exists
if [ ! -d "$(dirname "$SNS_NOTE")" ]; then mkdir -p "$(dirname "$SNS_NOTE")"; fi
# If the note previously existed, make a backup.
if [ -r "$SNS_NOTE" ]; then mv "$SNS_NOTE" "$SNS_NOTE.bk"; fi
# Re-encrypt it to the store
gpg2 -r "$SNS_PUBKEY" -o "$SNS_NOTE" -e /tmp/"$SNS_SID"
# If all went well, remove the backup
if [ -r "$SNS_NOTE" ]; then rm "$SNS_NOTE.bk"; fi
}
function sns_print(){
gpg2 -d "$SNS_STORE/$(echo "$@" | awk '{print $1}')"
}
function sns_rm(){
rm -f "$SNS_STORE/$(echo "$@" | awk '{print $1}')"
}
@@ -209,6 +171,71 @@ function sns_argParse(){
printf "%s\n%s\n" "$XL_PRODUCT" "$XL_VER"
printf "\n"
#Determine Run Mode
if [ -r "$SNS_STORE/.vim" ]; then
readonly SNS_DEPS=("vim" "tree" "git")
function sns_create(){
if [ -r "$SNS_STORE/$(echo "$@" | awk '{print $1}')" ]; then
sns_printError "Note already exists."
return
fi
vim -x "$SNS_STORE/$(echo "$@" | awk '{print $1}')"
}
function sns_edit(){
vim "$SNS_STORE/$(echo "$@" | awk '{print $1}')"
}
function sns_print(){
sns_printError "Printing of Vim-encrypted notes is not supported at this time."
}
else
readonly SNS_DEPS=("gpg2" "tree" "git")
function sns_create(){
# Make sure the note doesn't already exist
if [ -r "$SNS_STORE/$(echo "$@" | awk '{print $1}')" ]; then
sns_printError "Note already exists."
return
fi
# Print the standard header to a temporary note
sns_NoteHeader > /tmp/"$SNS_SID"
# Edit the new note
sns_edit "$(echo "$@" | awk '{print $1}')"
}
function sns_edit(){
# Make the function more readable
local readonly SNS_NOTE="$SNS_STORE/$(echo "$@" | awk '{print $1}')"
# Test if edit was called from create
if [ ! -r /tmp/"$SNS_SID" ]; then gpg2 -d -o /tmp/"$SNS_SID" "$SNS_NOTE"; fi
# Edit the note
vim /tmp/"$SNS_SID"
# Make sure the notebook/section exists
if [ ! -d "$(dirname "$SNS_NOTE")" ]; then mkdir -p "$(dirname "$SNS_NOTE")"; fi
# If the note previously existed, make a backup.
if [ -r "$SNS_NOTE" ]; then mv "$SNS_NOTE" "$SNS_NOTE.bk"; fi
# Re-encrypt it to the store
gpg2 -r "$SNS_PUBKEY" -o "$SNS_NOTE" -e /tmp/"$SNS_SID"
# If all went well, remove the backup
if [ -r "$SNS_NOTE" ]; then rm "$SNS_NOTE.bk"; fi
}
function sns_print(){
gpg2 -d "$SNS_STORE/$(echo "$@" | awk '{print $1}')"
}
fi
sns_argParse "$@"
sns_sanityCheck
if [ "$SNS_EXIT" -eq 0 ]; then