Continued work on rewrite v2a

This commit is contained in:
Jon-William Lewis
2016-12-30 21:07:07 -06:00
parent 207db67b68
commit 464c53f845

119
sns.sh
View File

@@ -3,64 +3,105 @@ XL_PRODUCT="Simple Note System"
XL_VER="v2a" XL_VER="v2a"
# Environment # Environment
SNS_STORE="$HOME/.local/sns" readonly SNS_STORE="$HOME/.local/sns"
SNS_KEYFILE="$SNS_STORE/.pubkey" readonly SNS_KEYFILE="$SNS_STORE/.pubkey"
SNS_DEPS=("gpg" "tree" "git") readonly SNS_DEPS=("gpg" "tree" "git")
readonly IFS=$'\n\t'
set -euo pipefail
declare -f sns_checkDeps # Checks the system for dependencies # Function Definitions
declare -f sns_initStore # Initializes the SNS Store ( -i) # sns_initStore Initializes the SNS Store ( -i)
declare -f sns_printHelp # Prints help page ( -h) # sns_printHelp Prints help page ( -h)
declare -f sns_list # Lists all notes in `tree` format ( *) # sns_list Lists all notes in `tree` format ( *)
declare -f sns_create # Creates note; calls sns_edit() ( -c) # sns_create Creates note; calls sns_edit() ( -c)
declare -f sns_edit # Decrypts note to /tmp, calls editor, re-encrypts note ( -e) # sns_edit Decrypts note to /tmp, calls editor, re-encrypts note ( -e)
declare -f sns_print # Prints note to stdout ( -p) # sns_print Prints note to stdout ( -p)
declare -f sns_rm # Deletes note from store ( -d) # sns_rm Deletes note from store ( -d)
declare -f sns_gitPassthrough # Passes through all instructions to git (git) # sns_gitPassthrough Passes through all instructions to git (git)
# sns_checkDeps Checks the system for required dependencies
# sns_checkStore Checks if $SNS_STORE exists
# sns_sanityCheck Wrapper for sns_checkDeps and sns_checkStore
function sns_checkDeps(){ function sns_initStore {
for DEP in "${SNS_DEPS[@]}"; do
if test ! -e "$(which gpg 2>/dev/null)"; then
echo "Dependency missing: $DEP"
fi
done
}
function sns_initStore(){
mkdir -p "$SNS_STORE" mkdir -p "$SNS_STORE"
echo "$1" > "$SNS_KEYFILE" echo $* | awk '{print $1}' > "$SNS_KEYFILE"
} }
function sns_printHelp(){ function sns_printHelp(){
printf "%s" "Import helpfile from sns v2" printf "%s" "Import helpfile from sns v2"
} }
function sns_list(){ function sns_list(){
tree -C --noreport "$1" cd "$SNS_STORE" || exit
tree -C --noreport "$(echo $* | awk '{print $1}')"
} }
function sns_create(){ function sns_create(){
gpg -r "$SNS_PUBKEY" -o "$1" < /dev/null gpg -r "$SNS_PUBKEY" -o "echo $* | awk '{print $1}'" < /dev/null
sns_edit "$1" sns_edit "echo $* | awk '{print $1}'"
} }
function sns_print(){ function sns_print(){
gpg -d "$1" gpg -d "echo $* | awk '{print $1}'"
} }
function sns_rm(){ function sns_rm(){
rm -f "$1" rm -f "echo $* | awk '{print $1}'"
} }
function sns_gitPassthrough(){ function sns_gitPassthrough(){
git "$@" cd "$SNS_STORE" || exit;
git "$@";
} }
# Entry Point function sns_checkDeps(){
printf "%s\n%s\n" "$XL_PRODUCT" "$XL_VER" local SNS_RETURN="true";
if [ -d "$SNS_STORE" ] && [ "$1" != "-i" ]; then for DEP in "${SNS_DEPS[@]}"; do
if test ! -e "$(which gpg 2>/dev/null)"; then
echo "Dependency missing: $DEP";
SNS_RETURN="false";
fi
done
"$SNS_RETURN";
}
function sns_checkStore(){
if [ -d "$SNS_STORE" ]; then true; else false; fi
}
function sns_sanityCheck {
if ! sns_checkDeps; then
exit 10
fi
if ! sns_checkStore; then
if [ "$(echo "$SNS_ACTION" | awk '{print $1;}')" != "sns_initStore" ]; then
printf "%s\n" "Please run \`sns -i\` to initialize sns." printf "%s\n" "Please run \`sns -i\` to initialize sns."
exit exit 20
fi fi
while getopts ":ihc:e:p:d:git:" OPT; do fi
}
function sns_argParse(){
declare -ga SNS_ACTION=()
while getopts ":i:hc:e:p:d:" OPT; do
case "$OPT" in case "$OPT" in
*) i)
if [ -d "$SNS_STORE/notes/$OPTARG" ]; then SNS_ACTION=("sns_initStore" "$OPTARG");;
sns_list "$SNS_STORE/notes/$OPTARG" h)
else SNS_ACTION=("sns_printHelp");;
sns_list "$SNS_STORE/notes/*" c)
fi SNS_ACTION=("sns_create $OPTARG");;
;; e)
SNS_ACTION=("sns_edit $OPTARG");;
p)
SNS_ACTION=("sns_print $OPTARG");;
d)
SNS_ACTION=("sns_rm $OPTARG");;
esac esac
done done
if [ "${#SNS_ACTION[@]}" -eq 0 ]; then
if [ "$(echo "$@" | awk '{print $1}')" == "git" ]; then
SNS_ACTION=($@)
else
if [ -d "$SNS_STORE/$*" ]; then
SNS_ACTION=("sns_list" "./$*")
fi
fi
fi
}
# Entry Point
printf "%s\n%s\n" "$XL_PRODUCT" "$XL_VER"
sns_argParse "$@"
sns_sanityCheck
"${SNS_ACTION[@]}"