summaryrefslogtreecommitdiffstats
path: root/SOURCE/CORE/core-remove.sh
blob: 24c7290e7bdac449f8b69e342fef743d2607e9e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
rmnote() {
	# remove eventually existing temp DB file
	if [[ -f $TMPDB ]]; then
		rm $TMPDB
	fi

	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 )
				# shellcheck disable=SC2086
				$JQ 'del(.notes[])' $DB > $TMPDB
				# shellcheck disable=SC2086
				mv $TMPDB $DB
				# shellcheck disable=SC2086
				rm $NOTESDIR/*
				gitremove "all"
				echo "Deleted all notes"
				;;
			* )
				echo "Aborting, no notes were deleted."
				exit 1
				;;
		esac
	else
		# shellcheck disable=SC2155
		local OK=$(check_noteID "$NOTE")
		if [ ! "$OK" ]; then
			echo "invalid note \"$NOTE\""
			echo "Use the note ID that you can fetch after listing your notes"
			sleep 1
			exit 1
		fi

		# shellcheck disable=SC2016,SC2086
		TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
		# shellcheck disable=SC2016,SC2086
		FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
		if [ "$TITLE" ]; then
			# shellcheck disable=SC2016,SC2086
			$JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
			# shellcheck disable=SC2086
			mv $TMPDB $DB
			rm $NOTESDIR/$FILE
			gitremove $OK $FILE
			echo "Deleted note $TITLE"
			sleep 1
			exit
		else
			 echo "note not found"
			 sleep 1
			 exit 1
		fi
	fi
}