From bbe2a9e0d412e06c7004cd41ed07744ef946a1ca Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Thu, 11 Nov 2021 23:52:42 -0500 Subject: [PATCH] add _setPath_ option to fail if dir not found --- template_standalone.sh | 27 ++++++++++++++++++++++++++- test/template_utils.bats | 7 +++++++ utilities/template_utils.bash | 27 ++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/template_standalone.sh b/template_standalone.sh index b6ee05c..7ad5a1f 100755 --- a/template_standalone.sh +++ b/template_standalone.sh @@ -390,12 +390,34 @@ _setPATH_() { # Add directories to $PATH so script can find executables # ARGS: # $@ - One or more paths - # OUTS: Adds items to $PATH + # OPTS: + # -x - Fail if directories are not found + # OUTS: + # 0: Success + # 1: Failure + # Adds items to $PATH # USAGE: # _setPATH_ "/usr/local/bin" "${HOME}/bin" "$(npm bin)" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" + local opt + local OPTIND=1 + local _failIfNotFound=false + + while getopts ":xX" opt; do + case ${opt} in + x | X) _failIfNotFound=true ;; + *) + { + error "Unrecognized option '${1}' passed to _backupFile_" "${LINENO}" + return 1 + } + ;; + esac + done + shift $((OPTIND - 1)) + local _newPath for _newPath in "$@"; do @@ -411,6 +433,9 @@ _setPATH_() { fi else debug "_setPATH_: can not find: ${_newPath}" + if [[ ${_failIfNotFound} == true ]]; then + return 1 + fi continue fi done diff --git a/test/template_utils.bats b/test/template_utils.bats index 262fa0d..f58b4f0 100755 --- a/test/template_utils.bats +++ b/test/template_utils.bats @@ -74,6 +74,13 @@ teardown() { assert_success } +@test "_setPATH_: fail on dir not found" { + mkdir -p "${TESTDIR}/testing/from/bats" + mkdir -p "${TESTDIR}/testing/from/bats_again" + run _setPATH_ -x "${TESTDIR}/testing/from/bats" "${TESTDIR}/testing/again" "${TESTDIR}/testing/from/bats_again" + assert_failure +} + @test "_setPATH_: success" { mkdir -p "${TESTDIR}/testing/from/bats" mkdir -p "${TESTDIR}/testing/from/bats_again" diff --git a/utilities/template_utils.bash b/utilities/template_utils.bash index 3836a15..5d5f4aa 100644 --- a/utilities/template_utils.bash +++ b/utilities/template_utils.bash @@ -93,12 +93,34 @@ _setPATH_() { # Add directories to $PATH so script can find executables # ARGS: # $@ - One or more paths - # OUTS: Adds items to $PATH + # OPTS: + # -x - Fail if directories are not found + # OUTS: + # 0: Success + # 1: Failure + # Adds items to $PATH # USAGE: # _setPATH_ "/usr/local/bin" "${HOME}/bin" "$(npm bin)" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" + local opt + local OPTIND=1 + local _failIfNotFound=false + + while getopts ":xX" opt; do + case ${opt} in + x | X) _failIfNotFound=true ;; + *) + { + error "Unrecognized option '${1}' passed to _backupFile_" "${LINENO}" + return 1 + } + ;; + esac + done + shift $((OPTIND - 1)) + local _newPath for _newPath in "$@"; do @@ -114,6 +136,9 @@ _setPATH_() { fi else debug "_setPATH_: can not find: ${_newPath}" + if [[ ${_failIfNotFound} == true ]]; then + return 1 + fi continue fi done