Major work done toward vns 1.1

This commit is contained in:
Jon Lewis
2018-07-15 02:04:54 -05:00
parent 5c4a1f8ebc
commit 6077825431

91
vns
View File

@@ -79,6 +79,10 @@ vns_init () {
# initStore
# Prepares $VNS_STORE for use
if [ -d "$VNS_STORE/.git" ]; then
vns_raise "Store already initialized" 2;
fi
mkdir -p "$VNS_STORE"
cd "$VNS_STORE" || exit
git init >/dev/null
@@ -91,17 +95,19 @@ vns_printHelp (){
# Prints help information to stdout
printf "%s" "usage: vns [-cedlp] <notebook/section/name>"
printf "\\n%s" " vns -i"
printf "\\n%s" " vns -I"
printf "\\n%s" " vns -h"
printf "\\n%s" " vns git ..."
printf "\\n"
printf "\\n%s" " -c | --create : Create note"
printf "\\n%s" " -d | --delete : Delete note"
printf "\\n%s" " -e | --edit : Open note for editing"
printf "\\n%s" " -h | --help : Display this message"
printf "\\n%s" " -i | --init : Initialize note store"
printf "\\n%s" " -l | --list : List all notes in <notebook>"
printf "\\n%s" " -p | --print : Print note to console"
printf "\\n%s" " -c : Create note"
printf "\\n%s" " -d : Delete note"
printf "\\n%s" " -e : Open note for editing"
printf "\\n%s" " -h : Display this message"
printf "\\n%s" " -i : import file as note"
printf "\\n%s" " -I : Initialize note store"
printf "\\n%s" " -l : List all notes in <notebook>"
printf "\\n%s" " -m : Merge two or more notes"
printf "\\n%s" " -p : Print note to console"
printf "\\n\\n"
}
@@ -208,6 +214,42 @@ vns_edit () {
git commit -m "Edited $1" > /dev/null
}
vns_import () {
# import (src) (dest)
# If src is the path to a file, and dest is a valid note name,
# import src dest copies an encrypted version of src to the note store
if [ -z "${*:1:1}" ]; then
vns_raise "No source file specified" 90
elif [ -z "${*:2:2}" ]; then
vns_raise "No destination name specified" 91
elif [ ! -f "$1" ]; then
vns_raise "Cannot read $1" 92
elif [ -f "$VNS_STORE/$2" ]; then
printf "%s" "Note already exists. Overwrite? (Y/N) "
local response; read -r response
case "$response" in
Yy)
rm "$VNS_STORE/$2";;
*)
vns_raise "User declined overwrite" 0;;
esac
fi
printf "%s:" "Specify GPG Public Key: "
local gpg_key; read -r gpg_key
gpg2 -r "$gpg_key" -o "$VNS_STORE/$2.gpg" -e "$1"
git add "$VNS_STORE/$2.gpg"
git commit -m "Imported file $1 as $2"
}
vns_print () {
# print (note)
@@ -241,6 +283,7 @@ vns_merge () {
# merge (output) (notes[]) concatenates notes[] into output.
if [ -z "${*:1}" ]; then vns_raise "No output specified" 81; fi
if [ -z "${*:2}" ]; then vns_raise "No inputs specified" 84; fi
if [ -r "$VNS_STORE/$1.gpg" ]; then vns_raise "Output file already exists." 82; fi
# Make output file
@@ -267,6 +310,31 @@ vns_merge () {
git commit -m "merged files ${*:2} into $1"
}
vns_duplicate () {
if [ -z "${*:1:1}" ]; then
vns_raise "No source note specified" 100
elif [ -z "${*:2:2}" ]; then
vns_raise "No destination name specified" 101
elif [ ! -f "$VNS_STORE/$1" ]; then
vns_raise "Cannot read $1" 102
elif [ -f "$VNS_STORE/$2" ]; then
printf "%s" "Note already exists. Overwrite? (Y/N) "
local response; read -r response
case "$response" in
Yy)
rm "$VNS_STORE/$2";;
*)
vns_raise "User declined overwrite" 0;;
esac
fi
cp "$VNS_STORE/$1.gpg" "$VNS_STORE/$2.gpg"
git add "$VNS_STORE/$2"
}
vns () {
# Bypass sanity check if told to initialize store
@@ -283,9 +351,10 @@ vns () {
# List of valid arguments
declare -A -r ACTIONS=( ["-c"]="vns_create" ["-d"]="vns_rm"\
["-e"]="vns_edit" ["-l"]="vns_list"\
["-h"]="vns_printHelp" ["-i"]="vns_init"\
["-m"]="vns_merge" ["-p"]="vns_print"\
["-r"]="vns_mv" ["git"]="git" )
["-h"]="vns_printHelp" ["-I"]="vns_init"\
["-i"]="vns_import" ["-m"]="vns_merge"\
["-p"]="vns_print" ["-r"]="vns_mv"\
["-u"]="vns_duplicate" ["git"]="git" )
# If given an invalid argument, inform the user and exit on code 10
# Otherwise, perform the corresponding action