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

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

View File

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

View File

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

View File

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

View File

@@ -24,10 +24,10 @@ 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."
fi

View File

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

View File

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

View File

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

View File

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

View File

@@ -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
if [ ! -d "$DIR" ]; then
mkdir -p "$DIR"
fi
done
}

View File

@@ -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
fi
fi
if [ -n "$ERR_NO_GPG" ]; then
>&2 echo " Error: Encryption was specified, but GPG is not installed."
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 [ -n "$ERR_NO_KEY" ]; then
>&2 echo " Error: No GPG recipient was provided in $CONFIG_FILE. "
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

View File

@@ -4,7 +4,7 @@
NOTE=""
if [ -z "$1" ]; then help; exit 20
else
for ARG in "$@"; do
for ARG in "$@"; do
case "$ARG" in
-c|--create)
CREATE="TRUE"
@@ -44,14 +44,5 @@ for ARG in "$@"; do
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=""
done
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
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
#==============================================================================