Skip to content

Commit 35beec7

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

File tree

2 files changed

+88
-88
lines changed

2 files changed

+88
-88
lines changed

.includes/misc_functions.sh

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -366,91 +366,3 @@ table() {
366366
local -a Data=("${@:Cols+1}")
367367
printf '%s\n' "${Data[@]}" | table_pipe "${Cols}" "${Headings[@]}"
368368
}
369-
370-
PrefixFileLines() {
371-
local Prefix="${1}"
372-
local FileName="${2}"
373-
374-
# Count lines in the Content string using process substitution
375-
local LineCount
376-
LineCount=$(wc -l < "${FileName}")
377-
378-
# Join the repeated prefix stream and the content stream
379-
paste -d '' <(yes "${Prefix}" | head -n "${LineCount}" || true) "${FileName}"
380-
}
381-
382-
RunAndLog() {
383-
# RunAndLog [RunningNoticeType] [Prefix:[OutputNoticeType]] [ErrorNoticeType] [ErrorMessage] [Command]
384-
# To skip an optional argument, pass an empty string
385-
local -l RunningNoticeType=${1-}
386-
local -l OutputNoticeType=${2-}
387-
local -l ErrorNoticeType=${3-}
388-
local ErrorMessage=${4-}
389-
shift 4
390-
local -a Command=("${@}")
391-
392-
local NoticeTypes_Regex='info|notice|warn|error|debug|trace'
393-
394-
local Prefix=''
395-
if [[ ${OutputNoticeType} == *:* ]]; then
396-
Prefix="${OutputNoticeType%%:*}:"
397-
OutputNoticeType=${OutputNoticeType#"${Prefix}"}
398-
Prefix="${C["RunningCommand"]}${Prefix}${NC-} "
399-
fi
400-
401-
local OutputFile
402-
403-
local CommandText
404-
CommandText="$(printf '%q ' "${Command[@]}" | xargs 2> /dev/null)"
405-
406-
# If the running notice type is set, log the command being run
407-
[[ -n ${RunningNoticeType-} ]] &&
408-
"${RunningNoticeType}" \
409-
"Running: ${C["RunningCommand"]}${CommandText}"
410-
411-
local ErrToNull=false
412-
local OutToNull=false
413-
if [[ ${OutputNoticeType-} =~ errtonull|bothtonull ]]; then
414-
ErrToNull=true
415-
fi
416-
if [[ ${OutputNoticeType-} =~ outtonull|bothtonull ]]; then
417-
OutToNull=true
418-
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
423-
local -i result=0
424-
if [[ ${ErrToNull} == true && ${OutToNull} == true ]]; then
425-
# Both stdout and stderr are redirected to /dev/null
426-
"${Command[@]}" &> /dev/null || result=$?
427-
elif [[ ${ErrToNull} == true && -n ${OutputFile-} ]]; then
428-
# stderr redircted to /dev/null, stdout redirected to output file
429-
"${Command[@]}" > "${OutputFile}" 2> /dev/null || result=$?
430-
elif [[ ${OutToNull} == true && -n ${OutputFile-} ]]; then
431-
# stdout redircted to /dev/null, stderr redirected to output file
432-
"${Command[@]}" 2> "${OutputFile}" > /dev/null || result=$?
433-
elif [[ -n ${OutputFile-} ]]; then
434-
# Both stdout and stderr redirected to output file
435-
"${Command[@]}" &> "${OutputFile}" || result=$?
436-
else
437-
# No redirection
438-
"${Command[@]}" || result=$?
439-
fi
440-
441-
if [[ -n ${OutputFile-} && -n $(cat "${OutputFile}") ]]; then
442-
"${OutputNoticeType}" \
443-
"$(PrefixFileLines "${Prefix}" "${OutputFile}")"
444-
rm -f "${OutputFile}"
445-
fi
446-
447-
[[ ${result} -eq 0 ]] && return
448-
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
455-
return ${result}
456-
}

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)