Skip to content

Commit

Permalink
FIX: Default action with arguments does not consider built-in commands
Browse files Browse the repository at this point in the history
Only $TODO_ACTIONS_DIR/$action, not even the $TODO_ACTIONS_DIR/$action/$action variant.
A totally different approach is needed: Instead of parsing off the first argument and treating that as the custom action, do a recursive invocation with the default action parsed as a command-line. A new isDefaultAction flag is needed so that we only do this when the default action actually applies (otherwise, strange invocations like todo.sh "add +project here" would also be possible). The condition for non-empty $TODOTXT_DEFAULT_ACTION avoids an endless loop.
  • Loading branch information
inkarkat committed Apr 3, 2023
1 parent 18479f9 commit 780c114
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
15 changes: 15 additions & 0 deletions tests/t0002-actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,28 @@ test_expect_success 'custom action (default action)' '
test_cmp expect output && rm -rf .todo.actions.d
'

test_todo_session 'default built-in action with multiple arguments' <<EOF
>>> TODOTXT_DEFAULT_ACTION='add +foo @bar baz' todo.sh
1 +foo @bar baz
TODO: 1 added.
EOF

test_todo_session 'default custom action with multiple arguments' <<EOF
>>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/
>>> TODOTXT_DEFAULT_ACTION='foo2 foo bar baz' todo.sh
TODO: foo-bar-baz
EOF

: > todo.txt
export TODOTXT_DEFAULT_ACTION="add foo\\ bar \\\$HOSTNAME O\\'Really\\? \\\"quoted\\\""
test_todo_session 'default built-in action with arguments that have special characters' <<EOF
>>> todo.sh
1 foo bar \$HOSTNAME O'Really? "quoted"
TODO: 1 added.
EOF

: > todo.txt
export TODOTXT_DEFAULT_ACTION="foo2 foo\\ bar \\\$HOSTNAME O\\'Really\\? \\\"quoted\\\""
test_todo_session 'default custom action with arguments that have special characters' <<EOF
>>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/
Expand Down
21 changes: 11 additions & 10 deletions todo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,13 @@ if [ -n "$OVR_TODOTXT_FINAL_FILTER" ] ; then
TODOTXT_FINAL_FILTER="$OVR_TODOTXT_FINAL_FILTER"
fi

ACTION=${1:-$TODOTXT_DEFAULT_ACTION}
isDefaultAction=
if [ -n "$1" ]; then
ACTION=$1
else
ACTION=$TODOTXT_DEFAULT_ACTION
isDefaultAction=t
fi

[ -z "$ACTION" ] && usage
[ -d "$TODO_DIR" ] || mkdir -p $TODO_DIR 2> /dev/null || dieWithHelp "$1" "Fatal Error: $TODO_DIR is not a directory"
Expand Down Expand Up @@ -982,15 +988,10 @@ elif [ -d "$TODO_ACTIONS_DIR" -a -x "$TODO_ACTIONS_DIR/$action" ]
then
"$TODO_ACTIONS_DIR/$action" "$@"
exit $?
else
## Use eventually given parameters
eval "actionarray=($ACTION)" # Note: Need to use original $ACTION to avoid that arguments are getting lowercased, too.
action=$( printf "%s\n" "${actionarray[0]}" | tr 'A-Z' 'a-z' )
if [ -d "$TODO_ACTIONS_DIR" -a -x "$TODO_ACTIONS_DIR/$action" ]
then
"$TODO_ACTIONS_DIR/$action" "${actionarray[@]}"
exit $?
fi
elif [ "$isDefaultAction" ] && [ -n "$TODOTXT_DEFAULT_ACTION" ]; then
# Recursive invocation with the contents of the default action parsed as a
# command-line.
eval "exec \"\${BASH_SOURCE[0]}\" $TODOTXT_DEFAULT_ACTION"
fi

## Only run if $action isn't found in .todo.actions.d
Expand Down

0 comments on commit 780c114

Please sign in to comment.