Make buffer print and notification generic. Add print_buffer notification arguments.

This commit is contained in:
Vorezal 2026-02-11 02:35:36 +00:00
parent bae266a58d
commit 37f81de556
2 changed files with 33 additions and 24 deletions

View file

@ -218,7 +218,7 @@ log_print() {
if [[ $levelnum -ge $LOG_LEVEL_NUM ]]; then
# Don't print if in quiet mode
[[ "$Quiet" == "false" ]] && printf "${format}\n" "$@"
[[ "$Quiet" == "false" ]] && printf "${format}" "$@"
# Also log to buffer and file if configured
log "$logvar" "$loglevel" "$format" "$@"
fi
@ -240,7 +240,10 @@ log() {
# optionally output to file if enabled
# Replace all newlines with literal \n and remove all color codes
[[ "${LogToFile}" == "true" ]] && { printf "[%(%Y-%m-%d %H:%M:%S)T] [%-5s] ${format}" -1 "$loglevel" "$@" | awk '{printf "%s\\n", $0}' | sed -r 's/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g'; echo; } >> "${LogFile}" # write to file
[[ "${LogToFile}" == "true" ]] && \
{ printf "[%(%Y-%m-%d %H:%M:%S)T] [%-5s] ${format}" -1 "$loglevel" "$@" | \
awk 'NR > 1 {printf "%s\\n", prev} {prev = $0} END {printf "%s\n", prev}' | \
sed -r 's/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g'; } >> "${LogFile}"
declare -n out_array="${logvar}"
out_array+=("${buffer}")
@ -255,9 +258,15 @@ print_buffer() {
declare -n buffer="$1"
local BufToString
if [[ "${#buffer[@]}" -gt 0 ]]; then
BufToString=$( printf '%s' "${buffer[@]}" )
echo "${BufToString}"
if [[ -n "${buffer:-}" ]] && [[ "${#buffer[@]}" -gt 0 ]]; then
if [[ "${2:-}" != "notifyonly" ]]; then
BufToString=$( printf '%s' "${buffer[@]}" )
echo "${BufToString}"
fi
if [[ "${2:-}" == "notify" ]] || [[ "${2:-}" == "notifyonly" ]]; then
exec_if_exists_or_fail send_buffer_notification $1 ${3:-} || printf "Could not source buffer notification function.\n";
fi
fi
}
@ -865,7 +874,7 @@ fi
[[ -n "${BackupForDays:-}" ]] && remove_backups
# Send final summary notification if enabled
[[ "${Notify}" == true ]] && [[ "${ENABLE_SUMMARY_NOTIFICATION:-}" == true ]] && [[ -n "${actionbuf:-}" ]] && [[ "${#actionbuf[@]} -gt 0" ]] && { exec_if_exists_or_fail send_summary_notification || printf "Could not source summary notification function.\n"; }
[[ "${Notify}" == true ]] && [[ "${ENABLE_SUMMARY_NOTIFICATION:-}" == true ]] && print_buffer actionbuf notifyonly Actions;
if [[ -d $(dirname "${LogFile}") ]]; then
log_cleanup

View file

@ -180,7 +180,7 @@ format_output() {
local FormattedTextTemplate="$3"
local tempcsv=""
if [[ "${UpdateType}" == "summary" ]]; then
if [[ "${UpdateType}" == "buffer" ]]; then
tempcsv="${UpdToString//;/,}"
elif [[ ! "${UpdateType}" == "dockcheck_update" ]]; then
tempcsv="${UpdToString// -> /,}"
@ -196,17 +196,17 @@ format_output() {
FormattedOutput="${tempcsv}"
fi
elif [[ "${OutputFormat}" == "json" ]]; then
if [[ "${UpdateType}" == "summary" ]] && [[ -z "${UpdToString}" ]]; then
FormattedOutput='{"actions": []}'
if [[ "${UpdateType}" == "buffer" ]] && [[ -z "${UpdToString}" ]]; then
FormattedOutput='{"buffer": []}'
elif [[ -z "${UpdToString}" ]]; then
FormattedOutput='{"updates": []}'
else
if [[ "${UpdateType}" == "container_update" ]]; then
# container updates case
FormattedOutput=$(jq --compact-output --null-input --arg updates "${tempcsv}" '($updates | split("\\n")) | map(split(",")) | {"updates": map({"container_name": .[0], "release_notes": .[1]})} | del(..|nulls)')
elif [[ "${UpdateType}" == "summary" ]]; then
# final summary notification case
FormattedOutput=$(jq --compact-output --null-input --arg actions "${tempcsv}" '($actions | split("\\n")) | map(split(",")) | {"actions": map({"action": .[0], "result": .[1], "description": .[2]})} | del(..|nulls)')
elif [[ "${UpdateType}" == "buffer" ]]; then
# buffer notification case
FormattedOutput=$(jq --compact-output --null-input --arg buffer "${tempcsv}" '($buffer | split("\\n")) | map(split(",")) | {"events": map({"event": .[0], "result": .[1], "description": .[2]})} | del(..|nulls)')
elif [[ "${UpdateType}" == "notify_update" ]]; then
# script updates case
FormattedOutput=$(jq --compact-output --null-input --arg updates "${tempcsv}" '($updates | split("\\n")) | map(split(",")) | {"updates": map({"script_name": .[0], "installed_version": .[1], "latest_version": .[2]})}')
@ -223,7 +223,7 @@ format_output() {
else
if [[ "${UpdateType}" == "container_update" ]]; then
FormattedOutput="${FormattedTextTemplate/<insert_text_cu>/${UpdToString}}"
elif [[ "${UpdateType}" == "summary" ]]; then
elif [[ "${UpdateType}" == "buffer" ]]; then
FormattedOutput="${FormattedTextTemplate/<insert_text_sn>/${UpdToString}}"
elif [[ "${UpdateType}" == "notify_update" ]]; then
FormattedOutput="${FormattedTextTemplate/<insert_text_nu>/${UpdToString}}"
@ -310,31 +310,31 @@ send_notification() {
return 0
}
### Set ENABLE_SUMMARY_NOTIFICATION=true in dockcheck.config
### to send a final action summary notification
send_summary_notification() {
### Set notification settings in dockcheck.config
### to send buffer notifications
send_buffer_notification() {
Notified="false"
MessageTitle="$FromHost - Action summary"
declare -n buffer="$1"
UpdToString=$( printf '%s\\n' "${actionbuf[@]}" )
UpdToString="${UpdToString%, }"
UpdToString=${UpdToString%\\n}
MessageTitle="$FromHost - ${2:-$1}"
UpdToString=$( printf '%s' "${buffer[@]}" )
for channel in "${enabled_notify_channels[@]}"; do
local SkipNotification=$(skip_notification "${channel}" "1" "summary")
local SkipNotification=$(skip_notification "${channel}" "1" "buffer")
if [[ "${SkipNotification}" == "false" ]]; then
local template=$(get_channel_template "${channel}")
# Formats UpdToString variable per channel settings
format_output "summary" "$(output_format "${channel}")" "🐋 Actions taken by $FromHost:\n<insert_text_sn>\n"
format_output "buffer" "$(output_format "${channel}")" "🐋 ${2:-Events} on $FromHost:\n<insert_text_sn>\n"
# Setting the MessageBody variable here.
printf -v MessageBody "${FormattedOutput}"
printf "\nSending ${channel} summary notification"
printf "\nSending ${channel} buffer notification"
exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \
printf "\nAttempted to send summary notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory."
printf "\nAttempted to send buffer notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory."
Notified="true"
fi
done