Added Query function, cleaned up arg parser

This commit is contained in:
Vera Lewis
2024-06-28 18:43:42 -05:00
parent 6ff62ebd19
commit 6a7655326b

51
vns
View File

@@ -143,7 +143,7 @@ vns_printHelp (){
# printHelp
# Prints help information to stdout
printf "%s" "usage: vns [-cedlp] <notebook/section/name>"
printf "%s" "usage: vns [-cedlpqr] <notebook/section/name>"
printf "\\n%s" " vns -I GPG_RECIPIENT (GPG_RECIPIENTS...)"
printf "\\n%s" " vns -h"
printf "\\n%s" " vns git ..."
@@ -157,6 +157,7 @@ vns_printHelp (){
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%s" " -q : Query notes for expression"
printf "\\n%s" " -r : Rename/move a note"
printf "\\n\\n"
@@ -430,6 +431,35 @@ vns_duplicate () {
vns_git add "$VNS_STORE/$2.gpg"
}
vns_query () {
# query EXPR
# Iterate through notes and find all instances of regular expression EXPR
if [ "$#" -lt 1 ]; then
vns_report "usage: $(basename "$0") -q EXPR"
return
fi
local -r VNS_ULNE="$(tput setaf smul)"
local -r VNS_="$(tput setaf rmul)"
# For each note, grep for QUERY
find "$VNS_STORE" -name "*.gpg" -not -name ".*" | while read -r note; do
# Remove VNS_STORE path and file extension for reporting
note_name="$(sed -e "s#$VNS_STORE/##" -e "s/.gpg//" -e "s#:#\\:#g" <<< "$note")"
# Decrypt note and perform the actual query
local query="$(gpg -d "$note" 2>/dev/null | grep -C 1 --color=always -nE "$1" )"
# Report only matches
if [ -n "${query:-}" ]; then
printf "$(tput smul)%s$(tput rmul)\n" "$note_name"
printf "%s\n" "$query"
fi
done
}
vns_git () {
# Simple passthrough for executing git commands in the VNS store
git -C "$VNS_STORE" "$@"
@@ -441,53 +471,46 @@ vns () {
vns_sanityCheck;
fi
case "$1" in
"-c")
primary_arg="$1"
shift
case "$primary_arg" in
"-c")
vns_create "$@"
;;
"-d")
shift
vns_rm "$@"
;;
"-e")
shift
vns_edit "$@"
;;
"-l")
shift
vns_list "$@"
;;
"-h")
shift
vns_printHelp "$@"
;;
"-I")
shift
vns_init "$@"
;;
"-i")
shift
vns_import "$@"
;;
"-m")
shift
vns_merge "$@"
;;
"-p")
shift
vns_print "$@"
;;
"-q")
vns_query "$@"
;;
"-r")
shift
vns_mv "$@"
;;
"-u")
shift
vns_duplicate "$@"
;;
"git")
shift
vns_git "$@"
;;
*)