Sweeping refinements and formatting changes

This commit is contained in:
Jon-William Lewis
2016-02-15 11:24:57 -06:00
parent 86fb0d2019
commit 7085a46ef3
16 changed files with 133 additions and 137 deletions

3
build.sh Normal file → Executable file
View File

@@ -10,7 +10,6 @@ bash header.sh > "$S"
echo -e "\n# Section: Functions" >> "$S" echo -e "\n# Section: Functions" >> "$S"
cat ./src/includes/init_store.sns.sh >> "$S" cat ./src/includes/init_store.sns.sh >> "$S"
cat ./src/includes/verify_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/help.sns.sh >> "$S"
cat ./src/includes/p_header.sh >> "$S" cat ./src/includes/p_header.sh >> "$S"
cat ./src/includes/libencryption.sns.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/edit.sns.sh >> "$S"
cat ./src/includes/print.sns.sh >> "$S" cat ./src/includes/print.sns.sh >> "$S"
cat ./src/includes/list.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/stage1.sns.sh >> "$S"
cat ./src/main/stage2.sns.sh >> "$S" cat ./src/main/stage2.sns.sh >> "$S"
cat ./src/main/stage3.sns.sh >> "$S" cat ./src/main/stage3.sns.sh >> "$S"

21
errors.md Normal file
View File

@@ -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 |

View File

@@ -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

View File

@@ -1,5 +1,5 @@
PROD_STR="Simple Note System" PROD_STR="Simple Note System"
VER_STR="v2.0a8" VER_STR="v2.0a9"
YEAR=2016 YEAR=2016
cat << EOF cat << EOF
@@ -12,6 +12,7 @@ cat << EOF
# Prevent freak accidents involving the root directory # Prevent freak accidents involving the root directory
if [ -z "\$HOME" ]; then HOME=/home/"\$(whoami)"; fi if [ -z "\$HOME" ]; then HOME=/home/"\$(whoami)"; fi
# Store files and locations
readonly PROD_STR="$PROD_STR" readonly PROD_STR="$PROD_STR"
readonly VER_STR="$VER_STR" readonly VER_STR="$VER_STR"
readonly ROOT_DIR="\$HOME"/.config/sns readonly ROOT_DIR="\$HOME"/.config/sns
@@ -19,9 +20,11 @@ readonly NOTES_DIR="\$ROOT_DIR"/notes
readonly TMP_DIR="\$ROOT_DIR"/tmp readonly TMP_DIR="\$ROOT_DIR"/tmp
readonly CONFIG_FILE="\$ROOT_DIR/sns.conf" readonly CONFIG_FILE="\$ROOT_DIR/sns.conf"
#Color codes for error reporting
readonly RED_COLOR='\033[1;31m' readonly RED_COLOR='\033[1;31m'
readonly RESET_COLOR='\033[0m' readonly RESET_COLOR='\033[0m'
#Print the program header to stdout
printf "%s\n" "\$PROD_STR" printf "%s\n" "\$PROD_STR"
printf "%s\n" "------------------" printf "%s\n" "------------------"
EOF EOF

View File

@@ -7,7 +7,7 @@ function create(){
# Refuse to overwrite a note # Refuse to overwrite a note
if [ -e "$NOTE_DIR/$NOTE" ]; then 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"\ "Note already exists"\
"Hint: use -e to edit the note." "Hint: use -e to edit the note."
exit 200 exit 200
@@ -29,7 +29,7 @@ function create(){
if [ -e "$NOTE_DIR/$NOTE" ]; then if [ -e "$NOTE_DIR/$NOTE" ]; then
printf " - %s\n" "Created note: ${NOTE%.*}" printf " - %s\n" "Created note: ${NOTE%.*}"
else 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." "Something went wrong, and the note was not created."
fi fi
} }

View File

@@ -6,6 +6,6 @@ function delete(){
rm "$NOTE_DIR/$NOTE" rm "$NOTE_DIR/$NOTE"
printf " - %s\n" "Deleted note: ${NOTE%.*}." printf " - %s\n" "Deleted note: ${NOTE%.*}."
else 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 fi
} }

View File

@@ -4,29 +4,46 @@ function edit(){
# Verify an editor was specified # Verify an editor was specified
if [ -z "$EDITOR" ]; then 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 exit
# Verify the note exists # Verify the note exists
elif [ ! -r "$NOTE" ]; then 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; exit 40;
fi 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 if [ "$ENCRYPTION" == "TRUE" ]; then
cp "$NOTE" "$NOTE".bk #Insurance cp "$NOTE" "$NOTE".bk #Insurance
if [ ! -d "$TMP_DIR" ]; then mkdir "$TMP_DIR"; fi
TMP_NOTE="$TMP_DIR/$SESSION_ID" TMP_NOTE="$TMP_DIR/$SESSION_ID"
decrypt > "$TMP_NOTE" 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" "$EDITOR" "$TMP_NOTE"
# If the file was previously decrypted, encrypt it back
if [ "$ENCRYPTION" == "TRUE" ]; then if [ "$ENCRYPTION" == "TRUE" ]; then
rm "$NOTE" rm "$NOTE"
encrypt; 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
fi
} }

View File

@@ -24,10 +24,10 @@ EOF
chmod 600 "$CONFIG_FILE" chmod 600 "$CONFIG_FILE"
printf "\n - %s\n" "Rewrote Default Configuration" printf " - %s\n" "Rewrote Default Configuration"
if [ "$WILL_INIT" == "TRUE" ]; then if [ "$WILL_INIT" == "TRUE" ]; then
printf " - %s %s\n" "Environment initialized in" "$ROOT_DIR" printf " - %s\n" "Environment initialized in $ROOT_DIR"
else else
printf " - %s\n" "Store already initialized." printf " - %s\n" "Store already initialized."
fi fi

View File

@@ -1,6 +1,6 @@
function encrypt(){ function encrypt(){
# This function, given a recipient, $PUBKEY; a file to encrypt, $TMP_NOTE; and an # 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 key.
gpg -r "$PUBKEY" -o "$NOTE" -e "$TMP_NOTE" gpg -r "$PUBKEY" -o "$NOTE" -e "$TMP_NOTE"
@@ -8,8 +8,8 @@ function encrypt(){
} }
function decrypt(){ function decrypt(){
# This function, given a recipient, $PUBKEY; a file to decrypt, $TMP_NOTE; and an # This function, given a file to decrypt, will attempt to decrypt the file
# output file, "$NOTE", will decrpyt $TMP_NOTE to $NOTE against $PUBKEY's private # against the specified recipient's private key, and print the result to
# GPG key. # stdout.
gpg -d "$NOTE" gpg -d "$NOTE"
} }

View File

@@ -1,8 +1,4 @@
function list(){ function list(){
if [ -d "$NOTES_DIR"/"$NOTEBOOK" ]; then # This function, given a folder, $NOTE, will list the contents of $NOTE.
printf "+%s\n" "$NOTEBOOK" ls "$NOTE"
find "$NOTES_DIR"/"$NOTEBOOK" -type f | while read -r NOTE; do
printf " -%s\n" "$(basename \"$NOTE\" | cut -d . -f 1 )"
done
fi
} }

View File

@@ -1,4 +0,0 @@
function pause {
read -rp " Press [Enter] to continue."
echo ""
}

View File

@@ -1,13 +1,12 @@
function print(){ function print(){
# Given an existing file, $NOTE, print prints the contents of $NOTE to stdout.
if [ -r "$NOTE" ]; then if [ -r "$NOTE" ]; then
if [ -z "$CREATE" ]; then if [ "$ENCRYPTION" == "TRUE" ]; then decrypt #to stdout
if [ "$ENCRYPTION" == "TRUE" ]; then else cat "$NOTE" fi
decrypt
else else
cat "$NOTE" >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
fi "Note cannot be found."
else exit 205 #ERR_NOTE_NO_READ
printf "\nERROR: Note cannot be found.\n"
fi
fi fi
} }

View File

@@ -1,7 +1,11 @@
function verify_store { function verify_store {
ETC_DIR=$(dirname "$CONFIG_FILE") ETC_DIR=$(dirname "$CONFIG_FILE")
STORE_DIRS=("$ROOT_DIR" "$NOTES_DIR" "$TMP_DIR" "$ETC_DIR") STORE_DIRS=("$ROOT_DIR" "$NOTES_DIR" "$TMP_DIR" "$ETC_DIR")
for DIR in "${STORE_DIRS[@]}"; do for DIR in "${STORE_DIRS[@]}"; do
if [ ! -d "$DIR" ]; then
mkdir -p "$DIR" mkdir -p "$DIR"
fi
done done
} }

View File

@@ -4,33 +4,20 @@
if [ -r "$CONFIG_FILE" ]; then if [ -r "$CONFIG_FILE" ]; then
source "$CONFIG_FILE" source "$CONFIG_FILE"
else
init_store
source "$CONFIG_FILE"
fi
verify_store verify_store
else
if [ "$ENCRYPTION" == "TRUE" ]; then >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
if [ -z "$PUBKEY" ]; then "Configuration note found. Please run sns -i."
ERR_NO_KEY="TRUE"
ENCRYPTION="FALSE"
fi
command -v gpg >/dev/null 2>&1 ||\
{ ERR_NO_GPG="TRUE"; ENCRYPTION="FALSE"; }
fi fi
if [ "$ENCRYPTION" == "TRUE" ]; then if [ "$ENCRYPTION" == "TRUE" ]; then
if [ ! -d "$ROOT_DIR"/tmp ]; then if [ ! -r $(which gpg) ]; then
mkdir -p "$ROOT_DIR"/tmp >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
fi "Encryption was specified, but GPG is not installed."
fi
if [ -n "$ERR_NO_GPG" ]; then
>&2 echo " Error: Encryption was specified, but GPG is not installed."
exit 100 exit 100
elif [ -n "$ERR_NO_KEY" ]; then elif [ -z "$PUBKEY" ]; then
>&2 echo " Error: No GPG recipient was provided in $CONFIG_FILE. " >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
"No GPG recipient was provided in $CONFIG_FILE. "
exit 110 exit 110
fi fi
fi

View File

@@ -45,13 +45,4 @@ for ARG in "$@"; do
;; ;;
esac esac
done 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=""
fi fi
fi
# w_conf and help are called here to avoid excess stage 3 code.

View File

@@ -13,8 +13,6 @@ if [ -z "$NOTE" ]; then
exit 30 exit 30
fi fi
NOTE_DIR="$NOTES_DIR"/"$NOTEBOOK"/"$SECTION"
if [ "$ENCRYPTION" == "TRUE" ]; then if [ "$ENCRYPTION" == "TRUE" ]; then
SESSION_ID="$RANDOM" #SESSION_ID later becomes the temporary filename SESSION_ID="$RANDOM" #SESSION_ID later becomes the temporary filename
readonly NOTE="$NOTE.$EXT.gpg" readonly NOTE="$NOTE.$EXT.gpg"
@@ -27,7 +25,6 @@ if [ "$PRINT" == "TRUE" ]; then print; exit 0; fi
if [ "$DELETE" == "TRUE" ]; then delete; exit 0; fi if [ "$DELETE" == "TRUE" ]; then delete; exit 0; fi
if [ "$CREATE" == "TRUE" ]; then create; fi if [ "$CREATE" == "TRUE" ]; then create; fi
if [ "$EDIT" == "TRUE" ]; then edit ; fi if [ "$EDIT" == "TRUE" ]; then edit ; fi
#============================================================================== #==============================================================================
# End Section: Actions / Stage 3 # End Section: Actions / Stage 3
#============================================================================== #==============================================================================