-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemo
executable file
·97 lines (85 loc) · 2.51 KB
/
memo
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
#
# rotsix - (c) wtfpl 2017
# memo manager using git
# custom variables
memo_dir="$HOME/memo"
#
usage () {
echo "$0"
echo -e "usage:"
echo -e "\t$(basename "$0") list|show|l|s|ls\t\tsimple \`tree \$memo_dir\`"
echo -e "\t$(basename "$0") add|a <item>\t\tcreate the selected item (and directories if needed)"
echo -e "\t$(basename "$0") edit|e <item>\t\tedit the selected item"
echo -e "\t$(basename "$0") delete|d <item|dir>\tdelete the selected item (directory requires -r option)"
echo -e "\t$(basename "$0") git|g <git-command>\tuses git directly, so uses ~/.gitconfig"
echo -e "\t$(basename "$0") help|h|?\t\t\tdisplay this message"
}
die () {
echo "$@" >&2
exit 1
}
case "$1" in
"list"|"show"|"l"|"s"|"ls")
tree "$memo_dir" || exit 1
exit 0
;;
"add"|"a")
shift
mkdir -p "$(dirname "$*")"
if [[ -n "$EDITOR" ]]; then
"$EDITOR" "$memo_dir/$*" || exit 1
else
die "Set $EDITOR first please, or use \`EDITOR=<editor> memo ...\`"
fi
git -C "$memo_dir" add "$@"
git -C "$memo_dir" commit -m "Adding $*"
git -C "$memo_dir" push
;;
"edit"|"e")
shift
mkdir -p "$(dirname "$*")"
if [[ -n "$EDITOR" ]]; then
"$EDITOR" "$memo_dir/$*" || exit 1
else
die "Set \$EDITOR first please, or use \`EDITOR=<editor> memo ...\`"
fi
git -C "$memo_dir" add "$@"
git -C "$memo_dir" commit -m "Editing $*"
git -C "$memo_dir" push
;;
"delete"|"d")
shift
__type="f"
while getopts "rf" opt; do
case "$opt" in
"r")
__type="d"
shift
;;
*)
echo "Invalid option: -$OPTARG" >&2
echo "Considering it is a file and not a directory."
;;
esac
done
test "$(dirname "$*")" == "." && __dir="" || __dir="$(dirname "$*")"
find "$memo_dir/$__dir" --type "$__type" -name "$(basename "$*")" -delete || exit 1
if [[ "$__type" == "f" ]]; then
git -C "$memo_dir" add "$@"
git -C "$memo_dir" commit -m "Deleting $*"
git -C "$memo_dir" push
fi
;;
"git"|"g")
shift
git -C "$memo_dir" "$@" || exit 1
;;
"help"|"h"|"?"|"")
usage
exit 0;;
*)
usage
exit 1
;;
esac