Use die() / print to stderr for error conditions

To indicate that something went wrong (e.g. the task already was unprioritized).
Note: For actions that handle multiple ITEMs in a loop, we cannot use die() as that would abort processing of any following ITEM(s). Instead, use a status variable and set it to 1 on error, then exit at the end.
This commit is contained in:
Ingo Karkat
2023-01-21 19:01:24 +01:00
parent 803881998f
commit ef419f3594
7 changed files with 26 additions and 10 deletions

23
todo.sh
View File

@@ -1170,7 +1170,7 @@ case $action in
echo "TODO: $item deleted."
fi
else
echo "TODO: No tasks were deleted."
die "TODO: No tasks were deleted."
fi
else
sed -i.bak \
@@ -1200,6 +1200,7 @@ case $action in
# Split multiple depri's, if comma separated change to whitespace separated
# Loop the 'depri' function for each item
status=0
for item in ${*//,/ }; do
getTodo "$item"
@@ -1211,9 +1212,11 @@ case $action in
echo "TODO: $item deprioritized."
fi
else
echo "TODO: $item is not prioritized."
echo >&2 "TODO: $item is not prioritized."
status=1
fi
done
exit $status
;;
"do" | "done" )
@@ -1224,6 +1227,7 @@ case $action in
# Split multiple do's, if comma separated change to whitespace separated
# Loop the 'do' function for each item
status=0
for item in ${*//,/ }; do
getTodo "$item"
@@ -1239,15 +1243,17 @@ case $action in
echo "TODO: $item marked as done."
fi
else
echo "TODO: $item is already marked done."
echo >&2 "TODO: $item is already marked done."
status=1
fi
done
if [ $TODOTXT_AUTO_ARCHIVE = 1 ]; then
# Recursively invoke the script to allow overriding of the archive
# action.
"$TODO_FULL_SH" archive
"$TODO_FULL_SH" archive || status=$?
fi
exit $status
;;
"help" )
@@ -1363,7 +1369,7 @@ case $action in
echo "TODO: $item moved from '$src' to '$dest'."
fi
else
echo "TODO: No tasks moved."
die "TODO: No tasks moved."
fi
;;
@@ -1374,6 +1380,7 @@ case $action in
"pri" | "p" )
shift
status=0
while [ "$#" -gt 0 ] ; do
item=$1
newpri=$( printf "%s\n" "$2" | tr '[:lower:]' '[:upper:]' )
@@ -1405,10 +1412,12 @@ note: PRIORITY must be anywhere from A to Z."
fi
fi
if [ "$oldpri" = "$newpri" ]; then
echo "TODO: $item already prioritized ($newpri)."
echo >&2 "TODO: $item already prioritized ($newpri)."
status=1
fi
shift; shift
done
exit $status
;;
"replace" )
@@ -1476,7 +1485,7 @@ note: PRIORITY must be anywhere from A to Z."
newTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' )
deduplicateNum=$(( originalTaskNum - newTaskNum ))
if [ $deduplicateNum -eq 0 ]; then
echo "TODO: No duplicate tasks found"
die "TODO: No duplicate tasks found"
else
echo "TODO: $deduplicateNum duplicate task(s) removed"
fi