1- #! /usr/bin/env bash
21# BASH3 Boilerplate
32#
43# define_functions.sh
54#
6- # - Defines helper functions containing commands extracted from the
7- # bash3boilerplate main.sh as part of a refactoring to facilitate
5+ # - Defines helper functions containing commands extracted from the
6+ # bash3boilerplate main.sh as part of a refactoring to facilitate
87# wholesale reuse of main.sh's contents of without modification.
98#
10- # Usage (as invoked in bootstrap.sh):
9+ # Usage (as invoked in bootstrap.sh):
1110#
1211# source define_functions.sh
1312#
3231# ## Functions
3332# ####################################################################
3433
34+ # shellcheck disable=SC2034
3535function _fmt () {
3636 local color_debug=" \x1b[35m"
3737 local color_info=" \x1b[32m"
@@ -49,41 +49,44 @@ function _fmt () {
4949 # Don't use colors on pipes or non-recognized terminals
5050 color=" " ; color_reset=" "
5151 fi
52- echo -e " $( date -u +" %Y-%m-%d %H:%M:%S UTC" ) ${color} $( printf " [%9s]" ${1} ) ${color_reset} " ;
52+ echo -e " $( date -u +" %Y-%m-%d %H:%M:%S UTC" ) ${color} $( printf " [%9s]" " ${1} " ) ${color_reset} " ;
5353}
5454
55- # The block of single-line functions below all print to STDERR,
55+ # The block of single-line functions below all print to STDERR,
5656# leaving STDOUT for piping machine readable information to other
57- # software. Above each such function is a commented demonstration
58- # of its usage. Execution continues after an invocation of each
59- # function, except the "emergency" function, which causes
57+ # software. Above each such function is a commented demonstration
58+ # of its usage. Execution continues after an invocation of each
59+ # function, except the "emergency" function, which causes
6060# termination with a non-zero exit status.
6161
62+ # shellcheck disable=SC2015
63+ {
6264# emergency "A \"panic\" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call."
63- function emergency () { echo " $( _fmt emergency) ${@ } " 1>&2 || true ; exit 1; }
65+ function emergency () { echo " $( _fmt emergency) ${* } " 1>&2 || true ; exit 1; }
6466# alert "Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection."
65- function alert () { [ " ${LOG_LEVEL} " -ge 1 ] && echo " $( _fmt alert) ${@ } " 1>&2 || true ; }
67+ function alert () { [ " ${LOG_LEVEL} " -ge 1 ] && echo " $( _fmt alert) ${* } " 1>&2 || true ; }
6668# critical "Should be corrected immediately, but indicates failure in a primary system, an example is a loss of a backup ISP connection."
67- function critical () { [ " ${LOG_LEVEL} " -ge 2 ] && echo " $( _fmt critical) ${@ } " 1>&2 || true ; }
69+ function critical () { [ " ${LOG_LEVEL} " -ge 2 ] && echo " $( _fmt critical) ${* } " 1>&2 || true ; }
6870# error "Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time."
69- function error () { [ " ${LOG_LEVEL} " -ge 3 ] && echo " $( _fmt error) ${@ } " 1>&2 || true ; }
71+ function error () { [ " ${LOG_LEVEL} " -ge 3 ] && echo " $( _fmt error) ${* } " 1>&2 || true ; }
7072# warning "Warning messages, not an error, but indication that an error will occur if action is not taken, e.g. file system 85% full - each item must be resolved within a given time. This is a debug message"
71- function warning () { [ " ${LOG_LEVEL} " -ge 4 ] && echo " $( _fmt warning) ${@ } " 1>&2 || true ; }
73+ function warning () { [ " ${LOG_LEVEL} " -ge 4 ] && echo " $( _fmt warning) ${* } " 1>&2 || true ; }
7274# notice "Events that are unusual but not error conditions - might be summarized in an email to developers or admins to spot potential problems - no immediate action required."
73- function notice () { [ " ${LOG_LEVEL} " -ge 5 ] && echo " $( _fmt notice) ${@ } " 1>&2 || true ; }
75+ function notice () { [ " ${LOG_LEVEL} " -ge 5 ] && echo " $( _fmt notice) ${* } " 1>&2 || true ; }
7476# info "Normal operational messages - may be harvested for reporting, measuring throughput, etc. - no action required."
75- function info () { [ " ${LOG_LEVEL} " -ge 6 ] && echo " $( _fmt info) ${@ } " 1>&2 || true ; }
77+ function info () { [ " ${LOG_LEVEL} " -ge 6 ] && echo " $( _fmt info) ${* } " 1>&2 || true ; }
7678# debug "Info useful to developers for debugging the application, not useful during operations."
77- function debug () { [ " ${LOG_LEVEL} " -ge 7 ] && echo " $( _fmt debug) ${@ } " 1>&2 || true ; }
78-
79+ function debug () { [ " ${LOG_LEVEL} " -ge 7 ] && echo " $( _fmt debug) ${* } " 1>&2 || true ; }
80+ }
7981function suppress_debug_messages() { export LOG_LEVEL=6; }
8082function suppress_info_debug_messages () { export LOG_LEVEL=5; }
8183function suppress_notice_info_debug_messages () { export LOG_LEVEL=4; }
8284
8385function help () {
8486 echo " " 1>&2
85- echo " ${@ } " 1>&2
87+ echo " ${* } " 1>&2
8688 echo " " 1>&2
89+ # shellcheck disable=SC2154
8790 cat " ${__usage} " 1>&2
8891 echo " " 1>&2
8992 exit 1
0 commit comments