Skip to content

Commit

Permalink
Merge pull request #201 from tegonal/improve/shift-or-traceAndDie
Browse files Browse the repository at this point in the history
use traceAndDie instead of die in places where error message doesn't give enough context
  • Loading branch information
robstoll authored Oct 17, 2024
2 parents b40e558 + c0d0e1e commit 21cf6ec
Show file tree
Hide file tree
Showing 28 changed files with 63 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ sourceOnce "$dir_of_tegonal_scripts/utility/parse-utils.sh"
function myParseFunction() {
while (($# > 0)); do
if [[ $1 == "--version" ]]; then
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
printVersion "$MY_LIBRARY_VERSION"
fi
#...
Expand Down
2 changes: 1 addition & 1 deletion scripts/prepare-next-dev-cycle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sourceOnce "$dir_of_tegonal_scripts/releasing/prepare-files-next-dev-cycle.sh"
sourceOnce "$scriptsDir/before-pr.sh"

function prepareNextDevCycle() {
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

# shellcheck disable=SC2034 # they seem unused but are necessary in order that parseArguments doesn't create global readonly vars
local version projectsRootDir additionalPattern beforePrFn
Expand Down
2 changes: 1 addition & 1 deletion scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function release() {
die "You need to have shellspec installed if you want to create a release."
fi

source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local version
# shellcheck disable=SC2034 # they seem unused but are necessary in order that parseArguments doesn't create global readonly vars
Expand Down
2 changes: 1 addition & 1 deletion src/ci/jelastic/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sourceOnce "$dir_of_tegonal_scripts/utility/parse-fn-args.sh"

function jelastic_exec() {
local -r command=$1
shift 1 || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"

local -r jelastic_cliHome="${JELASTIC_CLI_HOME:-${HOME:-~}}/jelastic"
if ! [[ -f "$jelastic_cliHome/$command" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion src/qa/run-shellcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function runShellcheck() {
fi
local -rn runShellcheck_paths=$1
local -r sourcePath=$2
shift 2 || die "could not shift by 2"
shift 2 || traceAndDie "could not shift by 2"

exitIfArgIsNotArrayOrIsEmpty runShellcheck_paths 1

Expand Down
2 changes: 1 addition & 1 deletion src/releasing/pre-release-checks-git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ sourceOnce "$dir_of_tegonal_scripts/utility/parse-args.sh"

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

local version branch
# shellcheck disable=SC2034 # is passed by name to parseArguments
Expand Down
2 changes: 1 addition & 1 deletion src/releasing/prepare-files-next-dev-cycle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ sourceOnce "$dir_of_tegonal_scripts/releasing/prepare-next-dev-cycle-template.sh
function prepareFilesNextDevCycle() {
local versionParamPatternLong projectsRootDirParamPatternLong
local additionalPatternParamPatternLong beforePrFnParamPatternLong afterVersionUpdateHookParamPatternLong
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local version afterVersionUpdateHook projectsRootDir additionalPattern beforePrFn afterVersionUpdateHook
# shellcheck disable=SC2034 # is passed by name to parseArguments
Expand Down
2 changes: 1 addition & 1 deletion src/releasing/prepare-next-dev-cycle-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function prepareNextDevCycleTemplate() {
local versionRegex versionParamPatternLong projectsRootDirParamPatternLong
local additionalPatternParamPatternLong beforePrFnParamPatternLong afterVersionUpdateHookParamPatternLong
local forReleaseParamPatternLong
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local version projectsRootDir additionalPattern beforePrFn afterVersionUpdateHook
# shellcheck disable=SC2034 # is passed by name to parseArguments
Expand Down
2 changes: 1 addition & 1 deletion src/releasing/release-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function releaseFiles() {
local versionParamPatternLong projectsRootDirParamPatternLong
local additionalPatternParamPatternLong afterVersionUpdateHookParamPatternLong releaseHookParamPatternLong
local findForSigningParamPatternLong beforePrFnParamPatternLong prepareNextDevCycleFnParamPatternLong
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local version key findForSigning branch projectsRootDir additionalPattern
# shellcheck disable=SC2034 # seems unused but is set in deduce-next-version
Expand Down
2 changes: 1 addition & 1 deletion src/releasing/release-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function releaseTemplate() {
local additionalPatternParamPatternLong prepareOnlyParamPatternLong
local beforePrFnParamPatternLong prepareNextDevCycleFnParamPatternLong afterVersionUpdateHookParamPatternLong
local forReleaseParamPatternLong releaseHookParamPatternLong
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local version releaseHook branch projectsRootDir additionalPattern nextVersion prepareOnly
local beforePrFn prepareNextDevCycleFn afterVersionUpdateHook
Expand Down
2 changes: 1 addition & 1 deletion src/releasing/update-version-README.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fi
sourceOnce "$dir_of_tegonal_scripts/utility/parse-args.sh"

function updateVersionReadme() {
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local version file additionalPattern
# shellcheck disable=SC2034 # is passed by name to parseArguments
Expand Down
2 changes: 1 addition & 1 deletion src/releasing/update-version-common-steps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ sourceOnce "$dir_of_tegonal_scripts/releasing/update-version-scripts.sh"

function updateVersionCommonSteps() {
local forReleaseParamPatternLong versionParamPatternLong additionalPatternParamPatternLong
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local forRelease version projectsRootDir additionalPattern
# shellcheck disable=SC2034 # is passed by name to parseArguments
Expand Down
2 changes: 1 addition & 1 deletion src/releasing/update-version-issue-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fi
sourceOnce "$dir_of_tegonal_scripts/utility/parse-args.sh"

function updateVersionIssueTemplates() {
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local version directory additionalPattern
# shellcheck disable=SC2034 # is passed by name to parseArguments
Expand Down
2 changes: 1 addition & 1 deletion src/releasing/update-version-scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fi
sourceOnce "$dir_of_tegonal_scripts/utility/parse-args.sh"

function updateVersionScripts() {
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || die "could not source common-constants.source.sh"
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"

local version directory additionalPattern
# shellcheck disable=SC2034 # is passed by name to parseArguments
Expand Down
10 changes: 5 additions & 5 deletions src/utility/array-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ sourceOnce "$dir_of_tegonal_scripts/utility/checks.sh"

joinByChar() {
local IFS="$1"
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
echo "$*"
}

Expand All @@ -78,7 +78,7 @@ joinByString() {
if (($# > 1)); then
local separator="$1"
local firstArg="$2"
shift 2 || die "could not shift by 2"
shift 2 || traceAndDie "could not shift by 2"
printf "%s" "$firstArg" "${@/#/$separator}"
fi
}
Expand All @@ -96,7 +96,7 @@ function arrFilter() {
local -rn arrFilter_arrIn=$1
local -rn arrFilter_arrOut=$2
local -r predicate=$3
shift 3 || die "could not shift by 3"
shift 3 || traceAndDie "could not shift by 3"

exitIfArgIsNotFunction "$predicate" 3

Expand Down Expand Up @@ -127,7 +127,7 @@ function arrTakeEveryX() {
local -rn arrFilterMod_arrOut=$2
local -ri modulo=$3
local -ri offset=$4
shift 4 || die "could not shift by 4"
shift 4 || traceAndDie "could not shift by 4"

# shellcheck disable=SC2317 # is passed by name to arrFilter
function arrFilterMod_fn() {
Expand All @@ -146,7 +146,7 @@ function arrStringEntryMaxLength() {
exit 9
fi
local -rn arrStringEntryMaxLength_arr=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"

local -i i maxLength=0 arrLength="${#arrStringEntryMaxLength_arr[@]}"
for ((i = 0; i < arrLength; ++i)); do
Expand Down
4 changes: 2 additions & 2 deletions src/utility/ask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function askYesOrNo() {
exit 9
fi
local -r question=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"

local -r askYesOrNo_timeout=20
local answer='n'
Expand Down Expand Up @@ -99,7 +99,7 @@ function askWithTimeout() {
local -r askWithTimeout_noAnswerFn=$3
local -r askWithTimeout_outVarName=$4
local -r askWithTimeout_readArgs=$5
shift 5 || die "could not shift by 5"
shift 5 || traceAndDie "could not shift by 5"

exitIfArgIsNotFunction "$askWithTimeout_noAnswerFn" 3
# shellcheck disable=SC2059 # the question itself can have %s thus we use it in the format string
Expand Down
16 changes: 8 additions & 8 deletions src/utility/checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function checkArgIsArray() {
fi
local -rn checkArgIsArray_arr=$1
local -r argNumberOrName=$2
shift 2 || die "could not shift by 2"
shift 2 || traceAndDie "could not shift by 2"

reg='^declare -a.*'
local arrayDefinition
Expand All @@ -121,9 +121,9 @@ function exitIfArgIsNotArrayOrIsEmpty() {
exitIfArgIsNotArray "$@"
local -rn exitIfArgIsNotArrayOrIsEmpty_arr=$1
local -r argNumberOrName=$2
shift 2 || die "could not shift by 2"
shift 2 || traceAndDie "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}"
traceAndDie "the passed argument \033[0;36m%s\033[0m is an empty array" "${!checkArgIsArray_arr}"
fi
}

Expand All @@ -144,7 +144,7 @@ function checkArgIsArrayWithTuples() {
local -r tupleRepresents=$3
local -r argNumberOrName=$4
local -r describeTupleFn=$5
shift 5 || die "could not shift by 5"
shift 5 || traceAndDie "could not shift by 5"

local -r arrLength=${#checkArgIsArrayWithTuples_paramArr[@]}

Expand All @@ -156,7 +156,7 @@ function checkArgIsArrayWithTuples() {
fi

local arrayDefinition
arrayDefinition=$(recursiveDeclareP checkArgIsArrayWithTuples_paramArr) || die "could not get array definition of %s" "${!checkArgIsArrayWithTuples_paramArr}"
arrayDefinition=$(recursiveDeclareP checkArgIsArrayWithTuples_paramArr) || traceAndDie "could not get array definition of %s" "${!checkArgIsArrayWithTuples_paramArr}"
reg='declare -a.*'
if ! [[ "$arrayDefinition" =~ $reg ]]; then
logError "the passed array \033[0;36m%s\033[0m is broken" "${!checkArgIsArrayWithTuples_paramArr}"
Expand Down Expand Up @@ -258,7 +258,7 @@ function exitIfArgIsNotVersion() {

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

local value argNumberOrName
# shellcheck disable=SC2034 # is passed by name to parseFnArgs
Expand Down Expand Up @@ -295,13 +295,13 @@ function exitIfCommandDoesNotExist() {
function exitIfVarsNotAlreadySetBySource() {
for varName in "$@"; do
if ! [[ -v "$varName" ]] || [[ -z ${!varName} ]]; then
die "looks like \$%s was not defined by %s where this file (%s) was sourced" "$varName" "${BASH_SOURCE[2]:-${BASH_SOURCE[1]}}" "${BASH_SOURCE[0]}"
traceAndDie "looks like \$%s was not defined by %s where this file (%s) was sourced" "$varName" "${BASH_SOURCE[2]:-${BASH_SOURCE[1]}}" "${BASH_SOURCE[0]}"
fi
done
}

function exitIfVariablesNotDefined() {
shift 1 || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
for variableName in "$@"; do
if ! declare -p "$variableName" 2>/dev/null | grep -q 'declare --'; then
logError "you need to define the variable \033[0;36m%s\033[0m otherwise we write to the global scope" "$variableName"
Expand Down
2 changes: 1 addition & 1 deletion src/utility/cleanups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function removeUnusedSignatures() {
exit 9
fi
local projectRootDir=$1
shift 1 || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"

find "$projectRootDir" \
-type f \
Expand Down
2 changes: 1 addition & 1 deletion src/utility/execute-if-defined.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function executeIfFunctionNameDefined() {
fi
local functionName=$1
local argNumberOrName=$2
shift 2 || die "could not shift by 2"
shift 2 || traceAndDie "could not shift by 2"
if [[ -n $functionName ]]; then
exitIfArgIsNotFunction "$functionName" "$argNumberOrName"
logInfo "arg %s defined (%s), going to call it" "$argNumberOrName" "$functionName"
Expand Down
6 changes: 3 additions & 3 deletions src/utility/git-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function hasRemoteTag() {
fi
local -r tag=$1
local -r remote=${2:-"origin"}
shift 1 || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
local output
output=$(git ls-remote -t "$remote") || die "the following command failed (see above): git ls-remote -t \"$remote\""
grep "$tag" >/dev/null <<<"$output"
Expand All @@ -141,7 +141,7 @@ function remoteTagsSorted() {
local remote="origin"
if (($# > 0)); then
remote=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
fi
git ls-remote --refs --tags "$remote" |
cut --delimiter='/' --fields=3 |
Expand All @@ -160,7 +160,7 @@ function latestRemoteTag() {
local -r tagFilter=${2:-".*"}
local tag
#shellcheck disable=SC2310 # we are aware of that || will disable set -e for remoteTagsSorted
tag=$(remoteTagsSorted "$remote" | grep -E "$tagFilter" | tail -n 1) || die "could not get remote tags sorted, see above"
tag=$(remoteTagsSorted "$remote" | grep -E "$tagFilter" | tail -n 1) || die "could not get remote tags sorted for remote %s, see above" "$remote"
if [[ -z $tag ]]; then
die "looks like remote \033[0;36m%s\033[0m does not have a tag yet." "$remote"
fi
Expand Down
8 changes: 4 additions & 4 deletions src/utility/io.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ function withCustomOutputInput() {
local outputNr=$1
local inputNr=$2
local fun=$3
shift 3 || die "could not shift by 3"
shift 3 || traceAndDie "could not shift by 3"

exitIfArgIsNotFunction "$fun" 3

local tmpFile
tmpFile=$(mktemp /tmp/tegonal-scripts-io.XXXXXXXXX)
eval "exec ${outputNr}>\"$tmpFile\"" || die "could not create output file descriptor %s" "$outputNr"
eval "exec ${inputNr}<\"$tmpFile\"" || die "could not create input file descriptor %s" "$inputNr"
eval "exec ${outputNr}>\"$tmpFile\"" || traceAndDie "could not create output file descriptor %s" "$outputNr"
eval "exec ${inputNr}<\"$tmpFile\"" || traceAndDie "could not create input file descriptor %s" "$inputNr"
# don't fail if we cannot delete the tmp file, if this should happened, then the system should clean-up the file when the process ends
rm "$tmpFile" || true

Expand All @@ -73,7 +73,7 @@ function withCustomOutputInput() {

function deleteDirChmod777() {
local -r dir=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
# e.g files in .git will be write-protected and we don't want sudo for this command
# yet, if it fails, then we ignore the problem and still try to delete the folder
chmod -R 777 "$dir" || true
Expand Down
16 changes: 8 additions & 8 deletions src/utility/log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,45 +88,45 @@ unset CDPATH

function logInfo() {
local msg=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
logInfoWithoutNewline "$msg\n" "$@"
}
function logInfoWithoutNewline() {
local msg=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
printf "\033[0;34mINFO\033[0m: $msg" "$@"
}

function logWarning() {
local msg=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
logWarningWithoutNewline "$msg\n" "$@"
}
function logWarningWithoutNewline() {
local msg=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
printf "\033[0;93mWARNING\033[0m: $msg" "$@"
}

function logError() {
local msg=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
logErrorWithoutNewline "$msg\n" "$@"
}
function logErrorWithoutNewline() {
local msg=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
printf >&2 "\033[0;31mERROR\033[0m: $msg" "$@"
}

function logSuccess() {
local msg=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
logSuccessWithoutNewline "$msg\n" "$@"
}
function logSuccessWithoutNewline() {
local msg=$1
shift || die "could not shift by 1"
shift 1 || traceAndDie "could not shift by 1"
printf "\033[0;32mSUCCESS\033[0m: $msg" "$@"
}

Expand Down
Loading

0 comments on commit 21cf6ec

Please sign in to comment.