From 1b09b1527744a50baf4e0cd45aacced461e2d862 Mon Sep 17 00:00:00 2001 From: Lyndon Brown Date: Thu, 12 Mar 2020 20:12:47 +0000 Subject: stagefiles: refactor Require_stagefile() - count of params is available as $#, we don't need the pipe-to-wc logic. - the whole 'CONTINUE' based logic is silly, we can just return once one of the files is found. - setting of 'NAME' in the loop was completely pointless. - the error message for multiple files was not very clear just injecting a sequence of words into a sentence. - it did not work correctly if no arguments were given (bad usage) note, you might question whether the functionality of this function is correct, as did I; this is tackled in a followup commit whilst this commit retains the existing functionality! Gbp-Dch: Short --- functions/stagefile.sh | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) (limited to 'functions') diff --git a/functions/stagefile.sh b/functions/stagefile.sh index 6f9e8cc8b..383df2c45 100755 --- a/functions/stagefile.sh +++ b/functions/stagefile.sh @@ -63,37 +63,28 @@ Remove_stagefile () rm -f "${FILE}" } +# Find at least one of the required stages, `exit 1` if none found Require_stagefile () { - local NAME - local FILES - local NUMBER - NAME="$(basename ${0})" - FILES="${@}" #must be on separate line to 'local' declaration to avoid error - NUMBER="$(echo ${@} | wc -w)" - + if [ $# -eq 0 ]; then + Echo_warning "Bad `Require_stagefile` usage, no params were supplied" + return 0 + fi local FILE - local CONTINUE=false - for FILE in ${FILES} - do - FILE=".build/${FILE}" - # Find at least one of the required stages - if [ -f ${FILE} ] - then - CONTINUE=true - NAME="${NAME} $(basename ${FILE})" + for FILE in ${@}; do + if [ -f ".build/${FILE}" ]; then + return 0 fi done - if ! $CONTINUE - then - if [ "${NUMBER}" -eq 1 ] - then - Echo_error "%s: %s missing" "${NAME}" "${FILE}" - else - Echo_error "%s: one of %s is missing" "${NAME}" "${FILES}" - fi + local NAME + NAME="$(basename ${0})" - exit 1 + if [ $# -eq 1 ]; then + Echo_error "%s requires stage: %s" "${NAME}" "${FILE}" + else + Echo_error "%s requires one of these stages: %s" "${NAME}" "$(echo ${@})" fi + + exit 1 } -- cgit v1.2.3