From 4eb8f12e25934e604ac096c357abcfdae87306ef Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sat, 28 Apr 2018 00:59:21 +0200 Subject: Initial commit, script is pretty much done --- README.md | 4 ++++ logentry | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 README.md create mode 100755 logentry diff --git a/README.md b/README.md new file mode 100644 index 0000000..c7af3e9 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# logentry +A shell script that creates a new logbook entry in the correct place, with the correct heading and correct name. + +In order for this to work, `LOGBOOK_ROOT` needs to be set as an environment variable. diff --git a/logentry b/logentry new file mode 100755 index 0000000..0c1bd41 --- /dev/null +++ b/logentry @@ -0,0 +1,54 @@ +#!/usr/bin/zsh +# Usage: logentry +# This script relies on LOGBOOK_ROOT being set, if it is not, the script fails. +if [ -z ${LOGBOOK_ROOT+x} ]; then + echo "LOGBOOK_ROOT is not set, exiting" + exit 1 +fi + +# It also needs to already exist +if [ ! -d "$LOGBOOK_ROOT" ]; then + echo "LOGBOOK_ROOT is not a valid directory, exiting" + exit 1 +fi + +# A category is required as an argument +if [ $# -le 0 ]; then + echo "Usage: logentry [title]" + exit +fi + +# It also needs to exist - this is to prevent typos from creating a new category and messing everything up +CAT_PATH="${LOGBOOK_ROOT%/}/$1" +if [ ! -d "$CAT_PATH" ]; then + echo "$1 is not a valid category, exiting" + exit 1 +fi + +# Ensure that the target directory exists +MONTH=$(date +%B | tr '[:upper:]' '[:lower:]') +YEAR=$(date +%Y) +DIR="${CAT_PATH%/}/$YEAR/$MONTH" + +mkdir -p "$DIR" +if [ $? -ne 0 ]; then + exit 1 +fi + +# Create full path +ISO_DATE=$(date --iso-8601) +if [ $# -gt 1 ]; then + NAME_OPT="-$2" +fi +FULLNAME="$DIR/$ISO_DATE$NAME_OPT.md" + +# Create the file with the correct heading, but only if it doesn't already exist +if [ ! -e "$FULLNAME" ]; then + echo "# $ISO_DATE\n## " > "$FULLNAME" + if [ $? -ne 0 ]; then + exit 1 + fi +fi + +# Edit the file +vim "$FULLNAME" -- cgit v1.2.3