mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-10 14:13:45 -05:00
add _setPath_ option to fail if dir not found
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user