b1e2a018 |
1 | rmnote() { |
fb711183 |
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/* |
d1f115c1 |
19 | gitremove "all" |
fb711183 |
20 | echo "Deleted all notes" |
21 | ;; |
22 | * ) |
23 | echo "Aborting, no notes were deleted." |
24 | exit 1 |
25 | ;; |
26 | esac |
27 | else |
28 | # shellcheck disable=SC2155 |
29 | local OK=$(check_noteID "$NOTE") |
30 | if [ ! "$OK" ]; then |
31 | echo "invalid note \"$NOTE\"" |
32 | echo "Use the note ID that you can fetch after listing your notes" |
f1343f20 |
33 | sleep 1 |
fb711183 |
34 | exit 1 |
35 | fi |
36 | |
37 | # shellcheck disable=SC2016,SC2086 |
38 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
39 | # shellcheck disable=SC2016,SC2086 |
40 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
41 | if [ "$TITLE" ]; then |
42 | # shellcheck disable=SC2016,SC2086 |
43 | $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB |
44 | # shellcheck disable=SC2086 |
45 | mv $TMPDB $DB |
46 | rm $NOTESDIR/$FILE |
d1f115c1 |
47 | gitremove $OK $FILE |
fb711183 |
48 | echo "Deleted note $TITLE" |
f1343f20 |
49 | sleep 1 |
50 | exit |
fb711183 |
51 | else |
52 | echo "note not found" |
f1343f20 |
53 | sleep 1 |
fb711183 |
54 | exit 1 |
55 | fi |
56 | fi |
57 | } |