Skip to content

Commit 63f5036

Browse files
authored
Merge pull request #250 from tegonal/improve/withCustomInputOutput-err-msg
improve withCustomOutputInput if not enough arguments are passed
2 parents 0945f7b + 684dd77 commit 63f5036

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/utility/io.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ fi
5151
sourceOnce "$dir_of_tegonal_scripts/utility/checks.sh"
5252

5353
function withCustomOutputInput() {
54+
if (($# < 3)); then
55+
logError "At least three arguments needs to be passed to withCustomOutputInput, given \033[0;36m%s\033[0m\nFollowing a description of the parameters:" "$#"
56+
echo >&2 ' 1: outputNr the file descriptor number for the output (i.e. in which you want to write)'
57+
echo >&2 ' 2: inputNr the file descriptor number for the input (i.e. from which you want to read)'
58+
echo >&2 ' 3: callback the name of the callback function which shall be called'
59+
echo >&2 '...: vararg arguments which are passed to the callback function'
60+
61+
printStackTrace
62+
exit 9
63+
fi
5464
# prefix variables as the callback function might use variables from an outer scope and we would shadow those
5565
local withCustomOutputInput_outputNr=$1
5666
local withCustomOutputInput_inputNr=$2
@@ -60,10 +70,11 @@ function withCustomOutputInput() {
6070
exitIfArgIsNotFunction "$withCustomOutputInput_fun" 3
6171

6272
local withCustomOutputInput_tmpFile
63-
withCustomOutputInput_tmpFile=$(mktemp /tmp/tegonal-scripts-io.XXXXXXXXX)
73+
withCustomOutputInput_tmpFile=$(mktemp /tmp/tegonal-scripts-io.XXXXXXXXX) || traceAndDie "could not create a temporary directory"
6474
eval "exec ${withCustomOutputInput_outputNr}>\"$withCustomOutputInput_tmpFile\"" || traceAndDie "could not create output file descriptor %s" "$withCustomOutputInput_outputNr"
6575
eval "exec ${withCustomOutputInput_inputNr}<\"$withCustomOutputInput_tmpFile\"" || traceAndDie "could not create input file descriptor %s" "$withCustomOutputInput_inputNr"
6676
# don't fail if we cannot delete the tmp file, if this should happen, then the system should clean-up the file when the process ends
77+
# same same if $withCustomOutputInput_fun should fail/exit, we don't setup a trap, the system should clean it up
6778
rm "$withCustomOutputInput_tmpFile" || true
6879

6980
$withCustomOutputInput_fun "$@"

src/utility/log.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,45 +87,45 @@ shopt -s inherit_errexit
8787
unset CDPATH
8888

8989
function logInfo() {
90-
local msg=$1
90+
local -r msg=$1
9191
shift 1 || traceAndDie "could not shift by 1"
9292
logInfoWithoutNewline "$msg\n" "$@"
9393
}
9494
function logInfoWithoutNewline() {
95-
local msg=$1
95+
local -r msg=$1
9696
shift 1 || traceAndDie "could not shift by 1"
9797
printf "\033[0;34mINFO\033[0m: $msg" "$@"
9898
}
9999

100100
function logWarning() {
101-
local msg=$1
101+
local -r msg=$1
102102
shift 1 || traceAndDie "could not shift by 1"
103103
logWarningWithoutNewline "$msg\n" "$@"
104104
}
105105
function logWarningWithoutNewline() {
106-
local msg=$1
106+
local -r msg=$1
107107
shift 1 || traceAndDie "could not shift by 1"
108108
printf "\033[0;93mWARNING\033[0m: $msg" "$@"
109109
}
110110

111111
function logError() {
112-
local msg=$1
112+
local -r msg=$1
113113
shift 1 || traceAndDie "could not shift by 1"
114114
logErrorWithoutNewline "$msg\n" "$@"
115115
}
116116
function logErrorWithoutNewline() {
117-
local msg=$1
117+
local -r msg=$1
118118
shift 1 || traceAndDie "could not shift by 1"
119119
printf >&2 "\033[0;31mERROR\033[0m: $msg" "$@"
120120
}
121121

122122
function logSuccess() {
123-
local msg=$1
123+
local -r msg=$1
124124
shift 1 || traceAndDie "could not shift by 1"
125125
logSuccessWithoutNewline "$msg\n" "$@"
126126
}
127127
function logSuccessWithoutNewline() {
128-
local msg=$1
128+
local -r msg=$1
129129
shift 1 || traceAndDie "could not shift by 1"
130130
printf "\033[0;32mSUCCESS\033[0m: $msg" "$@"
131131
}

0 commit comments

Comments
 (0)