Skip to content

Commit 7959707

Browse files
authored
Merge pull request #89 from DCsunset/master
Add post-command hook
2 parents aaadb8d + 703f844 commit 7959707

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ There are also more complex options available. You can set any configuration pro
9797
* `NOTES_EXT` changes the default extension that notes are saved with.
9898
* `NOTES_DIRECTORY` changes the directory in which notes are stored.
9999
* `EDITOR` can also be overriden here, for `notes` only.
100+
* `POST_COMMAND` sets the command to run after any modification command (e.g. `open`, `mv`, ...) succeeds
100101

101102

102103
## How do I use it?

config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313

1414
# Define the directory where notes are stored
1515
# NOTES_DIRECTORY=~/notes
16+
17+
# Define command to run after modification command
18+
# POST_COMMAND="/path/to/custom_script.sh"

notes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ EOF
316316
main() {
317317
local ret=0
318318
local cmd=""
319+
# variable to indicate whether it's a modification command
320+
local modified=0
319321

320322
if [ -z "$1" ]; then
321323
printf "No command specified\n\n"
@@ -326,6 +328,7 @@ main() {
326328
case "$1" in
327329
"new"|"n" )
328330
cmd="new_note"
331+
modified=1
329332
;;
330333
"ls" )
331334
cmd="ls_notes"
@@ -341,15 +344,19 @@ main() {
341344
;;
342345
"open"|"o" )
343346
cmd="handle_multiple_notes open"
347+
modified=1
344348
;;
345349
"append"|"a" )
346350
cmd="append_note"
351+
modified=1
347352
;;
348353
"mv" )
349354
cmd="move_note"
355+
modified=1
350356
;;
351357
"rm" )
352358
cmd="remove_note"
359+
modified=1
353360
;;
354361
"cat" )
355362
cmd="handle_multiple_notes cat"
@@ -370,6 +377,12 @@ main() {
370377

371378
$cmd "$@"
372379
ret=$[$ret+$?]
380+
381+
# run POST_COMMAND hook when modification cmd succeeds
382+
if [ $ret -eq 0 ] && [ $modified -eq 1 ] && [ -n "$POST_COMMAND" ]; then
383+
eval "$POST_COMMAND"
384+
fi
385+
373386
exit $ret
374387
}
375388
main "$@"

test/test-config.bats

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,24 @@ notes="./notes"
5757

5858
assert_failure
5959
assert_line "Could not create directory $NOTES_DIRECTORY/testfile, please update your \$NOTES_DIRECTORY"
60-
}
60+
}
61+
62+
@test "Post-command should run if it is a modification command" {
63+
mkdir -p $HOME/.config/notes
64+
echo 'POST_COMMAND="echo 1 > $HOME/post-output"' > $HOME/.config/notes/config
65+
run $notes new test
66+
67+
assert_success
68+
assert_exists $HOME/post-output
69+
}
70+
71+
@test "Post-command should not run if it is not a modification command" {
72+
run $notes new test
73+
74+
mkdir -p $HOME/.config/notes
75+
echo 'POST_COMMAND="echo 1 > $HOME/post-output"' > $HOME/.config/notes/config
76+
run $notes cat test
77+
78+
assert_success
79+
refute_exists $HOME/post-output
80+
}

0 commit comments

Comments
 (0)