Skip to content

Instantly share code, notes, and snippets.

@JosephViolago
Created June 28, 2018 16:11
Show Gist options
  • Save JosephViolago/49823e8420f00093f75f5a9eb0e69b70 to your computer and use it in GitHub Desktop.
Save JosephViolago/49823e8420f00093f75f5a9eb0e69b70 to your computer and use it in GitHub Desktop.
# Set magic variables for current file, directory, os, etc.
# shellcheck disable=SC2034,SC2086
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC2034,SC2086
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
# shellcheck disable=SC2034,SC2086
__base="$(basename ${__file} .sh)"
# Define the environment variables (and their defaults) that this script depends on
export LOG_LEVEL="${LOG_LEVEL:-6}" # 7 = debug -> 0 = emergency
export NO_COLOR="${NO_COLOR:-}" # true = disable color. otherwise autodetected
### Functions
##############################################################################
function _fmt () {
# shellcheck disable=SC2034
local color_debug="\x1b[35m"
# shellcheck disable=SC2034
local color_info="\x1b[32m"
# shellcheck disable=SC2034
local color_notice="\x1b[34m"
# shellcheck disable=SC2034
local color_warning="\x1b[33m"
# shellcheck disable=SC2034
local color_error="\x1b[31m"
# shellcheck disable=SC2034
local color_critical="\x1b[1;31m"
# shellcheck disable=SC2034
local color_alert="\x1b[1;33;41m"
# shellcheck disable=SC2034
local color_emergency="\x1b[1;4;5;33;41m"
local colorvar=color_$1
local color="${!colorvar:-$color_error}"
local color_reset="\x1b[0m"
if [ "${NO_COLOR}" = "true" ] || [[ "${TERM:-}" != "xterm"* ]] || [ -t 1 ]; then
# Don't use colors on pipes or non-recognized terminals
color=""; color_reset=""
fi
# shellcheck disable=SC2086
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") ${color}$(printf "[%9s]" ${1})${color_reset}";
}
# shellcheck disable=SC2015
function emergency () { echo "$(_fmt emergency) ${@}" 1>&2 || true; exit 1; }
# shellcheck disable=SC2015
function alert () { [ "${LOG_LEVEL}" -ge 1 ] && echo "$(_fmt alert) ${@}" 1>&2 || true; }
# shellcheck disable=SC2015
function critical () { [ "${LOG_LEVEL}" -ge 2 ] && echo "$(_fmt critical) ${@}" 1>&2 || true; }
# shellcheck disable=SC2015
function error () { [ "${LOG_LEVEL}" -ge 3 ] && echo "$(_fmt error) ${@}" 1>&2 || true; }
# shellcheck disable=SC2015
function warning () { [ "${LOG_LEVEL}" -ge 4 ] && echo "$(_fmt warning) ${@}" 1>&2 || true; }
# shellcheck disable=SC2015
function notice () { [ "${LOG_LEVEL}" -ge 5 ] && echo "$(_fmt notice) ${@}" 1>&2 || true; }
# shellcheck disable=SC2015
function info () { [ "${LOG_LEVEL}" -ge 6 ] && echo "$(_fmt info) ${@}" 1>&2 || true; }
# shellcheck disable=SC2015
function debug () { [ "${LOG_LEVEL}" -ge 7 ] && echo "$(_fmt debug) ${@}" 1>&2 || true; }
### Variables
##############################################################################
# Required general variable checks
if [ -z "${REGION:-}" ]; then
emergency "REGION is not set"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment