Skip to content

Commit

Permalink
Merge pull request #89 from DCsunset/master
Browse files Browse the repository at this point in the history
Add post-command hook
  • Loading branch information
pimterry authored Aug 2, 2022
2 parents aaadb8d + 703f844 commit 7959707
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ There are also more complex options available. You can set any configuration pro
* `NOTES_EXT` changes the default extension that notes are saved with.
* `NOTES_DIRECTORY` changes the directory in which notes are stored.
* `EDITOR` can also be overriden here, for `notes` only.
* `POST_COMMAND` sets the command to run after any modification command (e.g. `open`, `mv`, ...) succeeds


## How do I use it?
Expand Down
3 changes: 3 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Define the directory where notes are stored
# NOTES_DIRECTORY=~/notes

# Define command to run after modification command
# POST_COMMAND="/path/to/custom_script.sh"
13 changes: 13 additions & 0 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ EOF
main() {
local ret=0
local cmd=""
# variable to indicate whether it's a modification command
local modified=0

if [ -z "$1" ]; then
printf "No command specified\n\n"
Expand All @@ -326,6 +328,7 @@ main() {
case "$1" in
"new"|"n" )
cmd="new_note"
modified=1
;;
"ls" )
cmd="ls_notes"
Expand All @@ -341,15 +344,19 @@ main() {
;;
"open"|"o" )
cmd="handle_multiple_notes open"
modified=1
;;
"append"|"a" )
cmd="append_note"
modified=1
;;
"mv" )
cmd="move_note"
modified=1
;;
"rm" )
cmd="remove_note"
modified=1
;;
"cat" )
cmd="handle_multiple_notes cat"
Expand All @@ -370,6 +377,12 @@ main() {

$cmd "$@"
ret=$[$ret+$?]

# run POST_COMMAND hook when modification cmd succeeds
if [ $ret -eq 0 ] && [ $modified -eq 1 ] && [ -n "$POST_COMMAND" ]; then
eval "$POST_COMMAND"
fi

exit $ret
}
main "$@"
Expand Down
22 changes: 21 additions & 1 deletion test/test-config.bats
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,24 @@ notes="./notes"

assert_failure
assert_line "Could not create directory $NOTES_DIRECTORY/testfile, please update your \$NOTES_DIRECTORY"
}
}

@test "Post-command should run if it is a modification command" {
mkdir -p $HOME/.config/notes
echo 'POST_COMMAND="echo 1 > $HOME/post-output"' > $HOME/.config/notes/config
run $notes new test

assert_success
assert_exists $HOME/post-output
}

@test "Post-command should not run if it is not a modification command" {
run $notes new test

mkdir -p $HOME/.config/notes
echo 'POST_COMMAND="echo 1 > $HOME/post-output"' > $HOME/.config/notes/config
run $notes cat test

assert_success
refute_exists $HOME/post-output
}

0 comments on commit 7959707

Please sign in to comment.