From e875197950cd83827754f18dd5a1ee058d915f2e Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Wed, 29 Sep 2021 15:33:16 +0200 Subject: [PATCH] TT#142950 scripts: Fix options parsing Switch all code to use a loop to parse options so that these can parse multiple options (currently some scripts would simply parse one or another but not several), and to make them easier to extend. Change-Id: I5e902f2d7d8db78e66e48db1237924fa7d8fdda1 --- scripts/build | 5 +++-- scripts/del | 35 +++++++++++++++++++++-------------- scripts/patch | 38 ++++++++++++++++++++++++-------------- scripts/services | 21 ++++++++++++++++----- scripts/set | 35 +++++++++++++++++++++-------------- 5 files changed, 85 insertions(+), 49 deletions(-) diff --git a/scripts/build b/scripts/build index 82cefa69..c16a66c9 100755 --- a/scripts/build +++ b/scripts/build @@ -21,13 +21,14 @@ fi MODIFIED_ONLY=false check_args=() -if [ -n "${1:-}" ] ; then +while [ -n "${1:-}" ] ; do case "$1" in *--modified-only*) MODIFIED_ONLY=true ; shift ;; *--ignore-branch-check*) check_args+=( --ignore-branch-check ) ; shift ;; *--ignore-shared-storage-check*) check_args+=( --ignore-shared-storage-check ) ; shift ;; + *) break ;; esac -fi +done # Sanity check the YAML and schema files. # NOTE: we can't blindly pass "$@" to the check script, diff --git a/scripts/del b/scripts/del index 37478738..9d20ba93 100755 --- a/scripts/del +++ b/scripts/del @@ -31,24 +31,31 @@ help() { RC=0 b_show_diff=false -if [ "${#:-}" == "2" ]; then - file="$1" - option="$2" -elif [ "${#:-}" == "3" ]; then - if [ "$1" == "--diff" ]; then - b_show_diff=true - else - log_error "unsupported option '$1'. Exiting." - help >&2 - exit 1 - fi - file="$2" - option="$3" -else +while [ -n "${1:-}" ]; do + case "$1" in + --diff) + b_show_diff=true + shift + ;; + --*) + log_error "unsupported option '$1'. Exiting." + help >&2 + exit 1 + ;; + *) + break + ;; + esac +done + +if [ "${#:-}" != "2" ]; then help >&2 exit 1 fi +file="$1" +option="$2" + [ -f "${file}" ] || (log_error "missing ${file}. Exiting." ; exit 1) [ -z "${option}" ] && ( log_error "missing option to set. Exiting." ; exit 1) log_debug "Deleting option '${option}' from '${file}'" diff --git a/scripts/patch b/scripts/patch index 4e4b8765..04ec0b4a 100755 --- a/scripts/patch +++ b/scripts/patch @@ -286,20 +286,30 @@ patch_footer() { declare -a bad_files=() declare -a good_files=() -case "${1:-}" in - "--help") - patch_help - ;; - "--from-customtt") - shift # removing '--from-customtt' - patch_import "$@" - patch_footer "customtt" - ;; - *) - patch_patch "$@" - patch_footer "patchtt" - ;; -esac +patch_type=patchtt + +while [ -n "${1:-}" ] ; do + case "${1:-}" in + --help) + patch_help + exit 0 + ;; + --from-customtt) + patch_type=customtt + shift + ;; + *) + break + ;; + esac +done + +if [ "${patch_type}" = 'patchtt' ]; then + patch_patch "$@" +else + patch_import "$@" +fi +patch_footer "${patch_type}" exit "${RC:-0}" diff --git a/scripts/services b/scripts/services index e32351c2..e147bf61 100755 --- a/scripts/services +++ b/scripts/services @@ -229,11 +229,22 @@ RUNNING_FILE="${RUN_DIR}/ngcpcfg-services.running" DRYRUN='false' FORCE_ALL_SERVICES='false' -if [[ "${1:-}" == "test" ]] || [[ "${1:-}" == "--dry-run" ]]; then - DRYRUN='true' -elif [[ "${1:-}" == "--force-all-services" ]]; then - FORCE_ALL_SERVICES='true' -fi +while [ -n "${1:-}" ]; do + case "$1" in + test|--dry-run) + DRYRUN='true' + shift + ;; + --force-all-services) + FORCE_ALL_SERVICES='true' + shift + ;; + *) + break + ;; + esac +done + log_debug "DRYRUN = ${DRYRUN}" log_debug "FORCE_ALL_SERVICES = ${FORCE_ALL_SERVICES}" diff --git a/scripts/set b/scripts/set index aee2e1cf..3309a681 100755 --- a/scripts/set +++ b/scripts/set @@ -31,24 +31,31 @@ help() { RC=0 b_show_diff=false -if [ "${#:-}" == "2" ]; then - file="$1" - data="$2" -elif [ "${#:-}" == "3" ]; then - if [ "$1" == "--diff" ]; then - b_show_diff=true - else - log_error "unsupported option '$1'. Exiting." - help >&2 - exit 1 - fi - file="$2" - data="$3" -else +while [ -n "${1:-}" ]; do + case "$1" in + --diff) + b_show_diff=true + shift + ;; + --*) + log_error "unsupported option '$1'. Exiting." + help >&2 + exit 1 + ;; + *) + break + ;; + esac +done + +if [ "${#:-}" != "2" ]; then help >&2 exit 1 fi +file="$1" +data="$2" + [ -f "${file}" ] || (log_error "missing ${file}. Exiting." ; exit 1) [ -z "${data}" ] && (log_error "missing data to set. Exiting." ; exit 1)