From 026502da85622178ace9dedf26c2addc60a0d897 Mon Sep 17 00:00:00 2001 From: danix Date: Sat, 25 Mar 2023 11:54:41 +0100 Subject: [PATCH] implemented routine --- README.md | 4 ++-- notes.sh | 65 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 49dfabc..992e7a8 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ here's all the functions that I'm planning to implement: -h | --help : This help text -p | --plain : Output is in plain text (without this option the output is colored) -l | --list : List existing notes --a | --add : Add new note +-a | --add "<title>" : Add new note -m | --modify <note> : Modify note --d | --delete <note> : Delete note +-d | --delete [<note> | all] : Delete note -v | --version : Print version --userconf : Export User config file ``` diff --git a/notes.sh b/notes.sh index 87bb430..14fa059 100644 --- a/notes.sh +++ b/notes.sh @@ -97,15 +97,20 @@ __NOWCONF__ echo " (without this option the output is formatted)" echo " (this option must precede all others)" echo " -l | --list : List existing notes" - echo " -a | --add <title> : Add new note" - echo " -m | --modify <note> : Modify note" - echo " -d | --delete <note> : Delete note" + echo " -a | --add [\"<title>\"] : Add new note" + echo " -m | --modify [<note>] : Modify note" + echo " -d | --delete [<note> | all] : Delete single note or all notes at once" echo " -v | --version : Print version" echo " --userconf : Export User config file" echo "" } function addnote() { + # remove eventually existing temp DB file + if [[ -f $TMPDB ]]; then + rm $TMPDB + fi + NOTETITLE="$1" echo "adding new note - \"$NOTETITLE\"" LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) @@ -159,23 +164,45 @@ function editnote() { } function rmnote() { - NOTE=$1 - local OK=$(check_noteID $NOTE) - if [ ! $OK ]; then - echo "invalid note \"$NOTE\"" - exit 1 + # remove eventually existing temp DB file + if [[ -f $TMPDB ]]; then + rm $TMPDB fi - TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) - FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) - if [ "$TITLE" ]; then - $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB - mv $TMPDB $DB - rm $NOTESDIR/$FILE - echo "Deleted note $TITLE" + NOTE=$1 + if [ "all" == $NOTE ]; then + echo "You're going to delete all notes." + read -r -p "Do you wish to continue? (y/N) " ANSWER + case $ANSWER in + y|Y ) + $JQ 'del(.notes[])' $DB > $TMPDB + mv $TMPDB $DB + rm $NOTESDIR/* + echo "Deleted all notes" + ;; + * ) + echo "Aborting, no notes were deleted." + exit 1 + ;; + esac else - echo "note not found" - exit 1 + local OK=$(check_noteID $NOTE) + if [ ! $OK ]; then + echo "invalid note \"$NOTE\"" + exit 1 + fi + + TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) + FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) + if [ "$TITLE" ]; then + $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB + mv $TMPDB $DB + rm $NOTESDIR/$FILE + echo "Deleted note $TITLE" + else + echo "note not found" + exit 1 + fi fi } @@ -294,8 +321,8 @@ while true; do exit ;; -- ) - helptext - exit + shift + break ;; * ) break -- 2.20.1