This commit is contained in:
Jon-William Lewis
2016-08-11 16:02:08 -05:00
parent 47c697be24
commit 7438806847
13 changed files with 198 additions and 133 deletions

View File

@@ -7,7 +7,7 @@ function create(){
# Refuse to overwrite a note
if [ -e "$NOTES_DIR/$NOTE" ]; then
>&2 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
@@ -27,9 +27,9 @@ function create(){
fi
# Make sure the note exists, and inform the user of the result.
if [ -e "$NOTES_DIR/$NOTE" ]; then
printf " - %s\n" "Created note: ${NOTE%.*}"
printf "%s\n" "- Created note: ${NOTE%.*}"
else
>&2 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

@@ -4,15 +4,15 @@ function delete(){
if [ -e "$NOTES_DIR/$NOTE" ]; then
rm "$NOTES_DIR/$NOTE"
printf " - %s\n" "Deleted note: ${NOTE%.*}."
printf "%s\n" "- Deleted note: ${NOTE%.*}."
#Cleanup empty notebooks/sections]
find "$NOTES_DIR" -mindepth 1 -type d | tac |\
while read -r DIR ; do
if [ ! "$(ls -A $DIR)" ]; then
if [ ! "$(ls -A "$DIR")" ]; then
rmdir "$DIR"
fi
done
else
>&2 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,12 +4,12 @@ function edit(){
# Verify an editor was specified
if [ -z "$EDITOR" ]; then
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n"\
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n"\
"No editor specified in environment."
exit
# Verify the note exists
elif [ ! -r "$NOTES_DIR/$NOTE" ]; then
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n"\
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n"\
"Note cannot be opened for editing."
exit 40;
fi
@@ -31,17 +31,17 @@ if [ -z "$CREATE" ]; then
fi
# Call the editor
printf " - %s\n" "editing ${NOTE%.*}"
printf "%s\n" "- editing ${NOTE%.*}"
"$EDITOR" "$TMP_NOTE"
# If the file was previously decrypted, encrypt it back
if [ "$ENCRYPTION" == "TRUE" ]; then
printf " - %s\n" "encrypting ${NOTE%.*}"
printf "%s\n" "- encrypting ${NOTE%.*}"
rm "$NOTES_DIR/$NOTE"
encrypt;
rm "$TMP_NOTE"
if [ ! -r "$NOTES_DIR/$NOTE" ]; then
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n" "error: note was not saved."
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n" "error: note was not saved."
cp "$NOTES_DIR/$NOTE.bk" "$NOTES_DIR/$NOTE"
else
rm "$NOTES_DIR/$NOTE.bk";

37
src/includes/header.sh Normal file
View File

@@ -0,0 +1,37 @@
#!/bin/bash
# Simple Note System
# Copyright (C) 2016, Jon Lewis
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Prevent freak accidents involving the root directory
if [ -z "$HOME" ]; then HOME=/home/"$(whoami)"; fi
# Store files and locations
readonly PROD_STR="Simple Note System"
readonly VER_STR="v2.0a12"
readonly ROOT_DIR="$HOME"/.local/sns
readonly NOTES_DIR="$ROOT_DIR"/notes
readonly TMP_DIR="$ROOT_DIR"/tmp
readonly CONFIG_FILE="$ROOT_DIR/sns.conf"
#Color codes for messages
readonly RED_COLOR='\033[1;31m'
readonly YELLOW_COLOR='\033[1;33m'
readonly RESET_COLOR='\033[0m'
#Print the program header to stdout
printf "%s\n" "$PROD_STR"
printf "%s\n" "------------------"

View File

@@ -1,5 +1,8 @@
function help {
printf "\n%s" "usage: sns [-cedlp] <notebook/section/name>"
if [ -z "$VCTL" ]; then
printf "\n%s%s%s" "usage: sns " "$VCTL" " ..."
fi
printf "\n%s" " sns [-hi ]"
printf "\n%s" " -c | --create : Create note"
@@ -9,5 +12,5 @@ function help {
printf "\n%s" " -i | --init : Write default config and initalize SNS store"
printf "\n%s" " -l | --list : List all notes in NOTEBOOK"
printf "\n%s" " -p | --print : Print note to console"
printf "\n"
printf "\n\n"
}

View File

@@ -4,6 +4,7 @@ if [ ! -d "$ROOT_DIR" ]; then mkdir -p "$ROOT_DIR" ; WILL_INIT="TRUE"; fi
if [ ! -d "$TMP_DIR" ]; then mkdir -p "$TMP_DIR" ; WILL_INIT="TRUE"; fi
if [ ! -d "$NOTES_DIR" ]; then mkdir -p "$NOTES_DIR"; fi
if [ ! -r "$CONFIG_FILE" ]; then
cat > "$CONFIG_FILE" << EOF
# This file contains directives for the Simple Note System.
@@ -26,21 +27,19 @@ PUBKEY="" # Public Key
# Encryption is done using GPG. You must enter your
# public key's identifier here.
#VCTL="" # Version Control program
#VCTL="" # Version Control Program
# Set this to the name of your preferred version control
# program to use it in SNS. For example, if VCTL is Set
# to `git`, `sns git ...`` will call `git ...` in SNS's
# store. This can be used to revert changes, or to sync
# across computers.
# program to use it in SNS. Examples: git, hg, svn
EOF
chmod 600 "$CONFIG_FILE"
printf " - %s\n" "Rewrote Default Configuration"
if [ "$WILL_INIT" == "TRUE" ]; then
printf " - %s\n" "Environment initialized in $ROOT_DIR"
printf "%s\n" "- Rewrote Default Configuration"
else
printf " - %s\n" "Store already initialized."
printf "$RED_COLOR!$RESET_COLOR - %s" "Refusing to overwrite existing config"
fi
if [ "$WILL_INIT" == "TRUE" ]; then
printf "%s\n" "- Environment initialized in $ROOT_DIR"
else
printf "%s\n" "- Store already initialized."
fi
}

View File

@@ -1,3 +1,3 @@
function p_header(){
printf "# %s\n## %s\n" "$(basename ${NOTE%.*})" "$(date "$DATE_FMT")"
printf "# %s\n## %s\n" "$(basename "${NOTE%.*}")" "$(date "$DATE_FMT")"
}

View File

@@ -5,7 +5,7 @@ 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"\
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
"Note cannot be found."
exit 205 #ERR_NOTE_NO_READ
fi

View File

@@ -6,7 +6,7 @@ if [ -r "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
verify_store
elif [ "$1" != "-i" ]; then
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
"Configuration not found. Please run sns -i."
exit 5 #ERR_NO_STORE
fi
@@ -19,12 +19,12 @@ if [ "$ENCRYPTION" == "TRUE" ]; then
fi
# Check if GPG is installed.
if [ ! -r "$(which gpg)" ]; then
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
"Encryption was specified, but GPG is not installed."
exit 100
# Check if we have a GPG recipient
elif [ -z "$PUBKEY" ]; then
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
"No GPG recipient was provided in $CONFIG_FILE. "
exit 110
# All is good. If any previously unencrypted notes exist, encrypt them.
@@ -34,7 +34,7 @@ if [ "$ENCRYPTION" == "TRUE" ]; then
NOTE="${TMP_NOTE%.$EXT}.gpg.$EXT"
encrypt
if [ -r "$NOTE" ]; then
printf " $YELLOW_COLOR!$RESET_COLOR %s\n" "Encrypted ${NOTE%.$EXT}"
printf "$YELLOW_COLOR!$RESET_COLOR %s\n" "Encrypted ${NOTE%.$EXT}"
rm "$TMP_NOTE"
fi
done
@@ -57,7 +57,7 @@ else
--decrypt "$NOTE"
if [ -r "${NOTE%.gpg.note}.note" ]; then
printf " $YELLOW_COLOR!$RESET_COLOR %s\n"\
printf "$YELLOW_COLOR!$RESET_COLOR %s\n"\
"De-encrypted ${NOTE%.gpg.$EXT}"
rm "$NOTE";
fi

View File

@@ -9,7 +9,7 @@
# All options not requiring a note to be specified have been dealt
# with; if one isn't specified, exit on ERR_NO_NOTE.
if [ -z "$NOTE" ]; then
printf " $RED_COLOR!$RESET_COLOR %s\n" "No note specified."
printf "$RED_COLOR!$RESET_COLOR %s\n" "No note specified."
exit 30
fi