From 8c03b886c4d4b9bcfb1cb30b3704b8af561c2f45 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sun, 1 Oct 2023 06:33:43 +0100 Subject: Greatly simplify error handling in shell scripts Instead of having detailed error messages, run most commands through a function that calls err() under fault conditions. Where detail is still required, err() is still called manually. Where it isn't, the error message is simply whatever command was executed to cause the error. This results in a massive sloccount reduction for lbmk; specifically, 178 sloc reduction, or a 8.1% reduction. The total sloccount is now 2022, for shell scripts. Signed-off-by: Leah Rowe --- lbmk | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'lbmk') diff --git a/lbmk b/lbmk index 395cd130..5dc59b9c 100755 --- a/lbmk +++ b/lbmk @@ -21,9 +21,7 @@ option="" main() { - id -u 1>/dev/null 2>/dev/null || \ - fail "cannot ascertain user id" - + xx_ id -u 1>/dev/null 2>/dev/null initialise_command $@ shift 2 @@ -40,8 +38,7 @@ initialise_command() [ $# -lt 1 ] && fail "Too few arguments. Try: ${0} help" mode="${1}" - [ "${mode}" != "dependencies" ] || \ - install_packages $@ || fail "Can't install dependencies" + [ "${mode}" != "dependencies" ] || xx_ install_packages $@ [ "$(id -u)" != "0" ] || \ fail "running this command as root is not permitted" @@ -68,8 +65,7 @@ install_packages() aur_notice="" . "config/dependencies/${2}" - ${pkg_add} ${pkglist} || \ - fail "install_packages: Error installing dependencies" + xx_ ${pkg_add} ${pkglist} [ "${aur_notice}" = "" ] || \ printf "You must install AUR packages: %s\n" "${aur_notice}" 1>&2 @@ -79,8 +75,7 @@ install_packages() execute_command() { if [ "${option}" = "list" ]; then - listitems "${buildpath}/${mode}" || \ - fail "execute_command: cannot list command options" + xx_ listitems "${buildpath}/${mode}" lbmk_exit 0 fi [ -f "${buildpath}/${mode}/${option}" ] || \ -- cgit v1.2.1