From 7f093d32c7e7903e109e3fe703dd66343466a4b6 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Mon, 10 Jan 2022 16:21:11 -0700 Subject: initial commit --- notes | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 notes diff --git a/notes b/notes new file mode 100755 index 0000000..5cae4f8 --- /dev/null +++ b/notes @@ -0,0 +1,81 @@ +#!/bin/bash + +cmd=$1 +shift + +function do_init { + if [[ $# -gt 0 ]] ; then + echo "No arguments expected" >&2 + exit 1 + fi + + if [[ -d $HOME/.notes ]] ; then + echo "Directory ~/.notes already exists!" >&2 + exit 1 + fi + + git init $HOME/.notes +} + +function do_new { + cd "$HOME/.notes" + tmpfile="$(mktemp '/tmp/.notes.tmpfile.XXXX.md')" + rm -f "$tmpfile" + + file="$(date -u '+%Y-%m-%dT%H%M%S')".md + + "$EDITOR" "$tmpfile" + + if [[ "$?" -eq 0 && -f "$tmpfile" ]] ; then + mv "$tmpfile" "$file" + git add "$file" + git commit -m "New note taken: $file" + else + echo "Nothing added" + fi + + rm -f "$tmpfile" +} + +function do_grep { + cd "$HOME/.notes" + grep "$@" * +} + +function do_git { + cd $HOME/.notes + git "$@" +} + +function do_edit { + cd "$HOME/.notes" + file="$1" + + if [[ "$#" -ne 1 ]] ; then + echo "Exactly one argument required">&2 + exit 1 + fi + + if [[ ! -f "$file" ]] ; then + echo "No such file. Please use notes new.">&2 + exit 1 + fi + + "$EDITOR" "$file" + + if [[ "$?" -eq 0 ]] ; then + git add "$file" + git commit -m "Updated $file" + fi +} + +case $cmd in + init) do_init "$@" ;; + new) do_new "$@";; + grep) do_grep "$@" ;; + edit) do_edit "$@" ;; + git) do_git "$@" ;; + *) + echo "$cmd: unknown command" >&2 + exit 1;; +esac -- cgit