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