Added sleep 1 sec after deleting notes to allow the user to read the
[bash-notes.git] / SOURCE / CORE / core-remove.sh
1 function rmnote() {
2 # remove eventually existing temp DB file
3 if [[ -f $TMPDB ]]; then
4 rm $TMPDB
5 fi
6
7 NOTE=$1
8 if [ "all" == "$NOTE" ]; then
9 echo "You're going to delete all notes."
10 read -r -p "Do you wish to continue? (y/N) " ANSWER
11 case $ANSWER in
12 y|Y )
13 # shellcheck disable=SC2086
14 $JQ 'del(.notes[])' $DB > $TMPDB
15 # shellcheck disable=SC2086
16 mv $TMPDB $DB
17 # shellcheck disable=SC2086
18 rm $NOTESDIR/*
19 echo "Deleted all notes"
20 ;;
21 * )
22 echo "Aborting, no notes were deleted."
23 exit 1
24 ;;
25 esac
26 else
27 # shellcheck disable=SC2155
28 local OK=$(check_noteID "$NOTE")
29 if [ ! "$OK" ]; then
30 echo "invalid note \"$NOTE\""
31 echo "Use the note ID that you can fetch after listing your notes"
32 sleep 1
33 exit 1
34 fi
35
36 # shellcheck disable=SC2016,SC2086
37 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
38 # shellcheck disable=SC2016,SC2086
39 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
40 if [ "$TITLE" ]; then
41 # shellcheck disable=SC2016,SC2086
42 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
43 # shellcheck disable=SC2086
44 mv $TMPDB $DB
45 rm $NOTESDIR/$FILE
46 echo "Deleted note $TITLE"
47 sleep 1
48 exit
49 else
50 echo "note not found"
51 sleep 1
52 exit 1
53 fi
54 fi
55 }