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