mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-08 13:13:47 -05:00
update _alerts_
- 'info' now uses gray text - remove 'verbose' and favor 'debug' - 'input' is now underlined
This commit is contained in:
@@ -107,7 +107,7 @@ _sourceHelperFiles_
|
||||
Basic alerting, logging, and setting color functions (included in `scriptTemplate.sh` by default). Print messages to stdout and to a user specified logfile using the following functions.
|
||||
|
||||
```bash
|
||||
debug "some text" # Printed only when in Verbose mode
|
||||
debug "some text" # Printed only when in verbose (-v) mode
|
||||
info "some text" # Basic informational messages
|
||||
notice "some text" # Messages which should be read. Brighter than 'info'
|
||||
warning "some text" # Non-critical warnings
|
||||
|
||||
@@ -2,7 +2,16 @@
|
||||
|
||||
_mainScript_() {
|
||||
|
||||
info "Hello world"
|
||||
# Replace everything in _mainScript_() with your script's code
|
||||
header "Showing alert colors"
|
||||
debug "This is debug text"
|
||||
info "This is info text"
|
||||
notice "This is notice text"
|
||||
dryrun "This is dryrun text"
|
||||
warning "This is warning text"
|
||||
error "This is error text"
|
||||
success "This is success text"
|
||||
input "This is input text"
|
||||
|
||||
} # end _mainScript_
|
||||
|
||||
@@ -28,138 +37,6 @@ _mainScript_() {
|
||||
|
||||
# ################################## Common Functions for script template
|
||||
|
||||
_alert_() {
|
||||
# DESC: Controls all printing of messages to log files and stdout.
|
||||
# ARGS: $1 (required) - The type of alert to print
|
||||
# (success, header, notice, dryrun, debug, warning, error,
|
||||
# fatal, info, input)
|
||||
# $2 (required) - The message to be printed to stdout and/or a log file
|
||||
# $3 (optional) - Pass '${LINENO}' to print the line number where the _alert_ was triggered
|
||||
# OUTS: None
|
||||
# USAGE: [ALERTTYPE] "[MESSAGE]" "${LINENO}"
|
||||
# NOTES: The colors of each alert type are set in this function
|
||||
# For specified alert types, the funcstac will be printed
|
||||
|
||||
local function_name color
|
||||
local alertType="${1}"
|
||||
local message="${2}"
|
||||
local line="${3:-}" # Optional line number
|
||||
|
||||
if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
||||
message="${message} (line: ${line}) $(_functionStack_)"
|
||||
elif [[ -n "${line}" && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
||||
message="${message} (line: ${line})"
|
||||
elif [[ -z "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
||||
message="${message} $(_functionStack_)"
|
||||
fi
|
||||
|
||||
if [[ "${alertType}" =~ ^(error|fatal) ]]; then
|
||||
color="${bold}${red}"
|
||||
elif [ "${alertType}" = "warning" ]; then
|
||||
color="${red}"
|
||||
elif [ "${alertType}" = "success" ]; then
|
||||
color="${green}"
|
||||
elif [ "${alertType}" = "debug" ]; then
|
||||
color="${purple}"
|
||||
elif [ "${alertType}" = "header" ]; then
|
||||
color="${bold}${tan}"
|
||||
elif [[ "${alertType}" =~ ^(input|notice) ]]; then
|
||||
color="${bold}"
|
||||
elif [ "${alertType}" = "dryrun" ]; then
|
||||
color="${blue}"
|
||||
else
|
||||
color=""
|
||||
fi
|
||||
|
||||
_writeToScreen_() {
|
||||
|
||||
("${QUIET}") && return 0 # Print to console when script is not 'quiet'
|
||||
[[ ${VERBOSE} == false && "${alertType}" =~ ^(debug|verbose) ]] && return 0
|
||||
|
||||
if ! [[ -t 1 ]]; then # Don't use colors on non-recognized terminals
|
||||
color=""
|
||||
reset=""
|
||||
fi
|
||||
|
||||
echo -e "$(date +"%r") ${color}$(printf "[%7s]" "${alertType}") ${message}${reset}"
|
||||
}
|
||||
_writeToScreen_
|
||||
|
||||
_writeToLog_() {
|
||||
[[ "${alertType}" == "input" ]] && return 0
|
||||
[[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && return 0
|
||||
[ -z "${LOGFILE:-}" ] && LOGFILE="$(pwd)/$(basename "$0").log"
|
||||
[ ! -d "$(dirname "${LOGFILE}")" ] && command mkdir -p "$(dirname "${LOGFILE}")"
|
||||
[[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
|
||||
|
||||
# Don't use colors in logs
|
||||
if command -v gsed &>/dev/null; then
|
||||
local cleanmessage="$(echo "${message}" | gsed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
||||
else
|
||||
local cleanmessage="$(echo "${message}" | sed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
||||
fi
|
||||
echo -e "$(date +"%b %d %R:%S") $(printf "[%7s]" "${alertType}") [$(/bin/hostname)] ${cleanmessage}" >>"${LOGFILE}"
|
||||
}
|
||||
|
||||
# Write specified log level data to logfile
|
||||
case "${LOGLEVEL:-ERROR}" in
|
||||
ALL | all | All)
|
||||
_writeToLog_
|
||||
;;
|
||||
DEBUG | debug | Debug)
|
||||
_writeToLog_
|
||||
;;
|
||||
INFO | info | Info)
|
||||
if [[ "${alertType}" =~ ^(die|error|fatal|warning|info|notice|success) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
WARN | warn | Warn)
|
||||
if [[ "${alertType}" =~ ^(die|error|fatal|warning) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
ERROR | error | Error)
|
||||
if [[ "${alertType}" =~ ^(die|error|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
FATAL | fatal | Fatal)
|
||||
if [[ "${alertType}" =~ ^(die|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
OFF | off)
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
if [[ "${alertType}" =~ ^(die|error|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
} # /_alert_
|
||||
|
||||
error() { _alert_ error "${1}" "${2:-}"; }
|
||||
warning() { _alert_ warning "${1}" "${2:-}"; }
|
||||
notice() { _alert_ notice "${1}" "${2:-}"; }
|
||||
info() { _alert_ info "${1}" "${2:-}"; }
|
||||
success() { _alert_ success "${1}" "${2:-}"; }
|
||||
dryrun() { _alert_ dryrun "${1}" "${2:-}"; }
|
||||
input() { _alert_ input "${1}" "${2:-}"; }
|
||||
header() { _alert_ header "== ${1} ==" "${2:-}"; }
|
||||
die() {
|
||||
_alert_ fatal "${1}" "${2:-}"
|
||||
_safeExit_ "1"
|
||||
}
|
||||
fatal() {
|
||||
_alert_ fatal "${1}" "${2:-}"
|
||||
_safeExit_ "1"
|
||||
}
|
||||
debug() { _alert_ debug "${1}" "${2:-}"; }
|
||||
verbose() { _alert_ debug "${1}" "${2:-}"; }
|
||||
|
||||
_setColors_() {
|
||||
# DESC: Sets colors use for alerts.
|
||||
# ARGS: None
|
||||
@@ -180,7 +57,7 @@ _setColors_() {
|
||||
green=$(tput setaf 82)
|
||||
red=$(tput setaf 1)
|
||||
purple=$(tput setaf 171)
|
||||
gray=$(tput setaf 248)
|
||||
gray=$(tput setaf 250)
|
||||
else
|
||||
white=$(tput setaf 7)
|
||||
blue=$(tput setaf 38)
|
||||
@@ -207,6 +84,143 @@ _setColors_() {
|
||||
fi
|
||||
}
|
||||
|
||||
_alert_() {
|
||||
# DESC: Controls all printing of messages to log files and stdout.
|
||||
# ARGS: $1 (required) - The type of alert to print
|
||||
# (success, header, notice, dryrun, debug, warning, error,
|
||||
# fatal, info, input)
|
||||
# $2 (required) - The message to be printed to stdout and/or a log file
|
||||
# $3 (optional) - Pass '${LINENO}' to print the line number where the _alert_ was triggered
|
||||
# OUTS: None
|
||||
# USAGE: [ALERTTYPE] "[MESSAGE]" "${LINENO}"
|
||||
# NOTES: The colors of each alert type are set in this function
|
||||
# For specified alert types, the funcstac will be printed
|
||||
|
||||
local function_name color
|
||||
local alertType="${1}"
|
||||
local message="${2}"
|
||||
local line="${3:-}" # Optional line number
|
||||
|
||||
if [[ -n ${line} && ${alertType} =~ ^(fatal|error) && ${FUNCNAME[2]} != "_trapCleanup_" ]]; then
|
||||
message="${message} (line: ${line}) $(_functionStack_)"
|
||||
elif [[ -n ${line} && ${FUNCNAME[2]} != "_trapCleanup_" ]]; then
|
||||
message="${message} (line: ${line})"
|
||||
elif [[ -z ${line} && ${alertType} =~ ^(fatal|error) && ${FUNCNAME[2]} != "_trapCleanup_" ]]; then
|
||||
message="${message} $(_functionStack_)"
|
||||
fi
|
||||
|
||||
if [[ ${alertType} =~ ^(error|fatal) ]]; then
|
||||
color="${bold}${red}"
|
||||
elif [ "${alertType}" == "info" ]; then
|
||||
color="${gray}"
|
||||
elif [ "${alertType}" == "warning" ]; then
|
||||
color="${red}"
|
||||
elif [ "${alertType}" == "success" ]; then
|
||||
color="${green}"
|
||||
elif [ "${alertType}" == "debug" ]; then
|
||||
color="${purple}"
|
||||
elif [ "${alertType}" == "header" ]; then
|
||||
color="${bold}${tan}"
|
||||
elif [ ${alertType} == "notice" ]; then
|
||||
color="${bold}"
|
||||
elif [ ${alertType} == "input" ]; then
|
||||
color="${bold}${underline}"
|
||||
elif [ "${alertType}" = "dryrun" ]; then
|
||||
color="${blue}"
|
||||
else
|
||||
color=""
|
||||
fi
|
||||
|
||||
_writeToScreen_() {
|
||||
|
||||
("${QUIET}") && return 0 # Print to console when script is not 'quiet'
|
||||
[[ ${VERBOSE} == false && ${alertType} =~ ^(debug|verbose) ]] && return 0
|
||||
|
||||
if ! [[ -t 1 ]]; then # Don't use colors on non-recognized terminals
|
||||
color=""
|
||||
reset=""
|
||||
fi
|
||||
|
||||
echo -e "$(date +"%r") ${color}$(printf "[%7s]" "${alertType}") ${message}${reset}"
|
||||
}
|
||||
_writeToScreen_
|
||||
|
||||
_writeToLog_() {
|
||||
[[ ${alertType} == "input" ]] && return 0
|
||||
[[ ${LOGLEVEL} =~ (off|OFF|Off) ]] && return 0
|
||||
if [ -z "${LOGFILE:-}" ]; then
|
||||
LOGFILE="$(pwd)/$(basename "$0").log"
|
||||
fi
|
||||
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")"
|
||||
[[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}"
|
||||
|
||||
# Don't use colors in logs
|
||||
if command -v gsed &>/dev/null; then
|
||||
local cleanmessage="$(echo "${message}" | gsed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
||||
else
|
||||
local cleanmessage="$(echo "${message}" | sed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
||||
fi
|
||||
echo -e "$(date +"%b %d %R:%S") $(printf "[%7s]" "${alertType}") [$(/bin/hostname)] ${cleanmessage}" >>"${LOGFILE}"
|
||||
}
|
||||
|
||||
# Write specified log level data to logfile
|
||||
case "${LOGLEVEL:-ERROR}" in
|
||||
ALL | all | All)
|
||||
_writeToLog_
|
||||
;;
|
||||
DEBUG | debug | Debug)
|
||||
_writeToLog_
|
||||
;;
|
||||
INFO | info | Info)
|
||||
if [[ ${alertType} =~ ^(die|error|fatal|warning|info|notice|success) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
WARN | warn | Warn)
|
||||
if [[ ${alertType} =~ ^(die|error|fatal|warning) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
ERROR | error | Error)
|
||||
if [[ ${alertType} =~ ^(die|error|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
FATAL | fatal | Fatal)
|
||||
if [[ ${alertType} =~ ^(die|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
OFF | off)
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
if [[ ${alertType} =~ ^(die|error|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
} # /_alert_
|
||||
|
||||
error() { _alert_ error "${1}" "${2:-}"; }
|
||||
warning() { _alert_ warning "${1}" "${2:-}"; }
|
||||
notice() { _alert_ notice "${1}" "${2:-}"; }
|
||||
info() { _alert_ info "${1}" "${2:-}"; }
|
||||
success() { _alert_ success "${1}" "${2:-}"; }
|
||||
dryrun() { _alert_ dryrun "${1}" "${2:-}"; }
|
||||
input() { _alert_ input "${1}" "${2:-}"; }
|
||||
header() { _alert_ header "== ${1} ==" "${2:-}"; }
|
||||
debug() { _alert_ debug "${1}" "${2:-}"; }
|
||||
die() {
|
||||
_alert_ fatal "${1}" "${2:-}"
|
||||
_safeExit_ "1"
|
||||
}
|
||||
fatal() {
|
||||
_alert_ fatal "${1}" "${2:-}"
|
||||
_safeExit_ "1"
|
||||
}
|
||||
|
||||
_safeExit_() {
|
||||
# DESC: Cleanup and exit from a script
|
||||
# ARGS: $1 (optional) - Exit code (defaults to 0)
|
||||
|
||||
@@ -72,15 +72,6 @@ teardown() {
|
||||
refute_output --partial "testing"
|
||||
}
|
||||
|
||||
@test "_alert_: verbose" {
|
||||
run verbose "testing"
|
||||
refute_output --regexp "\[ debug\] testing"
|
||||
|
||||
VERBOSE=true
|
||||
run verbose "testing"
|
||||
assert_output --regexp "\[ debug\] testing"
|
||||
}
|
||||
|
||||
@test "_alert_: warning" {
|
||||
run warning "testing"
|
||||
assert_output --regexp "\[warning\] testing"
|
||||
|
||||
@@ -57,17 +57,32 @@ teardown() {
|
||||
@test "success" {
|
||||
run $s
|
||||
assert_success
|
||||
assert_output --partial "[ info] Hello world"
|
||||
assert_file_not_exist "${TESTDIR}/logs/log.txt"
|
||||
assert_output --partial "[ info] This is info text"
|
||||
assert_output --partial "[ notice] This is notice text"
|
||||
assert_output --partial "[ dryrun] This is dryrun text"
|
||||
assert_output --partial "[warning] This is warning text"
|
||||
assert_output --partial "[ error] This is error text"
|
||||
assert_output --partial "[success] This is success text"
|
||||
assert_output --partial "[ input] This is input text"
|
||||
|
||||
assert_file_exist "${TESTDIR}/logs/log.txt"
|
||||
run cat "${TESTDIR}/logs/log.txt"
|
||||
assert_line --index 0 --regexp "\[ error\] \[.*\] This is error text \( _mainScript_:scriptTemplate.* \)"
|
||||
assert_line --index 1 ""
|
||||
}
|
||||
|
||||
@test "success and INFO level log" {
|
||||
run $s --loglevel=INFO
|
||||
assert_success
|
||||
assert_output --partial "[ info] Hello world"
|
||||
assert_output --partial "[ info] This is info text"
|
||||
|
||||
run cat "${TESTDIR}/logs/log.txt"
|
||||
assert_line --index 0 --regexp "\[ info\].*Hello world"
|
||||
assert_line --index 0 --regexp "\[ info\].*This is info text"
|
||||
assert_line --index 1 --regexp "\[ notice\].*This is notice text"
|
||||
assert_line --index 2 --regexp "\[warning\].*This is warning text"
|
||||
assert_line --index 3 --regexp "\[ error\].*This is error text"
|
||||
assert_line --index 4 --regexp "\[success\].*This is success text"
|
||||
assert_line --index 5 ""
|
||||
}
|
||||
|
||||
@test "Usage (-h)" {
|
||||
@@ -90,5 +105,11 @@ teardown() {
|
||||
assert_output ""
|
||||
|
||||
run cat "${TESTDIR}/logs/log.txt"
|
||||
assert_line --index 0 --regexp "\[ info\].*Hello world"
|
||||
run cat "${TESTDIR}/logs/log.txt"
|
||||
assert_line --index 0 --regexp "\[ info\].*This is info text"
|
||||
assert_line --index 1 --regexp "\[ notice\].*This is notice text"
|
||||
assert_line --index 2 --regexp "\[warning\].*This is warning text"
|
||||
assert_line --index 3 --regexp "\[ error\].*This is error text"
|
||||
assert_line --index 4 --regexp "\[success\].*This is success text"
|
||||
assert_line --index 5 ""
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ _setColors_() {
|
||||
green=$(tput setaf 82)
|
||||
red=$(tput setaf 1)
|
||||
purple=$(tput setaf 171)
|
||||
gray=$(tput setaf 248)
|
||||
gray=$(tput setaf 250)
|
||||
else
|
||||
white=$(tput setaf 7)
|
||||
blue=$(tput setaf 38)
|
||||
@@ -72,16 +72,20 @@ _alert_() {
|
||||
|
||||
if [[ ${alertType} =~ ^(error|fatal) ]]; then
|
||||
color="${bold}${red}"
|
||||
elif [ "${alertType}" = "warning" ]; then
|
||||
elif [ "${alertType}" == "info" ]; then
|
||||
color="${gray}"
|
||||
elif [ "${alertType}" == "warning" ]; then
|
||||
color="${red}"
|
||||
elif [ "${alertType}" = "success" ]; then
|
||||
elif [ "${alertType}" == "success" ]; then
|
||||
color="${green}"
|
||||
elif [ "${alertType}" = "debug" ]; then
|
||||
elif [ "${alertType}" == "debug" ]; then
|
||||
color="${purple}"
|
||||
elif [ "${alertType}" = "header" ]; then
|
||||
elif [ "${alertType}" == "header" ]; then
|
||||
color="${bold}${tan}"
|
||||
elif [[ ${alertType} =~ ^(input|notice) ]]; then
|
||||
elif [ ${alertType} == "notice" ]; then
|
||||
color="${bold}"
|
||||
elif [ ${alertType} == "input" ]; then
|
||||
color="${bold}${underline}"
|
||||
elif [ "${alertType}" = "dryrun" ]; then
|
||||
color="${blue}"
|
||||
else
|
||||
@@ -168,6 +172,7 @@ success() { _alert_ success "${1}" "${2:-}"; }
|
||||
dryrun() { _alert_ dryrun "${1}" "${2:-}"; }
|
||||
input() { _alert_ input "${1}" "${2:-}"; }
|
||||
header() { _alert_ header "== ${1} ==" "${2:-}"; }
|
||||
debug() { _alert_ debug "${1}" "${2:-}"; }
|
||||
die() {
|
||||
_alert_ fatal "${1}" "${2:-}"
|
||||
_safeExit_ "1"
|
||||
@@ -176,8 +181,6 @@ fatal() {
|
||||
_alert_ fatal "${1}" "${2:-}"
|
||||
_safeExit_ "1"
|
||||
}
|
||||
debug() { _alert_ debug "${1}" "${2:-}"; }
|
||||
verbose() { _alert_ debug "${1}" "${2:-}"; }
|
||||
|
||||
_functionStack_() {
|
||||
# DESC: Prints the function stack in use
|
||||
|
||||
Reference in New Issue
Block a user