Skip to content

Commit 6c47f78

Browse files
committed
Add BothToNull option for RunAndParse, and make ErrorNotice optional
1 parent 53b5f60 commit 6c47f78

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

.includes/misc_functions.sh

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,17 @@ PrefixFileLines() {
380380
}
381381

382382
RunAndLog() {
383-
# RunAndLog [RunningNoticeType] [OutputNoticeType] [ErrorNoticeType] [ErrorMessage] [Command]
383+
# RunAndLog [RunningNoticeType] [Prefix:[OutputNoticeType]] [ErrorNoticeType] [ErrorMessage] [Command]
384+
# To skip an optional argument, pass an empty string
384385
local -l RunningNoticeType=${1-}
385386
local -l OutputNoticeType=${2-}
386387
local -l ErrorNoticeType=${3-}
387388
local ErrorMessage=${4-}
388389
shift 4
389390
local -a Command=("${@}")
390391

392+
NoticeTypes_Regex='info|notice|warn|error|debug|trace'
393+
391394
local Prefix=''
392395
if [[ ${OutputNoticeType} == *:* ]]; then
393396
Prefix="${OutputNoticeType%%:*}:"
@@ -407,24 +410,25 @@ RunAndLog() {
407410

408411
local ErrToNull=false
409412
local OutToNull=false
410-
if [[ ${OutputNoticeType-} =~ info|notice|warn|error|debug|trace ]]; then
411-
OutputFile=$(mktemp -t "${APPLICATION_NAME}.${FUNCNAME[0]}.RunAndLogOutputFile.XXXXXXXXXX")
412-
fi
413-
if [[ ${OutputNoticeType-} =~ errtonull ]]; then
413+
if [[ ${OutputNoticeType-} =~ errtonull|bothtonull ]]; then
414414
ErrToNull=true
415415
fi
416-
if [[ ${OutputNoticeType-} =~ outtonull ]]; then
416+
if [[ ${OutputNoticeType-} =~ outtonull|bothtonull ]]; then
417417
OutToNull=true
418418
fi
419+
if [[ ${ErrToNull} != true || ${OutToNull} != true ]] && [[ ${OutputNoticeType-} =~ ${NoticeTypes_Regex} ]]; then
420+
# If the output notice type is set, save the output to a file
421+
OutputFile=$(mktemp -t "${APPLICATION_NAME}.${FUNCNAME[0]}.RunAndLogOutputFile.XXXXXXXXXX")
422+
fi
419423
local -i result=0
420424
if [[ ${ErrToNull} == true && ${OutToNull} == true ]]; then
421425
# Both stdout and stderr are redirected to /dev/null
422426
"${Command[@]}" &> /dev/null || result=$?
423-
elif [[ -n ${OutputFile-} && ${ErrToNull} == true ]]; then
424-
# stderr redircted to null, stdout redirected to output file
427+
elif [[ ${ErrToNull} == true && -n ${OutputFile-} ]]; then
428+
# stderr redircted to /dev/null, stdout redirected to output file
425429
"${Command[@]}" > "${OutputFile}" 2> /dev/null || result=$?
426-
elif [[ -n ${OutputFile-} && ${OutToNull} == true ]]; then
427-
# stdout redircted to null, stderr redirected to output file
430+
elif [[ ${OutToNull} == true && -n ${OutputFile-} ]]; then
431+
# stdout redircted to /dev/null, stderr redirected to output file
428432
"${Command[@]}" 2> "${OutputFile}" > /dev/null || result=$?
429433
elif [[ -n ${OutputFile-} ]]; then
430434
# Both stdout and stderr redirected to output file
@@ -442,8 +446,11 @@ RunAndLog() {
442446

443447
[[ ${result} -eq 0 ]] && return
444448

445-
${ErrorNoticeType} \
446-
"${ErrorMessage}" \
447-
"Failing command: ${C["FailingCommand"]}${CommandText}"
449+
if [[ -n ${ErrorNoticeType-} ]]; then
450+
# If the error notice type is set, log the error
451+
${ErrorNoticeType} \
452+
"${ErrorMessage}" \
453+
"Failing command: ${C["FailingCommand"]}${CommandText}"
454+
fi
448455
return ${result}
449456
}

main.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ check_arch() {
467467

468468
# Check if the repo exists relative to the SCRIPTPATH
469469
check_repo() {
470-
if RunAndLog notice "git:notice" warn "Script repo has not been cloned." git -C "${SCRIPTPATH}" rev-parse --is-inside-work-tree; then
470+
if RunAndLog info "git:info" warn "Script repo has not been cloned." git -C "${SCRIPTPATH}" rev-parse --is-inside-work-tree; then
471471
if [[ -d ${SCRIPTPATH}/.includes ]] && [[ -d ${SCRIPTPATH}/.scripts ]]; then
472472
return
473473
else
@@ -480,7 +480,7 @@ check_repo() {
480480

481481
# Check if the templates repo exists relative to the ${TEMPLATES_PARENT_FOLDER}
482482
check_templates_repo() {
483-
if RunAndLog notice "git:notice" warn "Templates repo has not been cloned." git -C "${TEMPLATES_PARENT_FOLDER}" rev-parse --is-inside-work-tree; then
483+
if RunAndLog info "git:info" warn "Templates repo has not been cloned." git -C "${TEMPLATES_PARENT_FOLDER}" rev-parse --is-inside-work-tree; then
484484
return
485485
else
486486
return 1

0 commit comments

Comments
 (0)