summaryrefslogtreecommitdiff
path: root/notes
blob: 5cae4f8b83ef942cecf73274f586506c283faa92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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