aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rwxr-xr-xlogentry54
2 files changed, 58 insertions, 0 deletions
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 <category>
+# 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 <category> [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"