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} |
bba0734f |
56 | # The name of this client. Defaults to the output of hostname |
57 | GITCLIENT=${GITCLIENT:-""} |
4cbcb39e |
58 | |
d80ac20a |
59 | } # end set_defaults, do not change this line. |
60 | |
61 | set_defaults |
62 | |
63 | # Do not edit below this point |
64 | RCFILE=${RCFILE:-~/.config/bash-notes.rc} |
53f2ed57 |
65 | TMPDB=/tmp/db.json |
a4aaf855 |
66 | |
b9f21021 |
67 | if [ ! -x "$JQ" ]; then |
a4aaf855 |
68 | echo "jq not found in your PATH" |
69 | echo "install jq to continue" |
70 | exit 1 |
71 | fi |
72 | |
53f2ed57 |
73 | # IMPORT USER DEFINED OPTIONS IF ANY |
74 | if [[ -f $RCFILE ]]; then |
b9f21021 |
75 | # shellcheck disable=SC1090 |
76 | source "$RCFILE" |
53f2ed57 |
77 | fi |
78 | |
a4aaf855 |
79 | # We prevent the program from running more than one instance: |
b9f21021 |
80 | PIDFILE=/var/tmp/$(basename "$0" .sh).pid |
a4aaf855 |
81 | |
82 | # Make sure the PID file is removed when we kill the process |
83 | trap 'rm -f $PIDFILE; exit 1' TERM INT |
84 | |
85 | if [[ -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 |
92 | fi |
93 | |
94 | # create PIDFILE |
b9f21021 |
95 | echo $PID > "$PIDFILE" |
a4aaf855 |
96 | |
fb711183 |
97 | # Export config to file |
cf6d89bc |
98 | export_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 |
119 | firstrun() { |
fb711183 |
120 | [ -f $RCFILE ] && RC=$RCFILE || RC="none" |
121 | |
122 | clear |
123 | echo "${BASENAME} configuration: |
124 | |
125 | base directory: ${BASEDIR}/ |
126 | notes archive: ${NOTESDIR}/ |
127 | notes database: ${DB} |
128 | rc file: $RC |
129 | text editor: ${EDITOR} |
130 | terminal: ${TERMINAL} |
131 | jq 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 |
161 | if [[ ! -d $NOTESDIR ]]; then |
162 | # we don't have a directory. FIRST RUN? |
163 | firstrun |
164 | fi |
61c91990 |
165 | # check if input is a number, returns false or the number itself |
cf6d89bc |
166 | check_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 |
178 | helptext() { |
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 |
200 | configtext() { |
3951cc3d |
201 | cat << __NOWCONF__ |
202 | ${BASENAME} configuration is: |
203 | |
204 | base directory: ${BASEDIR}/ |
205 | notes archive: ${NOTESDIR}/ |
206 | notes database: ${DB} |
207 | rc file: $RCFILE |
208 | debug file: /tmp/debug_bash-note.log |
209 | |
210 | text editor: ${EDITOR} |
211 | terminal: ${TERMINAL} |
212 | jq executable: ${JQ} |
213 | PAGER: ${PAGER} |
214 | __NOWCONF__ |
215 | |
216 | } |
217 | |
6a193095 |
218 | # this function returns a random 2 words title |
cf6d89bc |
219 | random_title() { |
6a193095 |
220 | # Constants |
221 | X=0 |
222 | DICT=/usr/share/dict/words |
223 | OUTPUT="" |
224 | |
225 | # total number of non-random words available |
226 | COUNT=$(cat $DICT | wc -l) |
227 | |
228 | # while loop to generate random words |
229 | while [ "$X" -lt 2 ] |
230 | do |
231 | RAND=$(od -N3 -An -i /dev/urandom | awk -v f=0 -v r="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}') |
232 | OUTPUT+="$(sed `echo $RAND`"q;d" $DICT)" |
233 | (("X = X + 1")) |
234 | [[ $X -eq 1 ]] && OUTPUT+=" " |
235 | done |
236 | |
237 | echo $OUTPUT |
238 | } |
239 | |
bba0734f |
240 | # check if GITCLIENT has been set or set it to the output of hostname |
241 | if [ -z $GITCLIENT ]; then |
242 | GITCLIENT=$( hostname ) |
243 | fi |
4cbcb39e |
244 | # returns true if the argument provided directory is a git repository |
245 | is_git_repo() { |
246 | DIR=$1 |
247 | if [[ -d $DIR ]]; then |
248 | cd $DIR |
249 | if git rev-parse 2>/dev/null; then |
250 | true |
251 | else |
252 | false |
253 | fi |
254 | fi |
255 | } |
256 | |
257 | # sync local repository to remote |
258 | gitsync() { |
bba0734f |
259 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
260 | NOWSYNC=$(date +%s) |
261 | # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time. |
262 | LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB") |
263 | [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\"" |
264 | SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} )) |
265 | if (( $SYNCDIFF > $GITSYNCDELAY )); then |
266 | #more than our delay time has passed. We can sync again. |
267 | $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB |
268 | mv $TMPDB $DB |
269 | cd $BASEDIR |
270 | $GIT pull |
271 | else |
272 | # Last synced less than $GITSYNCDELAY seconds ago. We shall wait |
273 | [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait" |
274 | fi |
275 | else |
276 | # no git, so we just keep going |
277 | true |
278 | fi |
279 | } |
280 | |
281 | # add note to git and push it to remote |
282 | gitadd() { |
283 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
284 | [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\"" |
cf6d89bc |
285 | cd $BASEDIR |
bba0734f |
286 | $GIT add . |
287 | $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}" |
288 | $GIT push origin master |
cf6d89bc |
289 | else |
bba0734f |
290 | # no git, so we just keep going |
291 | true |
cf6d89bc |
292 | fi |
4cbcb39e |
293 | } |
294 | |
295 | # check for USEGIT and subsequent variables |
296 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
297 | # GIT is a go. |
298 | if ! is_git_repo $BASEDIR; then |
299 | # initializing git repository |
300 | cd $BASEDIR |
301 | $GIT init |
302 | echo "adding all files to git" |
303 | $GIT add . |
bba0734f |
304 | $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}" |
4cbcb39e |
305 | $GIT remote add origin $GITREMOTE |
306 | $GIT push -u origin master |
307 | fi |
308 | elif [[ $USEGIT && -z $GITREMOTE ]]; then |
309 | echo "GITREMOTE variable not set. reverting USEGIT to false" |
310 | USEGIT=false |
311 | fi |
312 | |
cf6d89bc |
313 | addnote() { |
bba0734f |
314 | # attempt syncing before adding a note |
315 | gitsync |
026502da |
316 | # remove eventually existing temp DB file |
317 | if [[ -f $TMPDB ]]; then |
318 | rm $TMPDB |
319 | fi |
320 | |
6a193095 |
321 | RTITLE=$(random_title) |
322 | [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1" |
53f2ed57 |
323 | echo "adding new note - \"$NOTETITLE\"" |
b9f21021 |
324 | # shellcheck disable=SC2086 |
e3670e83 |
325 | LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) |
326 | # [ "" == $LASTID ] && LASTID=0 |
b9f21021 |
327 | NOTEID=$(( LASTID + 1 )) |
328 | # shellcheck disable=SC2086 |
a4aaf855 |
329 | touch ${NOTESDIR}/${NOW} |
b9f21021 |
330 | # shellcheck disable=SC2016 |
a4aaf855 |
331 | $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB |
b9f21021 |
332 | # shellcheck disable=SC2086 |
a4aaf855 |
333 | mv $TMPDB $DB |
b648c006 |
334 | # example for alacritty: |
335 | # alacritty --class notes --title notes -e /usr/bin/vim ... |
b9f21021 |
336 | # shellcheck disable=SC2086,SC2091 |
e3670e83 |
337 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) |
bba0734f |
338 | # add note to git and push to remote |
339 | gitadd |
a4aaf855 |
340 | } |
cf6d89bc |
341 | backup_data() { |
9eb02251 |
342 | BACKUPDIR="$1" |
343 | echo "backing up data in $BACKUPDIR" |
344 | |
345 | |
346 | if [ -d $BACKUPDIR ]; then |
347 | if [ $(/bin/ls -A $BACKUPDIR) ]; then |
348 | echo "$BACKUPDIR is not empty. Cannot continue" |
349 | exit |
350 | else |
351 | echo "$BACKUPDIR is ok. Continuing!" |
352 | fi |
353 | else |
354 | # BACKUPDIR doesn't exists |
355 | echo "$BACKUPDIR doesn't exists" |
356 | read -r -p "Do you want me to create it for you? (y/N) " ANSWER |
357 | case $ANSWER in |
358 | y|Y ) |
359 | mkdir -p $BACKUPDIR |
360 | ;; |
361 | * ) |
362 | echo "No changes made. Exiting" |
363 | exit |
364 | ;; |
365 | esac |
366 | fi |
367 | # ok, we have a backup directory |
368 | if [ -r $RCFILE ]; then |
87a368fe |
369 | BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR}) |
9eb02251 |
370 | else |
87a368fe |
371 | BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR}) |
9eb02251 |
372 | fi |
373 | # run the command |
374 | if [ "$BCKUP_COMM" ]; then |
3951cc3d |
375 | echo -e "All files backed up." |
9eb02251 |
376 | echo -e "BACKUP directory:\t$BACKUPDIR" |
3951cc3d |
377 | tree $BACKUPDIR | $PAGER |
9eb02251 |
378 | echo; echo "BACKUP COMPLETED" |
379 | fi |
380 | } |
381 | |
cf6d89bc |
382 | backup_restore() { |
3951cc3d |
383 | BACKUPDIR="$1" |
384 | echo "restoring backup from $BACKUPDIR" |
385 | echo "This will overwrite all your notes and configurations with the backup." |
386 | read -r -p "Do you want to continue? (y/N) " ANSWER |
387 | case $ANSWER in |
388 | y|Y ) |
389 | # restoring rc file |
390 | BACKUPRC=$(basename $RCFILE) |
391 | if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then |
392 | if [ -r ${RCFILE} ]; then |
393 | echo "Backing up current '${RCFILE}'...." |
394 | mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) |
395 | fi |
396 | cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE |
397 | fi |
398 | # restoring notes directory |
399 | if [ -d $BACKUPDIR/notes ]; then |
400 | if [ $(/bin/ls -A $NOTESDIR) ]; then |
401 | rm --verbose $NOTESDIR/* |
402 | fi |
403 | cp -r --verbose $BACKUPDIR/notes $BASEDIR |
404 | fi |
405 | # restoring database |
406 | BACKUPDB=$(basename $DB) |
407 | if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then |
408 | if [ -r ${DB} ]; then |
409 | echo "Backing up current '${DB}'...." |
410 | mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M) |
411 | fi |
412 | cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB |
413 | fi |
87a368fe |
414 | # restoring git repo subdirectory |
415 | if [ -d $BACKUPDIR/.git ]; then |
416 | if [ /bin/ls -A ${BASEDIR}/.git ]; then |
417 | rm -rf ${BASEDIR}/.git |
418 | fi |
419 | cp -r --verbose ${BACKUPDIR}/.git ${BASEDIR}/ |
420 | fi |
3951cc3d |
421 | ;; |
422 | * ) |
423 | echo "No changes made. Exiting" |
424 | exit |
425 | ;; |
426 | esac |
427 | } |
428 | |
cf6d89bc |
429 | editnote() { |
61c91990 |
430 | NOTE=$1 |
b9f21021 |
431 | # shellcheck disable=SC2155 |
432 | local OK=$(check_noteID "$NOTE") |
433 | if [ ! "$OK" ]; then |
61c91990 |
434 | echo "invalid note \"$NOTE\"" |
b9f21021 |
435 | echo "Use the note ID that you can fetch after listing your notes" |
61c91990 |
436 | exit 1 |
437 | fi |
438 | |
b9f21021 |
439 | # shellcheck disable=SC2016,SC2086 |
61c91990 |
440 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
b9f21021 |
441 | # shellcheck disable=SC2016,SC2086 |
61c91990 |
442 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
44abbfe7 |
443 | if [ "$TITLE" ]; then |
444 | echo "editing note $TITLE" |
b9f21021 |
445 | # shellcheck disable=SC2086,SC2091 |
b648c006 |
446 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE}) |
44abbfe7 |
447 | else |
448 | echo "note not found" |
449 | exit 1 |
450 | fi |
a4aaf855 |
451 | } |
cf6d89bc |
452 | listnotes() { |
453 | # attempt syncing before listing all notes |
454 | gitsync |
fb711183 |
455 | # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored" |
456 | if [[ $(ls -A "$NOTESDIR") ]]; then |
457 | if [ $PLAIN == false ]; then |
458 | echo "listing all notes" |
459 | echo "" |
460 | fi |
461 | [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]" |
462 | for i in "${NOTESDIR}"/*; do |
463 | # shellcheck disable=SC2155 |
464 | local fname=$(basename $i) |
465 | DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z") |
466 | # shellcheck disable=SC2016,SC2086 |
467 | TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB) |
468 | # shellcheck disable=SC2016,SC2086 |
469 | ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB) |
470 | [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}" |
471 | done |
472 | else |
473 | echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\"" |
474 | fi |
475 | } |
cf6d89bc |
476 | rmnote() { |
026502da |
477 | # remove eventually existing temp DB file |
478 | if [[ -f $TMPDB ]]; then |
479 | rm $TMPDB |
b648c006 |
480 | fi |
481 | |
026502da |
482 | NOTE=$1 |
b9f21021 |
483 | if [ "all" == "$NOTE" ]; then |
026502da |
484 | echo "You're going to delete all notes." |
485 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
486 | case $ANSWER in |
487 | y|Y ) |
b9f21021 |
488 | # shellcheck disable=SC2086 |
026502da |
489 | $JQ 'del(.notes[])' $DB > $TMPDB |
b9f21021 |
490 | # shellcheck disable=SC2086 |
026502da |
491 | mv $TMPDB $DB |
b9f21021 |
492 | # shellcheck disable=SC2086 |
026502da |
493 | rm $NOTESDIR/* |
494 | echo "Deleted all notes" |
495 | ;; |
496 | * ) |
497 | echo "Aborting, no notes were deleted." |
498 | exit 1 |
499 | ;; |
500 | esac |
b648c006 |
501 | else |
b9f21021 |
502 | # shellcheck disable=SC2155 |
503 | local OK=$(check_noteID "$NOTE") |
504 | if [ ! "$OK" ]; then |
026502da |
505 | echo "invalid note \"$NOTE\"" |
b9f21021 |
506 | echo "Use the note ID that you can fetch after listing your notes" |
f1343f20 |
507 | sleep 1 |
026502da |
508 | exit 1 |
509 | fi |
510 | |
b9f21021 |
511 | # shellcheck disable=SC2016,SC2086 |
026502da |
512 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
b9f21021 |
513 | # shellcheck disable=SC2016,SC2086 |
026502da |
514 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
515 | if [ "$TITLE" ]; then |
ad818a9d |
516 | # shellcheck disable=SC2016,SC2086 |
517 | $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB |
518 | # shellcheck disable=SC2086 |
519 | mv $TMPDB $DB |
520 | rm $NOTESDIR/$FILE |
521 | echo "Deleted note $TITLE" |
f1343f20 |
522 | sleep 1 |
523 | exit |
026502da |
524 | else |
525 | echo "note not found" |
f1343f20 |
526 | sleep 1 |
026502da |
527 | exit 1 |
528 | fi |
b648c006 |
529 | fi |
a4aaf855 |
530 | } |
cf6d89bc |
531 | shownote() { |
ad818a9d |
532 | NOTE=$1 |
533 | |
534 | # shellcheck disable=SC2155 |
535 | local OK=$(check_noteID "$NOTE") |
536 | if [ ! "$OK" ]; then |
537 | echo "invalid note \"$NOTE\"" |
538 | echo "Use the note ID that you can fetch after listing your notes" |
539 | exit 1 |
540 | fi |
541 | |
542 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
543 | |
544 | if [ "$FILE" ]; then |
545 | $PAGER ${NOTESDIR}/${FILE} |
546 | fi |
547 | } |
b9f21021 |
548 | # shellcheck disable=SC2006 |
4cbcb39e |
549 | 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 |
550 | |
b9f21021 |
551 | # shellcheck disable=SC2181 |
cb8fcb2f |
552 | if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi |
53f2ed57 |
553 | |
554 | # Note the quotes around `$GOPT': they are essential! |
555 | eval set -- "$GOPT" |
ad818a9d |
556 | unset GOPT |
53f2ed57 |
557 | |
558 | while true; do |
559 | case "$1" in |
560 | -h | --help ) |
561 | helptext |
562 | exit |
563 | ;; |
564 | -v | --version ) |
565 | echo $BASENAME v${VERSION} |
566 | exit |
567 | ;; |
c018122c |
568 | -p | --plain ) |
569 | PLAIN=true |
570 | shift |
571 | ;; |
53f2ed57 |
572 | -l | --list ) |
573 | listnotes |
574 | exit |
575 | ;; |
576 | -a | --add ) |
ad818a9d |
577 | case "$2" in |
578 | '' ) |
579 | read -r -p "Title: " TITLE |
580 | ;; |
581 | * ) |
582 | TITLE=$2 |
583 | ;; |
584 | esac |
53f2ed57 |
585 | shift 2 |
586 | addnote "$TITLE" |
07d42c7a |
587 | exit |
53f2ed57 |
588 | ;; |
6c152f7e |
589 | -e | --edit ) |
ad818a9d |
590 | case "$2" in |
591 | '' ) |
592 | read -r -p "Note ID: " NOTE |
593 | ;; |
594 | * ) |
595 | NOTE=$2 |
596 | ;; |
597 | esac |
53f2ed57 |
598 | shift 2 |
599 | editnote "$NOTE" |
07d42c7a |
600 | exit |
53f2ed57 |
601 | ;; |
b648c006 |
602 | -d | --delete ) |
ad818a9d |
603 | case "$2" in |
604 | '' ) |
605 | read -r -p "Note ID: " NOTE |
606 | ;; |
607 | * ) |
608 | NOTE=$2 |
609 | ;; |
610 | esac |
53f2ed57 |
611 | shift 2 |
612 | rmnote "$NOTE" |
07d42c7a |
613 | exit |
53f2ed57 |
614 | ;; |
ad818a9d |
615 | -s | --show ) |
616 | case "$2" in |
617 | '' ) |
618 | read -r -p "Note ID: " NOTE |
619 | ;; |
620 | * ) |
621 | NOTE=$2 |
622 | ;; |
623 | esac |
624 | shift 2 |
625 | shownote "$NOTE" |
07d42c7a |
626 | exit |
ad818a9d |
627 | ;; |
3951cc3d |
628 | -r | --restore ) |
629 | case "$2" in |
630 | '' ) |
631 | read -r -p "Backup Dir: " RDIR |
632 | ;; |
633 | * ) |
634 | RDIR=$2 |
635 | ;; |
636 | esac |
637 | shift 2 |
638 | backup_restore $RDIR |
639 | exit |
640 | ;; |
4cbcb39e |
641 | --sync ) |
642 | gitsync |
643 | exit |
644 | ;; |
d80ac20a |
645 | --userconf ) |
646 | export_config |
b9f21021 |
647 | # shellcheck disable=SC2317 |
d80ac20a |
648 | echo "config exported to \"$RCFILE\"" |
b9f21021 |
649 | # shellcheck disable=SC2317 |
d80ac20a |
650 | exit |
53f2ed57 |
651 | ;; |
9eb02251 |
652 | --backup ) |
653 | case "$2" in |
654 | '' ) |
655 | read -r -p "Backup Dir: " BDIR |
656 | ;; |
657 | * ) |
658 | BDIR=$2 |
659 | ;; |
660 | esac |
661 | shift 2 |
662 | backup_data $BDIR |
663 | exit |
664 | ;; |
53f2ed57 |
665 | -- ) |
026502da |
666 | shift |
667 | break |
53f2ed57 |
668 | ;; |
669 | * ) |
670 | break |
671 | ;; |
672 | esac |
a4aaf855 |
673 | done |
efa3e607 |
674 | |
675 | for arg; do |
676 | if [ $(check_noteID $arg) ]; then |
677 | shownote $arg |
678 | else |
679 | helptext |
680 | exit |
681 | fi |
682 | done |