diff --git a/histd.sh b/histd.sh index 5a1da18..fbb6436 100755 --- a/histd.sh +++ b/histd.sh @@ -3,6 +3,8 @@ # Histd: how I spent this day. # A simple but useful personal diary CLI utility. +set -eo pipefail + # Check if a text editor is specified function _check_editor { if [[ $EDITOR == "" ]]; then @@ -16,26 +18,28 @@ function _check_editor { function edit_note { # Create dirs (base_dir/year/month) WORKDIR="$BASE_DIR/$(date +%Y)/$(date +%m)" - mkdir -p $WORKDIR + mkdir -p "$WORKDIR" # Generate file name PATH_TO_FILE="$WORKDIR/$(date +%d).md" # Open editor _check_editor - cd $BASE_DIR - $EDITOR $PATH_TO_FILE - cd - > /dev/null + ( + cd $BASE_DIR + $EDITOR "$PATH_TO_FILE" + ) } # Create an archive with all notes function backup { ARCHIVE_PATH=~/histd-$(date +%F).tar.xz - cd $BASE_DIR - tar cfJ $ARCHIVE_PATH ../histd - cd - > /dev/null + ( + cd $BASE_DIR + tar cfJ "$ARCHIVE_PATH" ../histd + ) - echo Saved to $ARCHIVE_PATH + echo "Saved to $ARCHIVE_PATH" } # Concatenate all files and prefix each with the filename. @@ -54,7 +58,7 @@ function last { _check_editor LAST_FILE=$(find $BASE_DIR -type f -name "*.md" | tail -1) cd $BASE_DIR - $EDITOR $LAST_FILE + $EDITOR "$LAST_FILE" cd - > /dev/null }