if PLAIN option is enabled, git pull doesn't emit output.
[bash-notes.git] / notes.sh
CommitLineData
a4aaf855 1#! /bin/bash
2
6c152f7e 3# bash-notes © 2023 by danix is licensed under CC BY-NC 4.0.
4# To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/
5
cb8fcb2f 6# to debug the script run it like:
7# DEBUG=true notes.sh ...
8# and check /tmp/debug_bash-notes.log
9if [[ $DEBUG == true ]]; then
10 exec 5> /tmp/debug_bash-notes.log
11 BASH_XTRACEFD="5"
12 PS4='$LINENO: '
13 set -x
14fi
a4aaf855 15
16PID=$$
ad818a9d 17BASENAME=$( basename "$0" )
18NOW=$(date +%s)
19
4cbcb39e 20VERSION="0.4git"
ad818a9d 21DBVERSION=${VERSION}_${NOW}
53f2ed57 22
d80ac20a 23set_defaults() {
24# Binaries to use
cb8fcb2f 25JQ=${JQ:-/usr/bin/jq}
a4aaf855 26EDITOR=${EDITOR:-/usr/bin/vim}
53f2ed57 27TERMINAL=${TERMINAL:-/usr/bin/alacritty}
4cbcb39e 28# Git binary only used if $USEGIT is true - See below
29GIT=${GIT:-/usr/bin/git}
b648c006 30# add options for your terminal. Remember to add the last option to execute
31# your editor program, otherwise the script will fail.
32# see example in the addnote function
e3670e83 33TERM_OPTS="--class notes --title notes -e "
ad818a9d 34# Setting PAGER here overrides whatever is set in your default shell
35# comment this option to use your default pager if set in your shell.
36PAGER=${PAGER:-/usr/bin/more}
53f2ed57 37
cb8fcb2f 38# set this to true to have output in plain text
39# or use the -p option on the command line before every other option
40PLAIN=false
d80ac20a 41# base directory for program files
53f2ed57 42BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
d80ac20a 43# notes database in json format
a4aaf855 44DB=${BASEDIR}/db.json
d80ac20a 45# directory containing the actual notes
a4aaf855 46NOTESDIR=${BASEDIR}/notes
53f2ed57 47
4cbcb39e 48### GIT SUPPORT
49
50# If you want to store your notes in a git repository set this to true
51USEGIT=true
52# Address of your remote repository
53GITREMOTE=${GITREMOTE:-""}
cf6d89bc 54# How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour)
1f4d7742 55GITSYNCDELAY=${GITSYNCDELAY:-"3600"}
bba0734f 56# The name of this client. Defaults to the output of hostname
57GITCLIENT=${GITCLIENT:-""}
4cbcb39e 58
d80ac20a 59} # end set_defaults, do not change this line.
60
61set_defaults
62
63# Do not edit below this point
64RCFILE=${RCFILE:-~/.config/bash-notes.rc}
53f2ed57 65TMPDB=/tmp/db.json
a4aaf855 66
b9f21021 67if [ ! -x "$JQ" ]; then
a4aaf855 68 echo "jq not found in your PATH"
69 echo "install jq to continue"
70 exit 1
71fi
72
53f2ed57 73# IMPORT USER DEFINED OPTIONS IF ANY
74if [[ -f $RCFILE ]]; then
b9f21021 75 # shellcheck disable=SC1090
76 source "$RCFILE"
53f2ed57 77fi
78
a4aaf855 79# We prevent the program from running more than one instance:
b9f21021 80PIDFILE=/var/tmp/$(basename "$0" .sh).pid
a4aaf855 81
82# Make sure the PID file is removed when we kill the process
83trap 'rm -f $PIDFILE; exit 1' TERM INT
84
85if [[ -r $PIDFILE ]]; then
86 # PIDFILE exists, so I guess there's already an instance running
87 # let's kill it and run again
b9f21021 88 # shellcheck disable=SC2046,SC2086
a4aaf855 89 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
90 # should already be deleted by trap, but just to be sure
b9f21021 91 rm "$PIDFILE"
a4aaf855 92fi
93
94# create PIDFILE
b9f21021 95echo $PID > "$PIDFILE"
a4aaf855 96
fb711183 97# Export config to file
cf6d89bc 98export_config() {
fb711183 99 if [ -r ${RCFILE} ]; then
100 echo "Backing up current '${RCFILE}'...."
101 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
102 fi
103 echo "Writing '${RCFILE}'...."
104 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
105 | grep -v set_defaults \
106 | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
107 > ${RCFILE}
108 if [ -r ${RCFILE} ]; then
109 echo "Taking no further action."
110 exit 0
111 else
112 echo "Could not write '${RCFILE}'...!"
113 exit 1
114 fi
115}
116
117# we should expand on this function to add a sample note and explain a little bit
118# how the program works.
cf6d89bc 119firstrun() {
fb711183 120 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
121
122 clear
123 echo "${BASENAME} configuration:
124
125base directory: ${BASEDIR}/
126notes archive: ${NOTESDIR}/
127notes database: ${DB}
128rc file: $RC
129text editor: ${EDITOR}
130terminal: ${TERMINAL}
131jq executable: ${JQ}
132"
133
9eb02251 134 echo "Now I'll create the needed files and directories."
fb711183 135 read -r -p "Do you wish to continue? (y/N) " ANSWER
136 case $ANSWER in
137 y|Y )
138 mkdir -p $NOTESDIR
139 cat << __EOL__ > ${DB}
140{
141 "params": {
142 "version": "${VERSION}",
143 "dbversion": "${DBVERSION}"
144 },
87a368fe 145 "git": {
146 "lastpull": ""
147 },
fb711183 148 "notes": []
149}
150__EOL__
151 echo; echo "All done, you can now write your first note."
152 ;;
153 * )
154 echo "No changes made. Exiting"
155 exit
156 ;;
157 esac
158}
159
160# check for notes dir existance and create it in case it doesn't exists
161if [[ ! -d $NOTESDIR ]]; then
162 # we don't have a directory. FIRST RUN?
163 firstrun
164fi
61c91990 165# check if input is a number, returns false or the number itself
cf6d89bc 166check_noteID() {
61c91990 167 IN=$1
168 case $IN in
169 ''|*[!0-9]*)
170 return 1
171 ;;
172 *)
b9f21021 173 echo "$IN"
61c91990 174 ;;
175 esac
176}
177
cf6d89bc 178helptext() {
d80ac20a 179 echo "Usage:"
efa3e607 180 echo " $0 [PARAMS] [note ID]..."
d80ac20a 181 echo ""
e3670e83 182 echo "${BASENAME} parameters are:"
9eb02251 183 echo -e " -h | --help\t\t\t: This help text"
184 echo -e " -p | --plain\t\t\t: Output is in plain text"
185 echo -e "\t\t\t\t (without this option the output is formatted)"
186 echo -e "\t\t\t\t (this option must precede all others)"
187 echo -e " -l | --list\t\t\t: List existing notes"
3951cc3d 188 echo -e " -a | --add=[\"<title>\"]\t: Add new note"
189 echo -e " -e | --edit=[<note>]\t\t: Edit note"
190 echo -e " -d | --delete=[<note> | all] : Delete single note or all notes at once"
191 echo -e " -s | --show=[<note>]\t\t: Display note using your favourite PAGER"
192 echo -e " -r | --restore=[<dir>]\t: Restore a previous backup from dir"
9eb02251 193 echo -e " -v | --version\t\t: Print version"
194 echo -e " --userconf\t\t\t: Export User config file"
195 echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder"
e3670e83 196 echo ""
efa3e607 197 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
a4aaf855 198}
9eb02251 199
cf6d89bc 200configtext() {
1f4d7742 201 [ $USEGIT ] && GITUSE="enabled" || GITUSE="disabled"
202 clear
203 echo -e "${BASENAME} configuration is:"
204
205 echo -e "base directory: ${BASEDIR}/"
206 echo -e "notes archive: ${NOTESDIR}/"
207 echo -e "notes database: ${DB}"
208 echo -e "rc file: $RCFILE"
209 echo -e "debug file: /tmp/debug_bash-note.log"
210
211 echo -e "text editor: ${EDITOR}"
212 echo -e "terminal: ${TERMINAL}"
213 echo -e "jq executable: ${JQ}"
214 echo -e "PAGER: ${PAGER}"
215
216 echo -e "GIT use: ${GITUSE}"
217 echo -e "GIT remote: ${GITREMOTE}"
218 echo -e "GIT sync delay: ${GITSYNCDELAY}"
3951cc3d 219}
220
6a193095 221# this function returns a random 2 words title
cf6d89bc 222random_title() {
6a193095 223 # Constants
224 X=0
225 DICT=/usr/share/dict/words
226 OUTPUT=""
227
228 # total number of non-random words available
229 COUNT=$(cat $DICT | wc -l)
230
231 # while loop to generate random words
232 while [ "$X" -lt 2 ]
233 do
234 RAND=$(od -N3 -An -i /dev/urandom | awk -v f=0 -v r="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}')
235 OUTPUT+="$(sed `echo $RAND`"q;d" $DICT)"
236 (("X = X + 1"))
237 [[ $X -eq 1 ]] && OUTPUT+=" "
238 done
239
240 echo $OUTPUT
241}
242
bba0734f 243# check if GITCLIENT has been set or set it to the output of hostname
1f4d7742 244if [ -z "$GITCLIENT" ]; then
bba0734f 245 GITCLIENT=$( hostname )
246fi
4cbcb39e 247# returns true if the argument provided directory is a git repository
248is_git_repo() {
249 DIR=$1
250 if [[ -d $DIR ]]; then
251 cd $DIR
252 if git rev-parse 2>/dev/null; then
253 true
254 else
255 false
256 fi
257 fi
258}
259
260# sync local repository to remote
1f4d7742 261# accepts -f parameter to skip last sync check
4cbcb39e 262gitsync() {
1f4d7742 263 FORCE=$1
bba0734f 264 if [[ $USEGIT && -n $GITREMOTE ]]; then
bba0734f 265 [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\""
1f4d7742 266 NOWSYNC=$(date +%s)
267 if [[ $FORCE == "-f" ]]; then
bba0734f 268 $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
269 mv $TMPDB $DB
270 cd $BASEDIR
cf9f797b 271 [ $PLAIN == false ] && $GIT pull || $GIT pull -q
bba0734f 272 else
1f4d7742 273 # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time.
274 LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB")
275 SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} ))
276 if (( $SYNCDIFF > $GITSYNCDELAY )); then
277 #more than our delay time has passed. We can sync again.
278 $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
279 mv $TMPDB $DB
280 cd $BASEDIR
cf9f797b 281 [ $PLAIN == false ] && $GIT pull || $GIT pull -q
1f4d7742 282 else
283 # Last synced less than $GITSYNCDELAY seconds ago. We shall wait
284 [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait"
285 fi
bba0734f 286 fi
287 else
288 # no git, so we just keep going
289 true
290 fi
291}
292
293# add note to git and push it to remote
294gitadd() {
295 if [[ $USEGIT && -n $GITREMOTE ]]; then
296 [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\""
cf6d89bc 297 cd $BASEDIR
bba0734f 298 $GIT add .
299 $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}"
300 $GIT push origin master
cf6d89bc 301 else
bba0734f 302 # no git, so we just keep going
303 true
cf6d89bc 304 fi
4cbcb39e 305}
306
d1f115c1 307# edited note added to git and pushed it to remote
308gitedit() {
309 if [[ $USEGIT && -n $GITREMOTE ]]; then
310 [ $PLAIN == false ] && echo "Editing note on remote \"$GITREMOTE\""
311 cd $BASEDIR
312 $GIT add .
313 $GIT commit -m "$(basename $0) - ${GITCLIENT} note edited."
314 $GIT push origin master
315 else
316 # no git, so we just keep going
317 true
318 fi
319}
320
321# add note to git and push it to remote
322gitremove() {
323 NOTE=$1
324 FILE=$2
325 if [[ $USEGIT && -n $GITREMOTE ]]; then
326 [ $PLAIN == false ] && echo "Deleting notes from remote \"$GITREMOTE\""
327 if [ "all" == $NOTE ];then
328 echo "Deleting all notes"
329 cd $BASEDIR
330 $GIT rm notes/*
331 $GIT commit -m "$(basename $0) - ${GITCLIENT} removing all notes."
332 $GIT push origin master
333 else
334 echo "Deleting note ID ${NOTE}"
335 local OK=$(check_noteID "$NOTE")
336 cd $BASEDIR
337 $GIT rm notes/${FILE}
338 $GIT add .
339 $GIT commit -m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}."
340 $GIT push origin master
341 fi
342 else
343 # no git, so we just keep going
344 true
345 fi
346}
347
4cbcb39e 348# check for USEGIT and subsequent variables
349if [[ $USEGIT && -n $GITREMOTE ]]; then
350 # GIT is a go.
351 if ! is_git_repo $BASEDIR; then
352 # initializing git repository
353 cd $BASEDIR
354 $GIT init
355 echo "adding all files to git"
356 $GIT add .
bba0734f 357 $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}"
4cbcb39e 358 $GIT remote add origin $GITREMOTE
359 $GIT push -u origin master
360 fi
361elif [[ $USEGIT && -z $GITREMOTE ]]; then
362 echo "GITREMOTE variable not set. reverting USEGIT to false"
363 USEGIT=false
364fi
365
cf6d89bc 366addnote() {
bba0734f 367 # attempt syncing before adding a note
1f4d7742 368 gitsync -f
026502da 369 # remove eventually existing temp DB file
370 if [[ -f $TMPDB ]]; then
371 rm $TMPDB
372 fi
373
6a193095 374 RTITLE=$(random_title)
375 [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
53f2ed57 376 echo "adding new note - \"$NOTETITLE\""
b9f21021 377 # shellcheck disable=SC2086
e3670e83 378 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
379 # [ "" == $LASTID ] && LASTID=0
b9f21021 380 NOTEID=$(( LASTID + 1 ))
381 # shellcheck disable=SC2086
a4aaf855 382 touch ${NOTESDIR}/${NOW}
b9f21021 383 # shellcheck disable=SC2016
a4aaf855 384 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
b9f21021 385 # shellcheck disable=SC2086
a4aaf855 386 mv $TMPDB $DB
b648c006 387 # example for alacritty:
388 # alacritty --class notes --title notes -e /usr/bin/vim ...
b9f21021 389 # shellcheck disable=SC2086,SC2091
e3670e83 390 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
bba0734f 391 # add note to git and push to remote
392 gitadd
a4aaf855 393}
cf6d89bc 394backup_data() {
9eb02251 395 BACKUPDIR="$1"
396 echo "backing up data in $BACKUPDIR"
397
398
399 if [ -d $BACKUPDIR ]; then
400 if [ $(/bin/ls -A $BACKUPDIR) ]; then
401 echo "$BACKUPDIR is not empty. Cannot continue"
402 exit
403 else
404 echo "$BACKUPDIR is ok. Continuing!"
405 fi
406 else
407 # BACKUPDIR doesn't exists
408 echo "$BACKUPDIR doesn't exists"
409 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
410 case $ANSWER in
411 y|Y )
412 mkdir -p $BACKUPDIR
413 ;;
414 * )
415 echo "No changes made. Exiting"
416 exit
417 ;;
418 esac
419 fi
420 # ok, we have a backup directory
421 if [ -r $RCFILE ]; then
87a368fe 422 BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
9eb02251 423 else
87a368fe 424 BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR})
9eb02251 425 fi
426 # run the command
427 if [ "$BCKUP_COMM" ]; then
3951cc3d 428 echo -e "All files backed up."
9eb02251 429 echo -e "BACKUP directory:\t$BACKUPDIR"
3951cc3d 430 tree $BACKUPDIR | $PAGER
9eb02251 431 echo; echo "BACKUP COMPLETED"
432 fi
433}
434
cf6d89bc 435backup_restore() {
3951cc3d 436 BACKUPDIR="$1"
437 echo "restoring backup from $BACKUPDIR"
438 echo "This will overwrite all your notes and configurations with the backup."
439 read -r -p "Do you want to continue? (y/N) " ANSWER
440 case $ANSWER in
441 y|Y )
442 # restoring rc file
443 BACKUPRC=$(basename $RCFILE)
444 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
445 if [ -r ${RCFILE} ]; then
446 echo "Backing up current '${RCFILE}'...."
447 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
448 fi
449 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
450 fi
451 # restoring notes directory
452 if [ -d $BACKUPDIR/notes ]; then
453 if [ $(/bin/ls -A $NOTESDIR) ]; then
454 rm --verbose $NOTESDIR/*
455 fi
456 cp -r --verbose $BACKUPDIR/notes $BASEDIR
457 fi
458 # restoring database
459 BACKUPDB=$(basename $DB)
460 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
461 if [ -r ${DB} ]; then
462 echo "Backing up current '${DB}'...."
463 mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M)
464 fi
465 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
466 fi
87a368fe 467 # restoring git repo subdirectory
468 if [ -d $BACKUPDIR/.git ]; then
469 if [ /bin/ls -A ${BASEDIR}/.git ]; then
470 rm -rf ${BASEDIR}/.git
471 fi
472 cp -r --verbose ${BACKUPDIR}/.git ${BASEDIR}/
473 fi
3951cc3d 474 ;;
475 * )
476 echo "No changes made. Exiting"
477 exit
478 ;;
479 esac
480}
481
cf6d89bc 482editnote() {
61c91990 483 NOTE=$1
b9f21021 484 # shellcheck disable=SC2155
485 local OK=$(check_noteID "$NOTE")
486 if [ ! "$OK" ]; then
61c91990 487 echo "invalid note \"$NOTE\""
b9f21021 488 echo "Use the note ID that you can fetch after listing your notes"
61c91990 489 exit 1
490 fi
491
b9f21021 492 # shellcheck disable=SC2016,SC2086
61c91990 493 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 494 # shellcheck disable=SC2016,SC2086
61c91990 495 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
44abbfe7 496 if [ "$TITLE" ]; then
497 echo "editing note $TITLE"
b9f21021 498 # shellcheck disable=SC2086,SC2091
b648c006 499 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
d1f115c1 500 gitedit
44abbfe7 501 else
502 echo "note not found"
503 exit 1
504 fi
a4aaf855 505}
cf6d89bc 506listnotes() {
507 # attempt syncing before listing all notes
508 gitsync
fb711183 509 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
510 if [[ $(ls -A "$NOTESDIR") ]]; then
511 if [ $PLAIN == false ]; then
512 echo "listing all notes"
513 echo ""
514 fi
515 [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
516 for i in "${NOTESDIR}"/*; do
517 # shellcheck disable=SC2155
518 local fname=$(basename $i)
519 DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
520 # shellcheck disable=SC2016,SC2086
521 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
522 # shellcheck disable=SC2016,SC2086
523 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
524 [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
525 done
526 else
527 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
528 fi
529}
cf6d89bc 530rmnote() {
026502da 531 # remove eventually existing temp DB file
532 if [[ -f $TMPDB ]]; then
533 rm $TMPDB
b648c006 534 fi
535
026502da 536 NOTE=$1
b9f21021 537 if [ "all" == "$NOTE" ]; then
026502da 538 echo "You're going to delete all notes."
539 read -r -p "Do you wish to continue? (y/N) " ANSWER
540 case $ANSWER in
541 y|Y )
b9f21021 542 # shellcheck disable=SC2086
026502da 543 $JQ 'del(.notes[])' $DB > $TMPDB
b9f21021 544 # shellcheck disable=SC2086
026502da 545 mv $TMPDB $DB
b9f21021 546 # shellcheck disable=SC2086
026502da 547 rm $NOTESDIR/*
d1f115c1 548 gitremove "all"
026502da 549 echo "Deleted all notes"
550 ;;
551 * )
552 echo "Aborting, no notes were deleted."
553 exit 1
554 ;;
555 esac
b648c006 556 else
b9f21021 557 # shellcheck disable=SC2155
558 local OK=$(check_noteID "$NOTE")
559 if [ ! "$OK" ]; then
026502da 560 echo "invalid note \"$NOTE\""
b9f21021 561 echo "Use the note ID that you can fetch after listing your notes"
f1343f20 562 sleep 1
026502da 563 exit 1
564 fi
565
b9f21021 566 # shellcheck disable=SC2016,SC2086
026502da 567 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 568 # shellcheck disable=SC2016,SC2086
026502da 569 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
570 if [ "$TITLE" ]; then
ad818a9d 571 # shellcheck disable=SC2016,SC2086
572 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
573 # shellcheck disable=SC2086
574 mv $TMPDB $DB
575 rm $NOTESDIR/$FILE
d1f115c1 576 gitremove $OK $FILE
ad818a9d 577 echo "Deleted note $TITLE"
f1343f20 578 sleep 1
579 exit
026502da 580 else
581 echo "note not found"
f1343f20 582 sleep 1
026502da 583 exit 1
584 fi
b648c006 585 fi
a4aaf855 586}
cf6d89bc 587shownote() {
ad818a9d 588 NOTE=$1
589
590 # shellcheck disable=SC2155
591 local OK=$(check_noteID "$NOTE")
592 if [ ! "$OK" ]; then
593 echo "invalid note \"$NOTE\""
594 echo "Use the note ID that you can fetch after listing your notes"
595 exit 1
596 fi
597
598 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
599
600 if [ "$FILE" ]; then
601 $PAGER ${NOTESDIR}/${FILE}
602 fi
603}
b9f21021 604# shellcheck disable=SC2006
1f4d7742 605GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,showconf,sync::,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
53f2ed57 606
b9f21021 607# shellcheck disable=SC2181
cb8fcb2f 608if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
53f2ed57 609
610# Note the quotes around `$GOPT': they are essential!
611eval set -- "$GOPT"
ad818a9d 612unset GOPT
53f2ed57 613
614while true; do
615 case "$1" in
616 -h | --help )
617 helptext
618 exit
619 ;;
620 -v | --version )
621 echo $BASENAME v${VERSION}
622 exit
623 ;;
c018122c 624 -p | --plain )
625 PLAIN=true
626 shift
627 ;;
53f2ed57 628 -l | --list )
629 listnotes
630 exit
631 ;;
632 -a | --add )
ad818a9d 633 case "$2" in
634 '' )
635 read -r -p "Title: " TITLE
636 ;;
637 * )
638 TITLE=$2
639 ;;
640 esac
53f2ed57 641 shift 2
642 addnote "$TITLE"
07d42c7a 643 exit
53f2ed57 644 ;;
6c152f7e 645 -e | --edit )
ad818a9d 646 case "$2" in
647 '' )
648 read -r -p "Note ID: " NOTE
649 ;;
650 * )
651 NOTE=$2
652 ;;
653 esac
53f2ed57 654 shift 2
655 editnote "$NOTE"
07d42c7a 656 exit
53f2ed57 657 ;;
b648c006 658 -d | --delete )
ad818a9d 659 case "$2" in
660 '' )
661 read -r -p "Note ID: " NOTE
662 ;;
663 * )
664 NOTE=$2
665 ;;
666 esac
53f2ed57 667 shift 2
668 rmnote "$NOTE"
07d42c7a 669 exit
53f2ed57 670 ;;
ad818a9d 671 -s | --show )
672 case "$2" in
673 '' )
674 read -r -p "Note ID: " NOTE
675 ;;
676 * )
677 NOTE=$2
678 ;;
679 esac
680 shift 2
681 shownote "$NOTE"
07d42c7a 682 exit
ad818a9d 683 ;;
3951cc3d 684 -r | --restore )
685 case "$2" in
686 '' )
687 read -r -p "Backup Dir: " RDIR
688 ;;
689 * )
690 RDIR=$2
691 ;;
692 esac
693 shift 2
694 backup_restore $RDIR
695 exit
696 ;;
4cbcb39e 697 --sync )
1f4d7742 698 case "$2" in
699 '' )
700 gitsync
701 ;;
702 '-f' )
703 gitsync -f
704 ;;
705 * )
706 helptext
707 exit
708 ;;
709 esac
710 shift 2
4cbcb39e 711 exit
712 ;;
d80ac20a 713 --userconf )
714 export_config
b9f21021 715 # shellcheck disable=SC2317
d80ac20a 716 echo "config exported to \"$RCFILE\""
b9f21021 717 # shellcheck disable=SC2317
d80ac20a 718 exit
53f2ed57 719 ;;
1f4d7742 720 --showconf )
721 configtext
722 exit
723 ;;
9eb02251 724 --backup )
725 case "$2" in
726 '' )
727 read -r -p "Backup Dir: " BDIR
728 ;;
729 * )
730 BDIR=$2
731 ;;
732 esac
733 shift 2
734 backup_data $BDIR
735 exit
736 ;;
53f2ed57 737 -- )
026502da 738 shift
739 break
53f2ed57 740 ;;
741 * )
742 break
743 ;;
744 esac
a4aaf855 745done
efa3e607 746
747for arg; do
748 if [ $(check_noteID $arg) ]; then
749 shownote $arg
750 else
751 helptext
752 exit
753 fi
754done