Skip to content

Commit cfedb49

Browse files
committed
Move RunAndLog to main.sh
1 parent d2b2c2e commit cfedb49

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

main.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,94 @@ fatal() {
456456
[[ -f "${SCRIPTPATH}/.includes/usage.sh" ]] && source "${SCRIPTPATH}/.includes/usage.sh"
457457
[[ -f "${SCRIPTPATH}/.includes/cmdline.sh" ]] && source "${SCRIPTPATH}/.includes/cmdline.sh"
458458

459+
PrefixFileLines() {
460+
local Prefix="${1}"
461+
local FileName="${2}"
462+
463+
# Count lines in the Content string using process substitution
464+
local LineCount
465+
LineCount=$(wc -l < "${FileName}")
466+
467+
# Join the repeated prefix stream and the content stream
468+
paste -d '' <(yes "${Prefix}" | head -n "${LineCount}" || true) "${FileName}"
469+
}
470+
471+
RunAndLog() {
472+
# RunAndLog [RunningNoticeType] [Prefix:[OutputNoticeType]] [ErrorNoticeType] [ErrorMessage] [Command]
473+
# To skip an optional argument, pass an empty string
474+
local -l RunningNoticeType=${1-}
475+
local -l OutputNoticeType=${2-}
476+
local -l ErrorNoticeType=${3-}
477+
local ErrorMessage=${4-}
478+
shift 4
479+
local -a Command=("${@}")
480+
481+
local NoticeTypes_Regex='info|notice|warn|error|debug|trace'
482+
483+
local Prefix=''
484+
if [[ ${OutputNoticeType} == *:* ]]; then
485+
Prefix="${OutputNoticeType%%:*}:"
486+
OutputNoticeType=${OutputNoticeType#"${Prefix}"}
487+
Prefix="${C["RunningCommand"]}${Prefix}${NC-} "
488+
fi
489+
490+
local OutputFile
491+
492+
local CommandText
493+
CommandText="$(printf '%q ' "${Command[@]}" | xargs 2> /dev/null)"
494+
495+
# If the running notice type is set, log the command being run
496+
[[ -n ${RunningNoticeType-} ]] &&
497+
"${RunningNoticeType}" \
498+
"Running: ${C["RunningCommand"]}${CommandText}"
499+
500+
local ErrToNull=false
501+
local OutToNull=false
502+
if [[ ${OutputNoticeType-} =~ errtonull|bothtonull ]]; then
503+
ErrToNull=true
504+
fi
505+
if [[ ${OutputNoticeType-} =~ outtonull|bothtonull ]]; then
506+
OutToNull=true
507+
fi
508+
if [[ ${ErrToNull} != true || ${OutToNull} != true ]] && [[ ${OutputNoticeType-} =~ ${NoticeTypes_Regex} ]]; then
509+
# If the output notice type is set, save the output to a file
510+
OutputFile=$(mktemp -t "${APPLICATION_NAME}.${FUNCNAME[0]}.RunAndLogOutputFile.XXXXXXXXXX")
511+
fi
512+
local -i result=0
513+
if [[ ${ErrToNull} == true && ${OutToNull} == true ]]; then
514+
# Both stdout and stderr are redirected to /dev/null
515+
"${Command[@]}" &> /dev/null || result=$?
516+
elif [[ ${ErrToNull} == true && -n ${OutputFile-} ]]; then
517+
# stderr redircted to /dev/null, stdout redirected to output file
518+
"${Command[@]}" > "${OutputFile}" 2> /dev/null || result=$?
519+
elif [[ ${OutToNull} == true && -n ${OutputFile-} ]]; then
520+
# stdout redircted to /dev/null, stderr redirected to output file
521+
"${Command[@]}" 2> "${OutputFile}" > /dev/null || result=$?
522+
elif [[ -n ${OutputFile-} ]]; then
523+
# Both stdout and stderr redirected to output file
524+
"${Command[@]}" &> "${OutputFile}" || result=$?
525+
else
526+
# No redirection
527+
"${Command[@]}" || result=$?
528+
fi
529+
530+
if [[ -n ${OutputFile-} && -n $(cat "${OutputFile}") ]]; then
531+
"${OutputNoticeType}" \
532+
"$(PrefixFileLines "${Prefix}" "${OutputFile}")"
533+
rm -f "${OutputFile}"
534+
fi
535+
536+
[[ ${result} -eq 0 ]] && return
537+
538+
if [[ -n ${ErrorNoticeType-} ]]; then
539+
# If the error notice type is set, log the error
540+
${ErrorNoticeType} \
541+
"${ErrorMessage}" \
542+
"Failing command: ${C["FailingCommand"]}${CommandText}"
543+
fi
544+
return ${result}
545+
}
546+
459547
# Check for supported CPU architecture
460548
check_arch() {
461549
if [[ ${ARCH} != "arm64" ]] && [[ ${ARCH} != "aarch64" ]] && [[ ${ARCH} != "x86_64" ]]; then

0 commit comments

Comments
 (0)