| 1 | #! /bin/bash |
| 2 | |
| 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 | |
| 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 |
| 15 | |
| 16 | PID=$$ |
| 17 | BASENAME=$( basename "$0" ) |
| 18 | NOW=$(date +%s) |
| 19 | |
| 20 | VERSION="0.4git" |
| 21 | DBVERSION=${VERSION}_${NOW} |
| 22 | |
| 23 | set_defaults() { |
| 24 | # Binaries to use |
| 25 | JQ=${JQ:-/usr/bin/jq} |
| 26 | EDITOR=${EDITOR:-/usr/bin/vim} |
| 27 | TERMINAL=${TERMINAL:-/usr/bin/alacritty} |
| 28 | # Git binary only used if $USEGIT is true - See below |
| 29 | GIT=${GIT:-/usr/bin/git} |
| 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 |
| 33 | TERM_OPTS="--class notes --title notes -e " |
| 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} |
| 37 | |
| 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 |
| 41 | # base directory for program files |
| 42 | BASEDIR=${BASEDIR:-~/.local/share/bash-notes} |
| 43 | # notes database in json format |
| 44 | DB=${BASEDIR}/db.json |
| 45 | # directory containing the actual notes |
| 46 | NOTESDIR=${BASEDIR}/notes |
| 47 | |
| 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. Without this GIT will refuse to work |
| 53 | GITREMOTE=${GITREMOTE:-""} |
| 54 | # How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour) |
| 55 | GITSYNCDELAY=${GITSYNCDELAY:-"3600"} |
| 56 | # The name of this client. If left empty, defaults to the output of hostname |
| 57 | GITCLIENT=${GITCLIENT:-""} |
| 58 | |
| 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} |
| 65 | TMPDB=/tmp/db.json |
| 66 | |
| 67 | if [ ! -x "$JQ" ]; then |
| 68 | echo "jq not found in your PATH" |
| 69 | echo "install jq to continue" |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | # IMPORT USER DEFINED OPTIONS IF ANY |
| 74 | if [[ -f $RCFILE ]]; then |
| 75 | # shellcheck disable=SC1090 |
| 76 | source "$RCFILE" |
| 77 | fi |
| 78 | |
| 79 | # We prevent the program from running more than one instance: |
| 80 | PIDFILE=/var/tmp/$(basename "$0" .sh).pid |
| 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 |
| 88 | # shellcheck disable=SC2046,SC2086 |
| 89 | kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1 |
| 90 | # should already be deleted by trap, but just to be sure |
| 91 | rm "$PIDFILE" |
| 92 | fi |
| 93 | |
| 94 | # create PIDFILE |
| 95 | echo $PID > "$PIDFILE" |
| 96 | |
| 97 | # Export config to file |
| 98 | export_config() { |
| 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. |
| 119 | firstrun() { |
| 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 | |
| 134 | echo "Now I'll create the needed files and directories." |
| 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 | }, |
| 145 | "git": { |
| 146 | "lastpull": "" |
| 147 | }, |
| 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 |
| 165 | # check if input is a number, returns false or the number itself |
| 166 | check_noteID() { |
| 167 | IN=$1 |
| 168 | case $IN in |
| 169 | ''|*[!0-9]*) |
| 170 | false |
| 171 | ;; |
| 172 | *) |
| 173 | echo "$IN" |
| 174 | ;; |
| 175 | esac |
| 176 | } |
| 177 | |
| 178 | helptext() { |
| 179 | echo "Usage:" |
| 180 | echo " $0 [PARAMS] [note ID]..." |
| 181 | echo "" |
| 182 | echo "${BASENAME} parameters are:" |
| 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" |
| 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" |
| 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" |
| 196 | echo -e " --showconf\t\t\t: Display running options" |
| 197 | echo -e " --sync\t\t\t: Sync notes to git repository" |
| 198 | echo "" |
| 199 | echo -e "if a non option is passed and is a valid note ID, the note will be displayed." |
| 200 | } |
| 201 | |
| 202 | configtext() { |
| 203 | [ $USEGIT ] && GITUSE="enabled" || GITUSE="disabled" |
| 204 | if [ -n $GITCLIENT ]; then |
| 205 | CLIENTGIT="$( hostname )" |
| 206 | else |
| 207 | CLIENTGIT="$GITCLIENT" |
| 208 | fi |
| 209 | clear |
| 210 | echo -e "${BASENAME} configuration is:" |
| 211 | |
| 212 | echo -e "\tbase directory: ${BASEDIR}/" |
| 213 | echo -e "\tnotes archive: ${NOTESDIR}/" |
| 214 | echo -e "\tnotes database: ${DB}" |
| 215 | echo -e "\trc file: $RCFILE" |
| 216 | echo -e "\tdebug file: /tmp/debug_bash-note.log" |
| 217 | echo |
| 218 | echo -e "\ttext editor: ${EDITOR}" |
| 219 | echo -e "\tterminal: ${TERMINAL}" |
| 220 | echo -e "\tjq executable: ${JQ}" |
| 221 | echo -e "\tPAGER: ${PAGER}" |
| 222 | echo |
| 223 | echo -e "\tGIT: ${GITUSE} - ${GIT}" |
| 224 | echo -e "\tGIT remote: ${GITREMOTE}" |
| 225 | echo -e "\tGIT sync delay: ${GITSYNCDELAY}" |
| 226 | echo -e "\tGIT client name: ${CLIENTGIT}" |
| 227 | } |
| 228 | |
| 229 | # this function returns a random 2 words title |
| 230 | random_title() { |
| 231 | # Constants |
| 232 | X=0 |
| 233 | DICT=/usr/share/dict/words |
| 234 | OUTPUT="" |
| 235 | |
| 236 | # total number of non-random words available |
| 237 | COUNT=$(cat $DICT | wc -l) |
| 238 | |
| 239 | # while loop to generate random words |
| 240 | while [ "$X" -lt 2 ] |
| 241 | do |
| 242 | RAND=$(od -N3 -An -i /dev/urandom | awk -v f=0 -v r="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}') |
| 243 | OUTPUT+="$(sed `echo $RAND`"q;d" $DICT)" |
| 244 | (("X = X + 1")) |
| 245 | [[ $X -eq 1 ]] && OUTPUT+=" " |
| 246 | done |
| 247 | |
| 248 | echo $OUTPUT |
| 249 | } |
| 250 | |
| 251 | # check if GITCLIENT has been set or set it to the output of hostname |
| 252 | if [ -z "$GITCLIENT" ]; then |
| 253 | GITCLIENT=$( hostname ) |
| 254 | fi |
| 255 | # returns true if the argument provided directory is a git repository |
| 256 | is_git_repo() { |
| 257 | DIR=$1 |
| 258 | if [[ -d $DIR ]]; then |
| 259 | cd $DIR |
| 260 | if git rev-parse 2>/dev/null; then |
| 261 | true |
| 262 | else |
| 263 | false |
| 264 | fi |
| 265 | fi |
| 266 | } |
| 267 | |
| 268 | # sync local repository to remote |
| 269 | # accepts -f parameter to skip last sync check |
| 270 | gitsync() { |
| 271 | FORCE=$1 |
| 272 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
| 273 | [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\"" |
| 274 | NOWSYNC=$(date +%s) |
| 275 | if [[ $FORCE == "-f" ]]; then |
| 276 | $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB |
| 277 | mv $TMPDB $DB |
| 278 | cd $BASEDIR |
| 279 | [ $PLAIN == false ] && $GIT pull || $GIT pull -q |
| 280 | else |
| 281 | # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time. |
| 282 | LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB") |
| 283 | SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} )) |
| 284 | if (( $SYNCDIFF > $GITSYNCDELAY )); then |
| 285 | #more than our delay time has passed. We can sync again. |
| 286 | $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB |
| 287 | mv $TMPDB $DB |
| 288 | cd $BASEDIR |
| 289 | [ $PLAIN == false ] && $GIT pull || $GIT pull -q |
| 290 | else |
| 291 | # Last synced less than $GITSYNCDELAY seconds ago. We shall wait |
| 292 | [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait" |
| 293 | fi |
| 294 | fi |
| 295 | else |
| 296 | # no git, so we just keep going |
| 297 | true |
| 298 | fi |
| 299 | } |
| 300 | |
| 301 | # add note to git and push it to remote |
| 302 | gitadd() { |
| 303 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
| 304 | [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\"" |
| 305 | cd $BASEDIR |
| 306 | $GIT add . |
| 307 | $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}" |
| 308 | $GIT push origin master |
| 309 | else |
| 310 | # no git, so we just keep going |
| 311 | true |
| 312 | fi |
| 313 | } |
| 314 | |
| 315 | # edited note added to git and pushed it to remote |
| 316 | gitedit() { |
| 317 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
| 318 | [ $PLAIN == false ] && echo "Editing note on remote \"$GITREMOTE\"" |
| 319 | cd $BASEDIR |
| 320 | $GIT add . |
| 321 | $GIT commit -m "$(basename $0) - ${GITCLIENT} note edited." |
| 322 | $GIT push origin master |
| 323 | else |
| 324 | # no git, so we just keep going |
| 325 | true |
| 326 | fi |
| 327 | } |
| 328 | |
| 329 | # add note to git and push it to remote |
| 330 | gitremove() { |
| 331 | NOTE=$1 |
| 332 | FILE=$2 |
| 333 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
| 334 | [ $PLAIN == false ] && echo "Deleting notes from remote \"$GITREMOTE\"" |
| 335 | if [ "all" == $NOTE ];then |
| 336 | echo "Deleting all notes" |
| 337 | cd $BASEDIR |
| 338 | $GIT rm notes/* |
| 339 | $GIT commit -m "$(basename $0) - ${GITCLIENT} removing all notes." |
| 340 | $GIT push origin master |
| 341 | else |
| 342 | local OK=$(check_noteID "$NOTE") |
| 343 | if [[ "$OK" ]]; then |
| 344 | echo "Deleting note ID ${NOTE}" |
| 345 | cd $BASEDIR |
| 346 | $GIT rm notes/${FILE} |
| 347 | $GIT add . |
| 348 | $GIT commit -m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}." |
| 349 | $GIT push origin master |
| 350 | fi |
| 351 | fi |
| 352 | else |
| 353 | # no git, so we just keep going |
| 354 | true |
| 355 | fi |
| 356 | } |
| 357 | |
| 358 | # check for USEGIT and subsequent variables |
| 359 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
| 360 | # GIT is a go. |
| 361 | if ! is_git_repo $BASEDIR; then |
| 362 | # initializing git repository |
| 363 | cd $BASEDIR |
| 364 | $GIT init |
| 365 | echo "adding all files to git" |
| 366 | $GIT add . |
| 367 | $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}" |
| 368 | $GIT remote add origin $GITREMOTE |
| 369 | $GIT push -u origin master |
| 370 | fi |
| 371 | elif [[ $USEGIT && -z $GITREMOTE ]]; then |
| 372 | echo "GITREMOTE variable not set. reverting USEGIT to false" |
| 373 | USEGIT=false |
| 374 | fi |
| 375 | |
| 376 | addnote() { |
| 377 | # attempt syncing before adding a note |
| 378 | gitsync -f |
| 379 | # remove eventually existing temp DB file |
| 380 | if [[ -f $TMPDB ]]; then |
| 381 | rm $TMPDB |
| 382 | fi |
| 383 | |
| 384 | # RANDOM TITLE |
| 385 | RTITLE=$(random_title) |
| 386 | |
| 387 | if [[ -z $1 ]]; then |
| 388 | read -r -p "Title: " TITLE |
| 389 | case "$TITLE" in |
| 390 | '' ) |
| 391 | NOTETITLE="$RTITLE" |
| 392 | ;; |
| 393 | * ) |
| 394 | NOTETITLE=$TITLE |
| 395 | ;; |
| 396 | esac |
| 397 | fi |
| 398 | |
| 399 | # [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1" |
| 400 | echo "adding new note - \"$NOTETITLE\"" |
| 401 | # shellcheck disable=SC2086 |
| 402 | LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) |
| 403 | # [ "" == $LASTID ] && LASTID=0 |
| 404 | NOTEID=$(( LASTID + 1 )) |
| 405 | # shellcheck disable=SC2086 |
| 406 | touch ${NOTESDIR}/${NOW} |
| 407 | # shellcheck disable=SC2016 |
| 408 | $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB |
| 409 | # shellcheck disable=SC2086 |
| 410 | mv $TMPDB $DB |
| 411 | # example for alacritty: |
| 412 | # alacritty --class notes --title notes -e /usr/bin/vim ... |
| 413 | # shellcheck disable=SC2086,SC2091 |
| 414 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) |
| 415 | # add note to git and push to remote |
| 416 | gitadd |
| 417 | } |
| 418 | backup_data() { |
| 419 | BACKUPDIR="$1" |
| 420 | echo "backing up data in $BACKUPDIR" |
| 421 | |
| 422 | |
| 423 | if [ -d $BACKUPDIR ]; then |
| 424 | if [ $(/bin/ls -A $BACKUPDIR) ]; then |
| 425 | echo "$BACKUPDIR is not empty. Cannot continue" |
| 426 | exit |
| 427 | else |
| 428 | echo "$BACKUPDIR is ok. Continuing!" |
| 429 | fi |
| 430 | else |
| 431 | # BACKUPDIR doesn't exists |
| 432 | echo "$BACKUPDIR doesn't exists" |
| 433 | read -r -p "Do you want me to create it for you? (y/N) " ANSWER |
| 434 | case $ANSWER in |
| 435 | y|Y ) |
| 436 | mkdir -p $BACKUPDIR |
| 437 | ;; |
| 438 | * ) |
| 439 | echo "No changes made. Exiting" |
| 440 | exit |
| 441 | ;; |
| 442 | esac |
| 443 | fi |
| 444 | # ok, we have a backup directory |
| 445 | if [ -r $RCFILE ]; then |
| 446 | BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR}) |
| 447 | else |
| 448 | BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR}) |
| 449 | fi |
| 450 | # run the command |
| 451 | if [ "$BCKUP_COMM" ]; then |
| 452 | echo -e "All files backed up." |
| 453 | echo -e "BACKUP directory:\t$BACKUPDIR" |
| 454 | tree $BACKUPDIR | $PAGER |
| 455 | echo; echo "BACKUP COMPLETED" |
| 456 | fi |
| 457 | } |
| 458 | |
| 459 | backup_restore() { |
| 460 | BACKUPDIR="$1" |
| 461 | echo "restoring backup from $BACKUPDIR" |
| 462 | echo "This will overwrite all your notes and configurations with the backup." |
| 463 | read -r -p "Do you want to continue? (y/N) " ANSWER |
| 464 | case $ANSWER in |
| 465 | y|Y ) |
| 466 | # restoring rc file |
| 467 | BACKUPRC=$(basename $RCFILE) |
| 468 | if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then |
| 469 | if [ -r ${RCFILE} ]; then |
| 470 | echo "Backing up current '${RCFILE}'...." |
| 471 | mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) |
| 472 | fi |
| 473 | cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE |
| 474 | fi |
| 475 | # restoring notes directory |
| 476 | if [ -d $BACKUPDIR/notes ]; then |
| 477 | if [ $(/bin/ls -A $NOTESDIR) ]; then |
| 478 | rm --verbose $NOTESDIR/* |
| 479 | fi |
| 480 | cp -r --verbose $BACKUPDIR/notes $BASEDIR |
| 481 | fi |
| 482 | # restoring database |
| 483 | BACKUPDB=$(basename $DB) |
| 484 | if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then |
| 485 | if [ -r ${DB} ]; then |
| 486 | echo "Backing up current '${DB}'...." |
| 487 | mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M) |
| 488 | fi |
| 489 | cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB |
| 490 | fi |
| 491 | # restoring git repo subdirectory |
| 492 | if [ -d $BACKUPDIR/.git ]; then |
| 493 | if [ /bin/ls -A ${BASEDIR}/.git ]; then |
| 494 | rm -rf ${BASEDIR}/.git |
| 495 | fi |
| 496 | cp -r --verbose ${BACKUPDIR}/.git ${BASEDIR}/ |
| 497 | fi |
| 498 | ;; |
| 499 | * ) |
| 500 | echo "No changes made. Exiting" |
| 501 | exit |
| 502 | ;; |
| 503 | esac |
| 504 | } |
| 505 | |
| 506 | editnote() { |
| 507 | NOTE=$1 |
| 508 | # shellcheck disable=SC2155 |
| 509 | local OK=$(check_noteID "$NOTE") |
| 510 | if [ ! "$OK" ]; then |
| 511 | echo "invalid note \"$NOTE\"" |
| 512 | echo "Use the note ID that you can fetch after listing your notes" |
| 513 | exit 1 |
| 514 | fi |
| 515 | |
| 516 | # shellcheck disable=SC2016,SC2086 |
| 517 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
| 518 | # shellcheck disable=SC2016,SC2086 |
| 519 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
| 520 | if [ "$TITLE" ]; then |
| 521 | echo "editing note $TITLE" |
| 522 | # shellcheck disable=SC2086,SC2091 |
| 523 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE}) |
| 524 | gitedit |
| 525 | else |
| 526 | echo "note not found" |
| 527 | exit 1 |
| 528 | fi |
| 529 | } |
| 530 | listnotes() { |
| 531 | # attempt syncing before listing all notes |
| 532 | gitsync |
| 533 | # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored" |
| 534 | if [[ $(ls -A "$NOTESDIR") ]]; then |
| 535 | if [ $PLAIN == false ]; then |
| 536 | echo "listing all notes" |
| 537 | echo "" |
| 538 | fi |
| 539 | [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]" |
| 540 | for i in "${NOTESDIR}"/*; do |
| 541 | # shellcheck disable=SC2155 |
| 542 | local fname=$(basename $i) |
| 543 | DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z") |
| 544 | # shellcheck disable=SC2016,SC2086 |
| 545 | TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB) |
| 546 | # shellcheck disable=SC2016,SC2086 |
| 547 | ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB) |
| 548 | [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}" |
| 549 | done |
| 550 | else |
| 551 | echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\"" |
| 552 | fi |
| 553 | } |
| 554 | rmnote() { |
| 555 | # remove eventually existing temp DB file |
| 556 | if [[ -f $TMPDB ]]; then |
| 557 | rm $TMPDB |
| 558 | fi |
| 559 | |
| 560 | NOTE=$1 |
| 561 | if [ "all" == "$NOTE" ]; then |
| 562 | echo "You're going to delete all notes." |
| 563 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
| 564 | case $ANSWER in |
| 565 | y|Y ) |
| 566 | # shellcheck disable=SC2086 |
| 567 | $JQ 'del(.notes[])' $DB > $TMPDB |
| 568 | # shellcheck disable=SC2086 |
| 569 | mv $TMPDB $DB |
| 570 | # shellcheck disable=SC2086 |
| 571 | rm $NOTESDIR/* |
| 572 | gitremove "all" |
| 573 | echo "Deleted all notes" |
| 574 | ;; |
| 575 | * ) |
| 576 | echo "Aborting, no notes were deleted." |
| 577 | exit 1 |
| 578 | ;; |
| 579 | esac |
| 580 | else |
| 581 | # shellcheck disable=SC2155 |
| 582 | local OK=$(check_noteID "$NOTE") |
| 583 | if [ ! "$OK" ]; then |
| 584 | echo "invalid note \"$NOTE\"" |
| 585 | echo "Use the note ID that you can fetch after listing your notes" |
| 586 | sleep 1 |
| 587 | exit 1 |
| 588 | fi |
| 589 | |
| 590 | # shellcheck disable=SC2016,SC2086 |
| 591 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
| 592 | # shellcheck disable=SC2016,SC2086 |
| 593 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
| 594 | if [ "$TITLE" ]; then |
| 595 | # shellcheck disable=SC2016,SC2086 |
| 596 | $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB |
| 597 | # shellcheck disable=SC2086 |
| 598 | mv $TMPDB $DB |
| 599 | rm $NOTESDIR/$FILE |
| 600 | gitremove $OK $FILE |
| 601 | echo "Deleted note $TITLE" |
| 602 | sleep 1 |
| 603 | exit |
| 604 | else |
| 605 | echo "note not found" |
| 606 | sleep 1 |
| 607 | exit 1 |
| 608 | fi |
| 609 | fi |
| 610 | } |
| 611 | shownote() { |
| 612 | NOTE=$1 |
| 613 | |
| 614 | # shellcheck disable=SC2155 |
| 615 | local OK=$(check_noteID "$NOTE") |
| 616 | if [ ! "$OK" ]; then |
| 617 | echo "invalid note \"$NOTE\"" |
| 618 | echo "Use the note ID that you can fetch after listing your notes" |
| 619 | exit 1 |
| 620 | fi |
| 621 | |
| 622 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
| 623 | |
| 624 | if [ "$FILE" ]; then |
| 625 | $PAGER ${NOTESDIR}/${FILE} |
| 626 | fi |
| 627 | } |
| 628 | # shellcheck disable=SC2006 |
| 629 | GOPT=$(getopt -o hvplr:a::e:d:s: --long help,version,list,plain,userconf,showconf,sync,restore:,backup:,add::,edit:,delete:,show: -n 'bash-notes' -- "$@") |
| 630 | |
| 631 | # shellcheck disable=SC2181 |
| 632 | if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi |
| 633 | |
| 634 | # Note the quotes around `$GOPT': they are essential! |
| 635 | eval set -- "$GOPT" |
| 636 | unset GOPT |
| 637 | |
| 638 | while true; do |
| 639 | case "$1" in |
| 640 | -h | --help ) |
| 641 | helptext |
| 642 | exit |
| 643 | ;; |
| 644 | -v | --version ) |
| 645 | echo $BASENAME v${VERSION} |
| 646 | exit |
| 647 | ;; |
| 648 | -p | --plain ) |
| 649 | PLAIN=true |
| 650 | shift |
| 651 | ;; |
| 652 | -l | --list ) |
| 653 | listnotes |
| 654 | exit |
| 655 | ;; |
| 656 | -a | --add ) |
| 657 | TITLE=$2 |
| 658 | shift 2 |
| 659 | addnote "$TITLE" |
| 660 | exit |
| 661 | ;; |
| 662 | -e | --edit ) |
| 663 | NOTE=$2 |
| 664 | shift 2 |
| 665 | editnote "$NOTE" |
| 666 | exit |
| 667 | ;; |
| 668 | -d | --delete ) |
| 669 | NOTE=$2 |
| 670 | shift 2 |
| 671 | rmnote "$NOTE" |
| 672 | exit |
| 673 | ;; |
| 674 | -s | --show ) |
| 675 | NOTE=$2 |
| 676 | shift 2 |
| 677 | shownote "$NOTE" |
| 678 | exit |
| 679 | ;; |
| 680 | -r | --restore ) |
| 681 | RDIR=$2 |
| 682 | shift 2 |
| 683 | backup_restore $RDIR |
| 684 | exit |
| 685 | ;; |
| 686 | --sync ) |
| 687 | # I'm forcing it because if you run it manually, chances are that you need to. |
| 688 | gitsync -f |
| 689 | shift |
| 690 | exit |
| 691 | ;; |
| 692 | --userconf ) |
| 693 | export_config |
| 694 | # shellcheck disable=SC2317 |
| 695 | echo "config exported to \"$RCFILE\"" |
| 696 | # shellcheck disable=SC2317 |
| 697 | exit |
| 698 | ;; |
| 699 | --showconf ) |
| 700 | configtext |
| 701 | exit |
| 702 | ;; |
| 703 | --backup ) |
| 704 | BDIR=$2 |
| 705 | shift 2 |
| 706 | backup_data $BDIR |
| 707 | exit |
| 708 | ;; |
| 709 | -- ) |
| 710 | shift |
| 711 | break |
| 712 | ;; |
| 713 | * ) |
| 714 | break |
| 715 | ;; |
| 716 | esac |
| 717 | done |
| 718 | |
| 719 | for arg; do |
| 720 | if [ $(check_noteID $arg) ]; then |
| 721 | shownote $arg |
| 722 | else |
| 723 | helptext |
| 724 | exit |
| 725 | fi |
| 726 | done |