Skip to content

Commit

Permalink
introduce checkArgIsVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
robstoll committed Apr 16, 2024
1 parent 7d87a14 commit 2ed5961
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1317,11 +1317,13 @@ function foo() {
local -rn arr=$1
local -r fn=$2
local -r bool=$3
local -r version=$4
# resolves arr recursively via recursiveDeclareP and check that is a non-associative array
checkArgIsArray arr 1 # same as exitIfArgIsNotArray if set -e has an effect on this line
checkArgIsFunction "$fn" 2 # same as exitIfArgIsNotFunction if set -e has an effect on this line
checkArgIsBoolean "$bool" 3 # same as exitIfArgIsNotBoolean if set -e has an effect on this line
checkArgIsArray arr 1 # same as exitIfArgIsNotArray if set -e has an effect on this line
checkArgIsFunction "$fn" 2 # same as exitIfArgIsNotFunction if set -e has an effect on this line
checkArgIsBoolean "$bool" 3 # same as exitIfArgIsNotBoolean if set -e has an effect on this line
checkArgIsVersion "$version" 4 # same as exitIfArgIsNotVersion if set -e has an effect on this line
# shellcheck disable=SC2317 # is passed by name to checkArgIsArrayWithTuples
function describeTriple() {
Expand All @@ -1333,7 +1335,8 @@ function foo() {
exitIfArgIsNotArray arr 1
exitIfArgIsNotArrayOrIsEmpty arr 1
exitIfArgIsNotFunction "$fn" 2
exitIfArgIsNotBoolean "$bool" 2
exitIfArgIsNotBoolean "$bool" 3
exitIfArgIsNotVersion "$version" 4
# shellcheck disable=SC2317 # is passed by name to exitIfArgIsNotArrayWithTuples
function describePair() {
Expand Down
11 changes: 7 additions & 4 deletions src/utility/checks.doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ function foo() {
local -rn arr=$1
local -r fn=$2
local -r bool=$3
local -r version=$4

# resolves arr recursively via recursiveDeclareP and check that is a non-associative array
checkArgIsArray arr 1 # same as exitIfArgIsNotArray if set -e has an effect on this line
checkArgIsFunction "$fn" 2 # same as exitIfArgIsNotFunction if set -e has an effect on this line
checkArgIsBoolean "$bool" 3 # same as exitIfArgIsNotBoolean if set -e has an effect on this line
checkArgIsArray arr 1 # same as exitIfArgIsNotArray if set -e has an effect on this line
checkArgIsFunction "$fn" 2 # same as exitIfArgIsNotFunction if set -e has an effect on this line
checkArgIsBoolean "$bool" 3 # same as exitIfArgIsNotBoolean if set -e has an effect on this line
checkArgIsVersion "$version" 4 # same as exitIfArgIsNotVersion if set -e has an effect on this line

# shellcheck disable=SC2317 # is passed by name to checkArgIsArrayWithTuples
function describeTriple() {
Expand All @@ -28,7 +30,8 @@ function foo() {
exitIfArgIsNotArray arr 1
exitIfArgIsNotArrayOrIsEmpty arr 1
exitIfArgIsNotFunction "$fn" 2
exitIfArgIsNotBoolean "$bool" 2
exitIfArgIsNotBoolean "$bool" 3
exitIfArgIsNotVersion "$version" 4

# shellcheck disable=SC2317 # is passed by name to exitIfArgIsNotArrayWithTuples
function describePair() {
Expand Down
48 changes: 38 additions & 10 deletions src/utility/checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
# local -rn arr=$1
# local -r fn=$2
# local -r bool=$3
# local -r version=$4
#
# # resolves arr recursively via recursiveDeclareP and check that is a non-associative array
# checkArgIsArray arr 1 # same as exitIfArgIsNotArray if set -e has an effect on this line
# checkArgIsFunction "$fn" 2 # same as exitIfArgIsNotFunction if set -e has an effect on this line
# checkArgIsBoolean "$bool" 3 # same as exitIfArgIsNotBoolean if set -e has an effect on this line
# checkArgIsArray arr 1 # same as exitIfArgIsNotArray if set -e has an effect on this line
# checkArgIsFunction "$fn" 2 # same as exitIfArgIsNotFunction if set -e has an effect on this line
# checkArgIsBoolean "$bool" 3 # same as exitIfArgIsNotBoolean if set -e has an effect on this line
# checkArgIsVersion "$version" 4 # same as exitIfArgIsNotVersion if set -e has an effect on this line
#
# # shellcheck disable=SC2317 # is passed by name to checkArgIsArrayWithTuples
# function describeTriple() {
Expand All @@ -44,7 +46,8 @@
# exitIfArgIsNotArray arr 1
# exitIfArgIsNotArrayOrIsEmpty arr 1
# exitIfArgIsNotFunction "$fn" 2
# exitIfArgIsNotBoolean "$bool" 2
# exitIfArgIsNotBoolean "$bool" 3
# exitIfArgIsNotVersion "$version" 4
#
# # shellcheck disable=SC2317 # is passed by name to exitIfArgIsNotArrayWithTuples
# function describePair() {
Expand Down Expand Up @@ -112,12 +115,12 @@ function exitIfArgIsNotArray() {

function exitIfArgIsNotArrayOrIsEmpty() {
exitIfArgIsNotArray "$@"
local -rn exitIfArgIsNotArrayOrIsEmpty_arr=$1
local -r argNumberOrName=$2
shift 2 || die "could not shift by 2"
if [[ ${#exitIfArgIsNotArrayOrIsEmpty_arr[@]} -lt 1 ]]; then
die "the passed argument \033[0;36m%s\033[0m is an empty array" "${!checkArgIsArray_arr}"
fi
local -rn exitIfArgIsNotArrayOrIsEmpty_arr=$1
local -r argNumberOrName=$2
shift 2 || die "could not shift by 2"
if [[ ${#exitIfArgIsNotArrayOrIsEmpty_arr[@]} -lt 1 ]]; then
die "the passed argument \033[0;36m%s\033[0m is an empty array" "${!checkArgIsArray_arr}"
fi
}

function checkArgIsArrayWithTuples() {
Expand Down Expand Up @@ -243,6 +246,31 @@ function exitIfArgIsNotBoolean() {
# shellcheck disable=SC2310 # we are aware of that || will disable set -e for checkArgIsBoolean
checkArgIsBoolean "$@" || exit $?
}

function exitIfArgIsNotVersion() {
# shellcheck disable=SC2310 # we are aware of that || will disable set -e for checkArgIsVersion
checkArgIsVersion "$@" || exit $?
}

function checkArgIsVersion() {
local versionRegex
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"

local value argNumberOrName
# shellcheck disable=SC2034 # is passed by name to parseFnArgs
local -ra params=(value argNumberOrName)
parseFnArgs params "$@"

if ! [[ "$value" =~ $versionRegex ]]; then
local funcName=${FUNCNAME[1]}
if [[ $funcName == "exitIfArgIsNotVersion" ]]; then
funcName=${FUNCNAME[2]}
fi
traceAndReturnDying "the %s argument to %s needs to match vX.Y.Z(-RC...) was %s" \
"$argNumberOrName" "$funcName" "$value"
fi
}

function checkCommandExists() {
if ! (($# == 1 || $# == 2)); then
traceAndDie "you need to pass the name of the command to check to checkCommandExists and optionally an additional hint (e.g. install via...)"
Expand Down

0 comments on commit 2ed5961

Please sign in to comment.