-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.sh
executable file
·699 lines (567 loc) · 18.2 KB
/
note.sh
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#!/bin/sh
# Copyright © 2022-2024 Kan Vladislav <thek4n@yandex.ru>. All rights reserved.
set -ue
: "${XDG_DATA_HOME:="${HOME}/.local/share"}"
readonly CONFIGFILE="${XDG_DATA_HOME}/note/notes-storage-path"
readonly DEFAULT_PREFIX="${HOME}/.notes"
readonly RUNTIME_DIR="${HOME}/.local/state"
readonly LOCKFILE="${RUNTIME_DIR}/note/lock"
readonly LAST_EDIT_NOTE="${RUNTIME_DIR}/note/last"
readonly ORIGIN="origin"
readonly BRANCH="master"
PROGRAM="$(basename "${0}")"
readonly PROGRAM
PROGRAM_REALPATH="$(realpath "${0}")"
readonly PROGRAM_REALPATH
readonly FZF="fzf"
readonly FZF_PAGER="bat"
readonly RG="rg"
readonly GREEN='\033[0;32m'
readonly RED='\033[0;31m'
readonly YELLOW='\033[1;33m'
readonly NOCOLOR='\033[0m'
readonly OK_MESSAGE="${GREEN}OK${NOCOLOR}"
readonly WARN_MESSAGE="${YELLOW}WARN${NOCOLOR}"
readonly ERROR_MESSAGE="${RED}ERROR${NOCOLOR}"
readonly EXIT_SUCCESS=0
readonly EXIT_FAILURE=1
readonly EXIT_INVALID_ARGUMENT=2
readonly EXIT_INVALID_OPTION=3
readonly EXIT_INVALID_STATE=4
die() {
echo "${PROGRAM}: Error: ${1}" 1>&2
exit "${2}"
}
cmd_usage() {
echo "Usage:
${PROGRAM} help
Show this text
${PROGRAM} init [-p PATH] [-r REMOTE]
Initialize new note storage in PATH(default=~/.notes), if REMOTE specified, pulls notes from there
${PROGRAM} version
Print version and exit
${PROGRAM} edit|e (PATH_TO_NOTE)
Creates or edit existing note with \$VISUAL, after save changes by git
${PROGRAM} today
Creates or edit note with name like daily/06-01-24.md
${PROGRAM} last
Edit last opened note
${PROGRAM} fedit|fe
Find note by fzf and edit with \$VISUAL
${PROGRAM} fgrep|fg
Find note by content with fzf and edit with \$VISUAL
${PROGRAM} show|cat (PATH_TO_NOTE)
Show note in terminal by \$NOTEPAGER if defined, otherwice \$PAGER
${PROGRAM} rm (PATH_TO_NOTE)
Removes note
${PROGRAM} mv (PATH_TO_NOTE) (new-note-name)
Rename note
${PROGRAM} draft (PATH_TO_NOTE)
Moves note to draft directory
${PROGRAM} undraft (PATH_TO_NOTE)
Moves note from draft directory
${PROGRAM} ln (PATH_TO_NOTE) (link-name)
Create symbolic link
${PROGRAM} ls|list [PATH_TO_NOTE]...
List notes
${PROGRAM} mkdir (PATH_TO_DIR)
Creates new directory and subdirs
${PROGRAM} tree [PATH_TO_SUBDIR]
Show notes in storage or subdir
${PROGRAM} find (NOTE_NAME)
Find note with name
${PROGRAM} grep (PATTERN)
Find notes by pattern
${PROGRAM} checkhealth
Check installed dependencies and initialized storage
${PROGRAM} sync
Pull changes from remote note storage(in case of conflict, accepts yours changes)
${PROGRAM} git ...
Proxy commands to git
${PROGRAM} --prefix
Prints to stdout current notes storage
${PROGRAM} export
Export notes in tar.gz format, redirect output in stdout (use ${PROGRAM} export > notes.tar.gz)" >&2
exit "${1}"
}
cmd_version() {
echo "%%VERSION%%"
exit "${EXIT_SUCCESS}"
}
_ask_user() {
question="${1}"
default_value="${2}"
printf '%s (default=%s): ' "${question}" "${default_value}" 1>&2
read -r answer
if [ -z "${answer}" ]; then
answer="${default_value}"
fi
echo "${answer}"
}
_is_yes() {
[ "${1}" = y ]
}
_string_valid_arg() {
echo "${1}" | grep '^-' 1>/dev/null 2>&1
}
_validate_arg() {
if ! _string_valid_arg "${1}"; then
die "Option ${1} requires an argument" "${EXIT_INVALID_ARGUMENT}"
fi
}
cmd_init() {
remote_storage=""
PREFIX="${DEFAULT_PREFIX}"
while getopts ":p:r:" opt; do
case "${opt}" in
p)
_validate_arg "-${opt}" "${OPTARG}"
PREFIX="$(realpath -m "${OPTARG}")"
;;
r)
_validate_arg "-${opt}" "${OPTARG}"
remote_storage="${OPTARG}"
;;
:)
die "Option -${OPTARG} requires an argument" "${EXIT_INVALID_ARGUMENT}"
;;
\?)
die "Invalid option: -${OPTARG}" "${EXIT_INVALID_OPTION}"
;;
esac
done
mkdir -p "$(dirname "${CONFIGFILE}")"
echo "${PREFIX}" > "${CONFIGFILE}"
if [ ! -d "${PREFIX}" ]; then
mkdir "${PREFIX}"
fi
git init -b "${BRANCH}" "${PREFIX}"
if [ -n "${remote_storage}" ]; then
git -C "${PREFIX}" remote add "${ORIGIN}" "${remote_storage}"
cmd_sync
fi
exit "${EXIT_SUCCESS}"
}
__is_note_storage_initialized() {
[ -r "${CONFIGFILE}" ]
prefix="$(cat "${CONFIGFILE}")"
[ -d "${prefix}" ]
[ -w "${prefix}" ]
[ -w "${prefix}/.git" ]
}
die_if_not_initialized() {
if ! __is_note_storage_initialized; then
die "You need to initialize: ${PROGRAM} init [-p PATH]" "${EXIT_INVALID_STATE}"
fi
}
die_if_name_not_entered() {
test -n "${1}" || die "Note name wasn\`t entered" "${EXIT_INVALID_ARGUMENT}"
}
cmd_git() {
git -C "${PREFIX}" "$@"
}
git_add() {
cmd_git add "${1}"
}
git_commit() {
cmd_git commit -m "${1}" 1>/dev/null
}
_string_contain_dotdot() {
echo "${1}" | grep '\.\.' 1>/dev/null 2>&1
}
_string_starts_with_slash() {
echo "${1}" | grep '^/' 1>/dev/null 2>&1
}
die_if_invalid_path() {
if _string_contain_dotdot "${1}"; then
die "Path can\`t contains '..'" "${EXIT_INVALID_ARGUMENT}"
fi
if _string_starts_with_slash "${1}"; then
die "Path can\`t start from '/'" "${EXIT_INVALID_ARGUMENT}"
fi
}
_is_depends_installed() {
command -v "${1}" 1>/dev/null 2>&1
}
die_if_depends_not_installed() {
_is_depends_installed "${1}" || die "'${1}' not installed. Use '${PROGRAM} checkhealth'." "${EXIT_INVALID_STATE}"
}
_is_first_command_in_variable_are_program() {
command -v "${1%% *}" 1>/dev/null 2>&1
}
cmd_edit() {
if [ -z "${VISUAL+x}" ] || ! _is_first_command_in_variable_are_program "${VISUAL}"; then
if [ -z "${EDITOR+x}" ] || ! _is_first_command_in_variable_are_program "${EDITOR}"; then
die "EDITOR (${EDITOR}) is invalid" "${EXIT_INVALID_STATE}"
fi
VISUAL="${EDITOR}"
fi
die_if_name_not_entered "${1}"
die_if_invalid_path "${1}"
test -d "${PREFIX}/${1}" && die "Can\`t edit directory '${1}'" $EXIT_INVALID_ARGUMENT
if [ ! -e "${PREFIX}/${1}" ]; then
echo "Creating new note '${1}'"
_new_note_flag=true
else
_new_note_flag=false
fi
_new_dir_flag=false
_dirname="$(dirname "${1}")"
if [ ! -d "${PREFIX}/${_dirname}" ]; then
mkdir -p "${PREFIX}/${_dirname}"
_new_dir_flag=true
fi
echo "${1}" > "${LAST_EDIT_NOTE}"
${VISUAL} "${PREFIX}/${1}"
if [ -e "${PREFIX}/${1}" ]; then
if ${_new_note_flag}; then
git_add "${1}"
git_commit "Created new note ${1} by ${HOST:-${HOSTNAME:-$USER}}"
echo "Note '${1}' has been created"
elif [ -n "$(cmd_git diff "${1}")" ]; then
git_add "${1}"
git_commit "Edited note ${1} by ${HOST:-${HOSTNAME:-$USER}}"
echo "Note '${1}' has been edited"
else
echo "Note '${1}' wasn\`t edited"
fi
else
echo "New note '${1}' wasn\`t created"
if ${_new_dir_flag}; then
# removes only empty dirs recursively
cd "${PREFIX}"
rmdir -p "${_dirname}"
fi
fi
}
cmd_today() {
cmd_edit "daily/$(date "+${DATE_FMT:-%d-%m-%y}").md"
}
cmd_last() {
if [ ! -e "${LAST_EDIT_NOTE}" ] || [ -z "$(cat "${LAST_EDIT_NOTE}")" ]; then
die "No last note" "${EXIT_INVALID_STATE}"
fi
cmd_edit "$(cat "${LAST_EDIT_NOTE}")"
}
cmd_fedit() {
die_if_depends_not_installed "${FZF}"
die_if_depends_not_installed "${FZF_PAGER}"
INITIAL_QUERY="${1:-}"
export FZF_DEFAULT_OPTS="\
${FZF_DEFAULT_OPTS:-}
--no-multi
--no-sort
--preview-window right:60%
--preview=\"${FZF_PAGER} --plain --wrap=never --color=always ${PREFIX}/{}\"
--bind enter:execute\(${PROGRAM_REALPATH}\ edit\ \"{1}\"\)"
cmd_complete_notes | ${FZF} --query "${INITIAL_QUERY}"
exit "${EXIT_SUCCESS}"
}
cmd_fg() {
die_if_depends_not_installed "${FZF}"
die_if_depends_not_installed "${FZF_PAGER}"
die_if_depends_not_installed "${RG}"
INITIAL_QUERY="${1:-}"
export FZF_DEFAULT_OPTS="\
${FZF_DEFAULT_OPTS:-}
--no-multi
--no-sort
--preview-window right:40%
--preview=\"rgout={}; \
lineno=\$(echo \$rgout | awk -F: '{print \$2}'); \
${FZF_PAGER} --plain --wrap=never --color=always \
-H \$lineno \
-r \$lineno:-\$((FZF_PREVIEW_LINES/2)) \
-r \$lineno:+\$FZF_PREVIEW_LINES \
${PREFIX}/\${rgout%%:*}\""
RG_PREFIX="${RG} --column --line-number --no-heading --color=always --smart-case"
choosed_note="$(FZF_DEFAULT_COMMAND="${RG_PREFIX} '${INITIAL_QUERY}'" \
${FZF} --bind "change:reload:${RG_PREFIX} {q} || true" \
--ansi --disabled --query "${INITIAL_QUERY}")"
cmd_edit "${choosed_note%%:*}"
}
cmd_list() {
die_if_invalid_path "$*"
cd "${PREFIX}"
ls "${LS_OPTIONS:-"--color=auto"}" "$@"
}
cmd_show() {
die_if_invalid_path "${1}"
die_if_name_not_entered "${1}"
if [ -z "${NOTEPAGER+x}" ] || ! _is_first_command_in_variable_are_program "${NOTEPAGER}"; then
if [ -z "${PAGER+x}" ] || ! _is_first_command_in_variable_are_program "${PAGER}"; then
die "PAGER (${PAGER}) is invalid" "${EXIT_INVALID_STATE}"
fi
NOTEPAGER="${PAGER}"
fi
test -e "${PREFIX}/${1}" || die "Note '${1}' doesn\`t exist" "${EXIT_INVALID_ARGUMENT}"
${NOTEPAGER} "${PREFIX}/${1}"
exit "${EXIT_SUCCESS}"
}
cmd_ls() {
die_if_invalid_path "$*"
if [ -z "$*" ]; then
cmd_list
else
cmd_list "$@"
fi
exit "${EXIT_SUCCESS}"
}
cmd_mkdir() {
die_if_name_not_entered "${1}"
die_if_invalid_path "${1}"
mkdir -p "${PREFIX}/${1}"
}
cmd_tree() {
path="${1:-.}"
die_if_invalid_path "${path}"
die_if_depends_not_installed "tree"
test -d "${PREFIX}/${path}" || die "'${path}' not a directory" "${EXIT_INVALID_ARGUMENT}"
cd "${PREFIX}"
tree -N -C --noreport "${path}"
exit "${EXIT_SUCCESS}"
}
cmd_delete() {
die_if_invalid_path "${1}"
die_if_name_not_entered "${1}"
test -e "${PREFIX}/${1}" || die "Note '${1}' doesn\`t exist" "${EXIT_INVALID_ARGUMENT}"
cmd_git rm -r "${1}"
git_commit "Removed note ${1}"
}
cmd_rename() {
die_if_invalid_path "${1}"
die_if_invalid_path "${2}"
die_if_name_not_entered "${1}"
die_if_name_not_entered "${2}"
test -e "${PREFIX}/${1}" || die "Note or directory '${1}' doesn\`t exist" "$EXIT_INVALID_ARGUMENT"
test -f "${PREFIX}/${2}" && die "Note '${2}' already exists" "$EXIT_INVALID_ARGUMENT"
_dirname="$(dirname "${2}")"
if [ ! -d "${PREFIX}/${_dirname}" ]; then
mkdir -p "${PREFIX}/${_dirname}"
fi
mv "${PREFIX}/${1}" "${PREFIX}/${2}"
git_add "${1}"
git_add "${2}"
git_commit "Note ${1} renamed to ${2}"
}
cmd_draft() {
cmd_rename "${1}" "draft/${1}"
}
cmd_undraft() {
cmd_rename "draft/${1}" "${1}"
}
cmd_ln() {
die_if_invalid_path "${1}"
die_if_invalid_path "${2}"
die_if_name_not_entered "${1}"
die_if_name_not_entered "${2}"
test -e "${PREFIX}/${1}" || die "Note or directory '${1}' doesn\`t exist" "${EXIT_INVALID_ARGUMENT}"
test -f "${PREFIX}/${2}" && die "Note or directory '${2}' already exists" "${EXIT_INVALID_ARGUMENT}"
ln -s "${PREFIX}/${1}" "${PREFIX}/${2}"
git_add "${2}"
git_commit "Created symlink ${2} to note ${1}"
}
cmd_find() {
die_if_depends_not_installed "find"
find "${PREFIX}" \( -name .git -o -name '.img*' \) -prune -o -iname "${1}" -print | _exclude_prefix
exit "${EXIT_SUCCESS}"
}
cmd_grep() {
grep "${1}" "${PREFIX}" -rH --color=always --exclude-dir=".git" --exclude-dir=".img" | _exclude_prefix
exit "${EXIT_SUCCESS}"
}
cmd_export() {
tar -C "${PREFIX}" -czf - .
}
_highlight_text() {
nocolor="$(printf '\e[0m')"
sed -e "s/${1}/${2}${1}${nocolor}/g"
}
cmd_sync() {
ff="Fast-forward"
merge="Merge"
red="$(printf '\e[31m')"
green="$(printf '\e[32m')"
output="$(cmd_git pull "${ORIGIN}" "${BRANCH}" --strategy-option ours --no-rebase --no-edit)"
printf '%s\n' "${output}" | \
_highlight_text "${ff}" "${green}" | \
_highlight_text "${merge}" "${red}"
if echo "${output}" | grep "${merge}" 1>/dev/null; then
if _is_yes "$(_ask_user "[?] Merge detected! Push merge-commit? [N/y]" "y")"; then
cmd_git push
fi
fi
}
_exclude_prefix() {
sed -e "s#${PREFIX}/\{0,1\}##"
}
_format_and_sort_completions() {
_exclude_prefix | sed '/^$/d' | sort
}
_find_notes_to_complete() {
die_if_depends_not_installed "find"
find "${PREFIX}" \( -name .git -o -name '.img*' \) -prune -o ${1} -print | _format_and_sort_completions
}
__error_if_storage_not_initialized() {
if __is_note_storage_initialized; then
printf "${OK_MESSAGE}\n"
else
printf "${ERROR_MESSAGE}\n"
fi
}
__error_if_invalid_VISUAL_variable() {
if [ -n "${VISUAL+x}" ] && _is_depends_installed "${VISUAL}"; then
printf "${OK_MESSAGE}\n"
else
printf "${ERROR_MESSAGE}\n"
fi
}
__error_if_depends_not_installed() {
if _is_depends_installed "${1}"; then
printf "${OK_MESSAGE}\n"
else
printf "${ERROR_MESSAGE}\n"
fi
}
__warn_if_depends_not_installed() {
if _is_depends_installed "${1}"; then
printf "${OK_MESSAGE}\n"
else
printf "${WARN_MESSAGE}\n"
fi
}
cmd_checkhealth() {
printf "%s\n" "Is note storage initialized?... $(__error_if_storage_not_initialized)"
printf '%s\n' "Is variable \$VISUAL valid?... $(__error_if_invalid_VISUAL_variable)"
printf '%s\n' "Is dependencies installed?..."
printf '\t%s\n' "git $(__error_if_depends_not_installed git)"
printf '%s\n' "Is optional dependencies installed?..."
printf '\t%s\n' "${FZF} $(__warn_if_depends_not_installed "${FZF}")"
printf '\t%s\n' "${FZF_PAGER} $(__warn_if_depends_not_installed "${FZF_PAGER}")"
printf '\t%s\n' "${RG} $(__warn_if_depends_not_installed "${RG}")"
printf '\t%s\n' "tree $(__warn_if_depends_not_installed tree)"
printf '\t%s\n' "find $(__warn_if_depends_not_installed find)"
exit "${EXIT_SUCCESS}"
}
cmd_complete_notes() {
_find_notes_to_complete '-type f'
}
cmd_complete_subdirs() {
_find_notes_to_complete '-type d'
}
cmd_complete_files() {
_find_notes_to_complete '-type f,d'
}
complete_commands() {
echo "init:Initialize new note storage in ~/.notes
edit:Creates or edit existing note with \$VISUAL
e:Creates or edit existing note with \$VISUAL (alias)
today:Creates or edit note with name like daily/06-01-24.md
last:edit last opened note
fedit:Find note by fzf and edit with \$VISUAL
fe:Find note by fzf and edit with \$VISUAL (alias)
fgrep:Find note by content with fzf and edit with \$VISUAL
fg:Find note by content with fzf and edit with \$VISUAL (alias)
show:Render note in terminal by \$NOTEPAGER if defined, otherwice \$PAGER
cat:Render note in terminal by \$PAGER (alias)
rm:Remove note
mv:Rename note
draft:Moves note to draft directory
undraft:Moves note from draft directory
ln:Create symbolic link
list:List notes
ls:List notes (alias)
export:Export notes in tar.gz format, redirect output in stdout
tree:Show tree of notes
find:Find note by name
grep:Find notes by pattern
mkdir:Creates directory
sync:Pull changes from remote note storage(in case of conflict, accepts yours changes)
git:Proxy commands to git
--prefix:Prints to stdout current notes storage
checkhealth:Check installed dependencies and initialized storage"
}
cmd_complete_bash_commands() {
IFS=';'
for cmd in $(complete_commands | tr '\n' ';')
do
echo "${cmd}" | cut -f1 -d":"
done
}
cmd_complete_zsh_commands() {
complete_commands | tr "\n" ";" | head --bytes -1
}
cmd_get_storage() {
echo "${PREFIX}"
exit "${EXIT_SUCCESS}"
}
cmd_complete() {
case "${1}" in
edit|e|fe|show|cat) shift; cmd_complete_notes "$@" ;;
tree|mkdir) shift; cmd_complete_subdirs "$@" ;;
mv|rm|ls|list) shift; cmd_complete_files "$@" ;;
bash) shift; cmd_complete_bash_commands "$@" ;;
zsh) shift; cmd_complete_zsh_commands "$@" ;;
esac
exit "${EXIT_SUCCESS}"
}
_die_if_locked() {
if [ -e "${LOCKFILE}" ]; then
die "Seems another process is running. If not, just delete ${LOCKFILE}" "${EXIT_INVALID_STATE}"
fi
}
_set_lock() {
mkdir -p "$(dirname "${LOCKFILE}")"
touch "${LOCKFILE}"
}
_release_lock() {
rm "${LOCKFILE}"
}
_is_repository_not_clean() {
[ -n "$(cmd_git status -s)" ]
}
if [ -z "${1+x}" ]; then
die "Type '${PROGRAM} help' for usage" "${EXIT_FAILURE}"
fi
case "${1}" in
init) shift; cmd_init "$@" ;;
help|--help|-h) shift; cmd_usage "${EXIT_SUCCESS}" "$@" ;;
version|-V) shift; cmd_version "$@" ;;
checkhealth) shift; cmd_checkhealth "$@" ;;
esac
die_if_not_initialized
PREFIX="$(cat "${CONFIGFILE}")"
cd "${PREFIX}"
if _is_repository_not_clean; then
echo "${PROGRAM}: WARNING: repository not clean!" 1>&2
fi
case "${1}" in
show|cat) shift; cmd_show "$@" ;;
list|ls) shift; cmd_ls "$@" ;;
tree) shift; cmd_tree "$@" ;;
find) shift; cmd_find "$@" ;;
grep) shift; cmd_grep "$@" ;;
fedit|fe) shift; cmd_fedit "$@" ;;
complete) shift; cmd_complete "$@" ;;
--prefix) shift; cmd_get_storage "$@" ;;
esac
_die_if_locked
_set_lock
trap _release_lock EXIT INT HUP
case "${1}" in
edit|e) shift; cmd_edit "$@" ;;
today) shift; cmd_today "$@" ;;
fgrep|fg) shift; cmd_fg "$@" ;;
last) shift; cmd_last "$@" ;;
rm) shift; cmd_delete "$@" ;;
mv) shift; cmd_rename "$@" ;;
draft) shift; cmd_draft "$@" ;;
undraft) shift; cmd_undraft "$@" ;;
ln) shift; cmd_ln "$@" ;;
mkdir) shift; cmd_mkdir "$@" ;;
export) shift; cmd_export "$@" ;;
sync) shift; cmd_sync "$@" ;;
git) shift; cmd_git "$@" ;;
*) cmd_usage "${EXIT_FAILURE}" "$@" ;;
esac
exit "${EXIT_SUCCESS}"