aboutsummaryrefslogtreecommitdiff
path: root/note
blob: aa5b80c105e1b6c2ce4d59bf62ea6bdc47172eb9 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
# note - script for accessing and managing notes in plain text files
# Copyright (C) 2016 Karel Kočí
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# As first thing move to directory where we store notes
if ! [ -d ~/.notes ]; then
	if [ -e ~/.notes ]; then
		echo "Can't create directory ~/.notes, there is some file with such name." >&2
		exit -1
	else
		mkdir ~/.notes
		echo "New note storage created in ~/.notes"
	fi
fi
cd ~/.notes

# git stuff #####################################################################
# Check if there is git repository (at least if there is .git folder or file)
check_git() {
	if [ -e .git ]; then
		return 0
	else
		return 1
	fi
}

GIT_COMMIT="git commit --no-gpg-sign"

# If there are some changes in directory, then commit them as unmanaged.
git_commit_unmanaged() {
	if ! check_git; then return; fi
	# Just add any changes to index
	git add .
	if [ -n "$(git diff-index --cached --name-status HEAD)" ]; then
		# No changes staged, so we can return
		return
	fi
	$GIT_COMMIT -m "unmanaged: add all changes made without using note" 2>/dev/null
}

# If there are some changes in given file, then commit
git_commit_file_edited() {
	if ! check_git; then return; fi
	git add "$1"
	if [ -n "$(git diff-index --cached --name-status HEAD -- "$1")" ]; then
		# No changes staged, so we can return
		return
	fi
	$GIT_COMMIT -m "$1 updated" 2>/dev/null
}

git_commit_new() {
	if ! check_git; then return; fi
	git add "$1"
	$GIT_COMMIT -m "$1 created" 2>/dev/null
}

git_commit_remove() {
	if ! check_git; then return; fi
	git rm -f "$1"
	$GIT_COMMIT -m "$1 removed" 2>/dev/null
}

git_commit_move() {
	if ! check_git; then return; fi
	git mv "$1" "$2"
	$GIT_COMMIT -m "$1 renamed to $2" 2>/dev/null
}

# operations on notes ###########################################################

note_edit() {
	$EDITOR "$1"
	git_commit_file_edited "$1"
}

note_move() {
	# We have to check if we have git repository, otherwise we must move it our on self.
	if ! check_git; then
		mv "$1" "$2"
	else
		git_commit_move "$1" "$2"
	fi
}

note_less() {
	less "$1"
}

note_delete() {
	rm -rf "$1"
	echo "Removed $1"
	git_commit_remove "$1"
}

note_create() {
	touch "$1"
	echo "Create $1"
	git_commit_new "$1"
}

# arguments parsing and hep #####################################################

print_help() {
	echo "Usage: note [OPTIONS] <TARGET_NOTE>"
	echo "Edit and see notes from ~/.notes directory."
	echo
	echo "Options:"
	echo "  -d, --delete Remove given note."
	echo "  -c, --create Specifies that we want to just create note."
	echo "               Editor won't be opened. If such note exists,"
	echo "               error is reported."
	echo "  -e, --edit   Specifies that we want to edit given note."
	echo "               If note is missing, error is reported."
	echo "  -m, --move   Renames given note to new TARGET_NOTE."
	echo "  -l, --less   Shows note in less instead of editing it."
	echo "  -t, --tree   Shows all notes in tree. No TARGET_NOTE is"
	echo "               expected for when this option is used."
	echo "Git usage: note git <GIT_ARGS>"
	echo "  Executes git command in ~/.notes directory All arguments"
	echo "  after this operation is passed to git call as its arguments."
}

is_op() {
	case "$1" in
		git|d|delete|c|create|e|edit|m|move|l|less)
			return 0
			;;
		*)
			return 1
			;;
	esac
}

while (( $# > 0 )); do
	case "$1" in
		-h|--help|-?)
			print_help
			exit 0
			;;
		*)
			if [ -z "$OPERATION" ] && is_op "$1"; then
				OPERATION="$1"
				shift
				if [ "$OPERATION" == "git" ]; then
					GIT_ARGS="$@"
					shift $#
				fi
				continue
			fi
			if [ -z "$NOTE" ]; then
				NOTE="$1"
				shift
				continue
			fi
			if [ -z "$TARGET_NOTE" ]; then
				TARGET_NOTE="$1"
				shift
				continue
			fi
			echo "Unknown argument $1" >&2
			exit 1
			;;
	esac
	shift
done
if [ -n "$TARGET_NOTE" ] && [ "$MOVE" != "y" ]; then
	echo "Unknown argument $TARGET_NOTE" >&2
	exit 1
fi

case "$OPERATION" in
	git)
		# TODO handle git init (create initial commit otherwise errors can ocure)
		git $GIT_ARGS
		exit $?
		;;
	d|delete)
		note_delete "$NOTE"
		;;
	c|create)
		if [ -e "$NOTE" ]; then
			echo "$NOTE already exists." >&2
			exit 2
		fi
		note_create "$NOTE"
		;;
	e|edit)
		if ! [ -e "$NOTE" ]; then
			echo "$NOTE doesn't exists." >&2
			exit 3
		fi
		note_edit "$NOTE"
		;;
	m|move)
		if ! [ -e "$NOTE" ]; then
			echo "$NOTE doesn't exists." >&2
			exit 3
		fi
		if [ -e "$TARGET_NOTE" ]; then
			echo "$TARGET_NOTE already exists." >&2
			exit 2
		fi
		note_move "$NOTE" "$TARGET_NOTE"
		;;
	l|less)
		if ! [ -e "$NOTE" ]; then
			echo "$NOTE doesn't exists." >&2
			exit 3
		fi
		note_less "$NOTE"
		;;
	*)
		note_edit "$NOTE"
		;;
esac