Sweeping refinements and formatting changes
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
function pause {
|
||||
read -rp " Press [Enter] to continue."
|
||||
echo ""
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
#==============================================================================
|
||||
|
||||
Reference in New Issue
Block a user