summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <rahm@google.com>2022-01-10 16:21:11 -0700
committerJosh Rahm <rahm@google.com>2022-01-10 16:21:11 -0700
commit7f093d32c7e7903e109e3fe703dd66343466a4b6 (patch)
tree5604a6124bde103edbfcc52ea047c318d33e2631
downloadnotes-7f093d32c7e7903e109e3fe703dd66343466a4b6.tar.gz
notes-7f093d32c7e7903e109e3fe703dd66343466a4b6.tar.bz2
notes-7f093d32c7e7903e109e3fe703dd66343466a4b6.zip
initial commit
-rwxr-xr-xnotes81
1 files changed, 81 insertions, 0 deletions
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