diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index 3a306e3..cb5e42d --- a/build.sh +++ b/build.sh @@ -10,7 +10,6 @@ bash header.sh > "$S" echo -e "\n# Section: Functions" >> "$S" cat ./src/includes/init_store.sns.sh >> "$S" cat ./src/includes/verify_store.sns.sh >> "$S" -cat ./src/includes/pause.sns.sh >> "$S" cat ./src/includes/help.sns.sh >> "$S" cat ./src/includes/p_header.sh >> "$S" cat ./src/includes/libencryption.sns.sh >> "$S" @@ -19,7 +18,7 @@ cat ./src/includes/delete.sns.sh >> "$S" cat ./src/includes/edit.sns.sh >> "$S" cat ./src/includes/print.sns.sh >> "$S" cat ./src/includes/list.sns.sh >> "$S" -printf "%s\n\n\n\n" "# End Section: Functions" >> "$S" +printf "%s\n" "# End Section: Functions" >> "$S" cat ./src/main/stage1.sns.sh >> "$S" cat ./src/main/stage2.sns.sh >> "$S" cat ./src/main/stage3.sns.sh >> "$S" diff --git a/errors.md b/errors.md new file mode 100644 index 0000000..edde651 --- /dev/null +++ b/errors.md @@ -0,0 +1,21 @@ +# Simple Note System, version 2 +## Error Code Reference + +### General Codes +| Name | Code | Meaning | +|-------------|------|--------------------------------------| +| ERR_NO_ARGS | 10 | No arguments were specified | +| ERR_NO_OP | 20 | No operation was specified | +| ERR_NO_NOTE | 30 | A required argument was not provided | + +### Encryption-related codes +|Name | Code | Meaning | +|------------|------|-------------------------------------------------------| +| ERR_NO_GPG | 100 | Encryption is enabled, but GPG is not installed | +| ERR_NO_KEY | 110 | Encryption is enabled, but no recipient was specified | + +### Creation-related codes +|Name | Code | Meaning | +|-----------------|------|-----------------------------------| +| ERR_NOTE_EXiSTS | 200 | The specified note already exists | +| ERR_NOTE_NO_READ| 205 | The specified note cannot be read | diff --git a/errors.ref b/errors.ref deleted file mode 100644 index eb46e38..0000000 --- a/errors.ref +++ /dev/null @@ -1,14 +0,0 @@ -Simple Note System, version 2 -Error Code Reference - -General------------------------------------------------------------------------- -ERR_NO_ARGS 10 No arguments were specified -ERR_NO_OP 20 No operation was specified -ERR_NO_NOTE 30 A required argument was not provided - -Encryption---------------------------------------------------------------------- -ERR_NO_GPG 100 Encryption is enabled, but GPG is not installed -ERR_NO_KEY 110 Encryption is enabled, but no recipient was specified - -Create-------------------------------------------------------------------------- -ERR_NOTE_EXiSTS 200 The specified note already exists diff --git a/header.sh b/header.sh index 63d8c87..d3c6c8f 100644 --- a/header.sh +++ b/header.sh @@ -1,5 +1,5 @@ PROD_STR="Simple Note System" -VER_STR="v2.0a8" +VER_STR="v2.0a9" YEAR=2016 cat << EOF @@ -12,6 +12,7 @@ cat << EOF # Prevent freak accidents involving the root directory if [ -z "\$HOME" ]; then HOME=/home/"\$(whoami)"; fi +# Store files and locations readonly PROD_STR="$PROD_STR" readonly VER_STR="$VER_STR" readonly ROOT_DIR="\$HOME"/.config/sns @@ -19,9 +20,11 @@ readonly NOTES_DIR="\$ROOT_DIR"/notes readonly TMP_DIR="\$ROOT_DIR"/tmp readonly CONFIG_FILE="\$ROOT_DIR/sns.conf" +#Color codes for error reporting readonly RED_COLOR='\033[1;31m' readonly RESET_COLOR='\033[0m' +#Print the program header to stdout printf "%s\n" "\$PROD_STR" printf "%s\n" "------------------" EOF diff --git a/src/includes/create.sns.sh b/src/includes/create.sns.sh index 228bad0..2a9d6a6 100644 --- a/src/includes/create.sns.sh +++ b/src/includes/create.sns.sh @@ -7,7 +7,7 @@ function create(){ # Refuse to overwrite a note if [ -e "$NOTE_DIR/$NOTE" ]; then - printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ "Note already exists"\ "Hint: use -e to edit the note." exit 200 @@ -29,7 +29,7 @@ function create(){ if [ -e "$NOTE_DIR/$NOTE" ]; then printf " - %s\n" "Created note: ${NOTE%.*}" else - printf " $RED_COLOR!$RESET_COLOR%s\n"\ + >&2 printf " $RED_COLOR!$RESET_COLOR%s\n"\ "Something went wrong, and the note was not created." fi } diff --git a/src/includes/delete.sns.sh b/src/includes/delete.sns.sh index a8aefed..4404f95 100644 --- a/src/includes/delete.sns.sh +++ b/src/includes/delete.sns.sh @@ -6,6 +6,6 @@ function delete(){ rm "$NOTE_DIR/$NOTE" printf " - %s\n" "Deleted note: ${NOTE%.*}." else - printf " $RED_COLOR!$RESET_COLOR %s\n" "Note ${NOTE%.*} does not exist." + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n" "Note ${NOTE%.*} does not exist." fi } diff --git a/src/includes/edit.sns.sh b/src/includes/edit.sns.sh index 54702c5..a12ab5d 100644 --- a/src/includes/edit.sns.sh +++ b/src/includes/edit.sns.sh @@ -4,29 +4,46 @@ function edit(){ # Verify an editor was specified if [ -z "$EDITOR" ]; then - >&2 echo "Error no editor specified in environment." + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n"\ + "No editor specified in environment." exit # Verify the note exists elif [ ! -r "$NOTE" ]; then - echo "ERROR: Note cannot be opened for editing." + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n"\ + "Note cannot be opened for editing." exit 40; fi -# When encryption is enabled, decrypt $NOTE to a temp file + +# If encryption is enabled, decrypt $NOTE to a temp file, otherwise +# operate on the note directly. if [ "$ENCRYPTION" == "TRUE" ]; then cp "$NOTE" "$NOTE".bk #Insurance - if [ ! -d "$TMP_DIR" ]; then mkdir "$TMP_DIR"; fi TMP_NOTE="$TMP_DIR/$SESSION_ID" decrypt > "$TMP_NOTE" -else TMP_NOTE="$NOTE"; fi +else + TMP_NOTE="$NOTE"; +fi +# Write an ammendment header +if [ -z "$CREATE" ]; then + printf "\n %s\n" "edit - $(date)" >> "$TMP_NOTE" + printf "\n %s\n" "===================================" >> "$TMP_NOTE" +fi -if [ -z "$CREATE" ]; then printf "\nEDIT %s" "$(date)" >> "$TMP_NOTE"; fi - +# Call the editor +printf " - %s\n" "editing ${NOTE%.*}" "$EDITOR" "$TMP_NOTE" +# If the file was previously decrypted, encrypt it back if [ "$ENCRYPTION" == "TRUE" ]; then rm "$NOTE" encrypt; - if [ -r "$NOTE" ]; then rm "$NOTE".bk; fi + if [ ! -r "$NOTE" ]; then + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n" "error: note was not saved." + cp "$NOTE.bk" "$NOTE" + else + rm "$NOTE.bk"; + fi fi + } diff --git a/src/includes/init_store.sns.sh b/src/includes/init_store.sns.sh index 40a4f0d..9a2c20d 100644 --- a/src/includes/init_store.sns.sh +++ b/src/includes/init_store.sns.sh @@ -24,11 +24,11 @@ EOF chmod 600 "$CONFIG_FILE" -printf "\n - %s\n" "Rewrote Default Configuration" +printf " - %s\n" "Rewrote Default Configuration" if [ "$WILL_INIT" == "TRUE" ]; then - printf " - %s %s\n" "Environment initialized in" "$ROOT_DIR" + printf " - %s\n" "Environment initialized in $ROOT_DIR" else - printf " - %s\n" "Store already initialized." + printf " - %s\n" "Store already initialized." fi } diff --git a/src/includes/libencryption.sns.sh b/src/includes/libencryption.sns.sh index 71b1181..04ba8b3 100644 --- a/src/includes/libencryption.sns.sh +++ b/src/includes/libencryption.sns.sh @@ -1,6 +1,6 @@ function encrypt(){ # This function, given a recipient, $PUBKEY; a file to encrypt, $TMP_NOTE; and an -# output file, "$NOTE", will encrypt $TMP_NOTE to $NOTE against $PUBKEY's private +# output file, $NOTE, will encrypt $TMP_NOTE to $NOTE against $PUBKEY's private # GPG key. gpg -r "$PUBKEY" -o "$NOTE" -e "$TMP_NOTE" @@ -8,8 +8,8 @@ function encrypt(){ } function decrypt(){ -# This function, given a recipient, $PUBKEY; a file to decrypt, $TMP_NOTE; and an -# output file, "$NOTE", will decrpyt $TMP_NOTE to $NOTE against $PUBKEY's private -# GPG key. +# This function, given a file to decrypt, will attempt to decrypt the file +# against the specified recipient's private key, and print the result to +# stdout. gpg -d "$NOTE" } diff --git a/src/includes/list.sns.sh b/src/includes/list.sns.sh index d65d4f7..2869286 100644 --- a/src/includes/list.sns.sh +++ b/src/includes/list.sns.sh @@ -1,8 +1,4 @@ function list(){ - if [ -d "$NOTES_DIR"/"$NOTEBOOK" ]; then - printf "+%s\n" "$NOTEBOOK" - find "$NOTES_DIR"/"$NOTEBOOK" -type f | while read -r NOTE; do - printf " -%s\n" "$(basename \"$NOTE\" | cut -d . -f 1 )" - done - fi + # This function, given a folder, $NOTE, will list the contents of $NOTE. + ls "$NOTE" } diff --git a/src/includes/pause.sns.sh b/src/includes/pause.sns.sh deleted file mode 100644 index ace9b99..0000000 --- a/src/includes/pause.sns.sh +++ /dev/null @@ -1,4 +0,0 @@ -function pause { - read -rp " Press [Enter] to continue." - echo "" -} diff --git a/src/includes/print.sns.sh b/src/includes/print.sns.sh index a24cd2b..fba75a4 100644 --- a/src/includes/print.sns.sh +++ b/src/includes/print.sns.sh @@ -1,13 +1,12 @@ function print(){ - if [ -r "$NOTE" ]; then - if [ -z "$CREATE" ]; then - if [ "$ENCRYPTION" == "TRUE" ]; then - decrypt - else - cat "$NOTE" - fi - else - printf "\nERROR: Note cannot be found.\n" - fi - fi +# Given an existing file, $NOTE, print prints the contents of $NOTE to stdout. + +if [ -r "$NOTE" ]; then + if [ "$ENCRYPTION" == "TRUE" ]; then decrypt #to stdout + else cat "$NOTE" fi +else + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ + "Note cannot be found." + exit 205 #ERR_NOTE_NO_READ +fi } diff --git a/src/includes/verify_store.sns.sh b/src/includes/verify_store.sns.sh index 31a59b9..ef0e9f6 100644 --- a/src/includes/verify_store.sns.sh +++ b/src/includes/verify_store.sns.sh @@ -1,7 +1,11 @@ function verify_store { + ETC_DIR=$(dirname "$CONFIG_FILE") + STORE_DIRS=("$ROOT_DIR" "$NOTES_DIR" "$TMP_DIR" "$ETC_DIR") for DIR in "${STORE_DIRS[@]}"; do - mkdir -p "$DIR" + if [ ! -d "$DIR" ]; then + mkdir -p "$DIR" + fi done } diff --git a/src/main/stage1.sns.sh b/src/main/stage1.sns.sh index 77d5008..40cbd1e 100644 --- a/src/main/stage1.sns.sh +++ b/src/main/stage1.sns.sh @@ -4,33 +4,20 @@ if [ -r "$CONFIG_FILE" ]; then source "$CONFIG_FILE" + verify_store else - init_store - source "$CONFIG_FILE" -fi - -verify_store - -if [ "$ENCRYPTION" == "TRUE" ]; then - if [ -z "$PUBKEY" ]; then - ERR_NO_KEY="TRUE" - ENCRYPTION="FALSE" - fi - - command -v gpg >/dev/null 2>&1 ||\ - { ERR_NO_GPG="TRUE"; ENCRYPTION="FALSE"; } + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ + "Configuration note found. Please run sns -i." fi if [ "$ENCRYPTION" == "TRUE" ]; then - if [ ! -d "$ROOT_DIR"/tmp ]; then - mkdir -p "$ROOT_DIR"/tmp + if [ ! -r $(which gpg) ]; then + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ + "Encryption was specified, but GPG is not installed." + exit 100 + elif [ -z "$PUBKEY" ]; then + >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ + "No GPG recipient was provided in $CONFIG_FILE. " + exit 110 fi fi - -if [ -n "$ERR_NO_GPG" ]; then - >&2 echo " Error: Encryption was specified, but GPG is not installed." - exit 100 -elif [ -n "$ERR_NO_KEY" ]; then - >&2 echo " Error: No GPG recipient was provided in $CONFIG_FILE. " - exit 110 -fi diff --git a/src/main/stage2.sns.sh b/src/main/stage2.sns.sh index 3ddb324..4423feb 100644 --- a/src/main/stage2.sns.sh +++ b/src/main/stage2.sns.sh @@ -4,54 +4,45 @@ NOTE="" if [ -z "$1" ]; then help; exit 20 else -for ARG in "$@"; do - case "$ARG" in - -c|--create) - CREATE="TRUE" - OP="TRUE" - ;; - -d|--delete) - DELETE="TRUE" - OP="TRUE" - ;; - -e|--edit) - EDIT="TRUE" - OP="TRUE" - ;; - -ce|-ec) - CREATE="TRUE" - EDIT="TRUE" - OP="TRUE" - ;; - -l|--list) - LIST="TRUE" - OP="TRUE" - ;; - -p|--print) - PRINT="TRUE" - OP="TRUE" - ;; - -h|--help) - help - exit 0 - ;; - -i|--init-store) - init_store - exit 0 - ;; - *) - NOTE="$ARG" - break; - ;; - esac -done -if [ -n "$NAME" ] && [ -z "$NOTEBOOK" ] && [ -n "$LIST" ]; then - # If a note title was specified, but no notebook, and the list option - # was specified, assume that the detected note title is actually the - # name of a notebook - NOTEBOOK="$NAME" - NAME="" + for ARG in "$@"; do + case "$ARG" in + -c|--create) + CREATE="TRUE" + OP="TRUE" + ;; + -d|--delete) + DELETE="TRUE" + OP="TRUE" + ;; + -e|--edit) + EDIT="TRUE" + OP="TRUE" + ;; + -ce|-ec) + CREATE="TRUE" + EDIT="TRUE" + OP="TRUE" + ;; + -l|--list) + LIST="TRUE" + OP="TRUE" + ;; + -p|--print) + PRINT="TRUE" + OP="TRUE" + ;; + -h|--help) + help + exit 0 + ;; + -i|--init-store) + init_store + exit 0 + ;; + *) + NOTE="$ARG" + break; + ;; + esac + done fi -fi - -# w_conf and help are called here to avoid excess stage 3 code. diff --git a/src/main/stage3.sns.sh b/src/main/stage3.sns.sh index ca3c262..2880385 100644 --- a/src/main/stage3.sns.sh +++ b/src/main/stage3.sns.sh @@ -13,8 +13,6 @@ if [ -z "$NOTE" ]; then exit 30 fi -NOTE_DIR="$NOTES_DIR"/"$NOTEBOOK"/"$SECTION" - if [ "$ENCRYPTION" == "TRUE" ]; then SESSION_ID="$RANDOM" #SESSION_ID later becomes the temporary filename readonly NOTE="$NOTE.$EXT.gpg" @@ -22,12 +20,11 @@ else readonly NOTE="$NOTE.$EXT" fi -if [ "$LIST" == "TRUE" ]; then list; exit 0; fi -if [ "$PRINT" == "TRUE" ]; then print; exit 0; fi +if [ "$LIST" == "TRUE" ]; then list ; exit 0; fi +if [ "$PRINT" == "TRUE" ]; then print ; exit 0; fi if [ "$DELETE" == "TRUE" ]; then delete; exit 0; fi if [ "$CREATE" == "TRUE" ]; then create; fi -if [ "$EDIT" == "TRUE" ]; then edit; fi - +if [ "$EDIT" == "TRUE" ]; then edit ; fi #============================================================================== # End Section: Actions / Stage 3 #==============================================================================