use 4 spaces for indent

This commit is contained in:
Nathaniel Landau
2021-08-09 12:14:22 -04:00
parent f7ca40e128
commit c430628f9d

View File

@@ -10,23 +10,22 @@ _mainScript_() {
# Script specific
# Common
LOGFILE="${HOME}/logs/$(basename "$0")"
LOGFILE="${HOME}/logs/$(basename "$0").log"
QUIET=false
LOGLEVEL=ERROR
VERBOSE=false
FORCE=false
DRYRUN=false
declare -a args=()
now=$(LC_ALL=C date +"%m-%d-%Y %r") # Returns: 06-14-2015 10:34:40 PM
datestamp=$(LC_ALL=C date +%Y-%m-%d) # Returns: 2015-06-14
hourstamp=$(LC_ALL=C date +%r) # Returns: 10:34:40 PM
timestamp=$(LC_ALL=C date +%Y%m%d_%H%M%S) # Returns: 20150614_223440
longdate=$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z") # Returns: Sun, 10 Jan 2016 20:47:53 -0500
gmtdate=$(LC_ALL=C date -u -R | sed 's/\+0000/GMT/') # Returns: Wed, 13 Jan 2016 15:55:29 GMT
declare -a ARGS=()
NOW=$(LC_ALL=C date +"%m-%d-%Y %r") # Returns: 06-14-2015 10:34:40 PM
DATESTAMP=$(LC_ALL=C date +%Y-%m-%d) # Returns: 2015-06-14
HOURSTAMP=$(LC_ALL=C date +%r) # Returns: 10:34:40 PM
TIMESTAMP=$(LC_ALL=C date +%Y%m%d_%H%M%S) # Returns: 20150614_223440
LONGDATE=$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z") # Returns: Sun, 10 Jan 2016 20:47:53 -0500
GMTDATE=$(LC_ALL=C date -u -R | sed 's/\+0000/GMT/') # Returns: Wed, 13 Jan 2016 15:55:29 GMT
# ################################## Custom utility functions
# ################################## Common Functions for script template
# Colors
if tput setaf 1 &>/dev/null; then
@@ -113,8 +112,8 @@ _alert_() {
_writeToLog_() {
[[ "${alertType}" == "input" ]] && return 0
[[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && return 0
[ -z "${LOGFILE:-}" ] && fatal "\$LOGFILE must be set"
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")"
[ -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
@@ -174,8 +173,14 @@ 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" ; }
die() {
_alert_ fatal "${1}" "${2:-}"
_safeExit_ "1"
}
fatal() {
_alert_ fatal "${1}" "${2:-}"
_safeExit_ "1"
}
debug() { _alert_ debug "${1}" "${2:-}"; }
verbose() { _alert_ debug "${1}" "${2:-}"; }
@@ -184,19 +189,21 @@ _safeExit_() {
# ARGS: $1 (optional) - Exit code (defaults to 0)
# OUTS: None
if [[ -d "${script_lock:-}" ]]; then
if command rm -rf "${script_lock}"; then
if [[ -d "${SCRIPT_LOCK:-}" ]]; then
if command rm -rf "${SCRIPT_LOCK}"; then
debug "Removing script lock"
else
warning "Script lock could not be removed. Try manually deleting ${tan}'${lock_dir}'${red}"
warning "Script lock could not be removed. Try manually deleting ${tan}'${LOCK_DIR}'${red}"
fi
fi
if [[ -n "${tmpDir:-}" && -d "${tmpDir:-}" ]]; then
if [[ ${1:-} == 1 && -n "$(ls "${tmpDir}")" ]]; then
command rm -r "${tmpDir}"
if [[ -n "${TMP_DIR:-}" && -d "${TMP_DIR:-}" ]]; then
if [[ ${1:-} == 1 && -n "$(ls "${TMP_DIR}")" ]]; then
# Do something here to save TMP_DIR on a non-zero script exit for debugging
command rm -r "${TMP_DIR}"
debug "Removing temp directory"
else
command rm -r "${tmpDir}"
command rm -r "${TMP_DIR}"
debug "Removing temp directory"
fi
fi
@@ -225,9 +232,9 @@ _trapCleanup_() {
funcstack="'$(echo "$funcstack" | sed -E 's/ / < /g')'"
if [[ "${script##*/}" == "${sourced##*/}" ]]; then
fatal "${7:-} command: '$command' (line: $line) [func: $(_functionStack_)]"
fatal "${7:-} command: '${command}' (line: ${line}) [func: $(_functionStack_)]"
else
fatal "${7:-} command: '$command' (func: ${funcstack} called at line $linecallfunc of '${script##*/}') (line: $line of '${sourced##*/}') "
fatal "${7:-} command: '${command}' (func: ${funcstack} called at line ${linecallfunc} of '${script##*/}') (line: $line of '${sourced##*/}') "
fi
_safeExit_ "1"
@@ -236,43 +243,43 @@ _trapCleanup_() {
_makeTempDir_() {
# DESC: Creates a temp directory to house temporary files
# ARGS: $1 (Optional) - First characters/word of directory name
# OUTS: $tmpDir - Temporary directory
# OUTS: $TMP_DIR - Temporary directory
# USAGE: _makeTempDir_ "$(basename "$0")"
[ -d "${tmpDir:-}" ] && return 0
[ -d "${TMP_DIR:-}" ] && return 0
if [ -n "${1:-}" ]; then
tmpDir="${TMPDIR:-/tmp/}${1}.$RANDOM.$RANDOM.$$"
TMP_DIR="${TMPDIR:-/tmp/}${1}.$RANDOM.$RANDOM.$$"
else
tmpDir="${TMPDIR:-/tmp/}$(basename "$0").$RANDOM.$RANDOM.$RANDOM.$$"
TMP_DIR="${TMPDIR:-/tmp/}$(basename "$0").$RANDOM.$RANDOM.$RANDOM.$$"
fi
(umask 077 && mkdir "${tmpDir}") || {
(umask 077 && mkdir "${TMP_DIR}") || {
fatal "Could not create temporary directory! Exiting."
}
debug "\$tmpDir=$tmpDir"
debug "\$TMP_DIR=${TMP_DIR}"
}
_acquireScriptLock_() {
# DESC: Acquire script lock
# ARGS: $1 (optional) - Scope of script execution lock (system or user)
# OUTS: $script_lock - Path to the directory indicating we have the script lock
# OUTS: $SCRIPT_LOCK - Path to the directory indicating we have the script lock
# NOTE: This lock implementation is extremely simple but should be reliable
# across all platforms. It does *not* support locking a script with
# symlinks or multiple hardlinks as there's no portable way of doing so.
# If the lock was acquired it's automatically released in _safeExit_()
local lock_dir
local LOCK_DIR
if [[ ${1:-} == 'system' ]]; then
lock_dir="${TMPDIR:-/tmp/}$(basename "$0").lock"
LOCK_DIR="${TMPDIR:-/tmp/}$(basename "$0").lock"
else
lock_dir="${TMPDIR:-/tmp/}$(basename "$0").$UID.lock"
LOCK_DIR="${TMPDIR:-/tmp/}$(basename "$0").$UID.lock"
fi
if command mkdir "${lock_dir}" 2>/dev/null; then
readonly script_lock="${lock_dir}"
debug "Acquired script lock: ${tan}${script_lock}${purple}"
if command mkdir "${LOCK_DIR}" 2>/dev/null; then
readonly SCRIPT_LOCK="${LOCK_DIR}"
debug "Acquired script lock: ${tan}${SCRIPT_LOCK}${purple}"
else
error "Unable to acquire script lock: ${tan}${lock_dir}${red}"
error "Unable to acquire script lock: ${tan}${LOCK_DIR}${red}"
fatal "If you trust the script isn't running, delete the lock dir"
fi
}
@@ -285,7 +292,7 @@ _functionStack_() {
local _i
funcStackResponse=()
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do
case "${FUNCNAME[$_i]}" in "_alert_" | "_trapCleanup_" | fatal | error | warning | verbose | debug | die) continue ;; esac
case "${FUNCNAME[$_i]}" in "_alert_" | "_trapCleanup_" | fatal | error | warning | notice | info | verbose | debug | dryrun | header | success | die) continue ;; esac
funcStackResponse+=("${FUNCNAME[$_i]}:$(basename ${BASH_SOURCE[$_i]}):${BASH_LINENO[$_i - 1]}")
done
printf "( "
@@ -330,15 +337,18 @@ _parseOptions_() {
# Read the options and set stuff
while [[ ${1:-} == -?* ]]; do
case $1 in
# Custom options
# Common options
-h | --help)
_usage_ >&2
_safeExit_
;;
-l | --loglevel)
--loglevel)
shift
LOGLEVEL=${1}
;;
-L | --logfile)
--logfile)
shift
LOGFILE="${1}"
;;
@@ -354,7 +364,7 @@ _parseOptions_() {
esac
shift
done
args+=("$@") # Store the remaining user input as arguments.
ARGS+=("$@") # Store the remaining user input as arguments.
}
_usage_() {
@@ -366,20 +376,22 @@ _usage_() {
${bold}Options:${reset}
-h, --help Display this help and exit
-l, --loglevel One of: FATAL, ERROR, WARN, INFO, DEBUG, ALL, OFF (Default is 'ERROR')
$ $(basename "$0") --loglevel 'WARN'
-L, --logfile Full PATH to logfile. (Default is '${HOME}/logs/$(basename "$0")')
--loglevel [LEVEL] One of: FATAL, ERROR, WARN, INFO, DEBUG, ALL, OFF (Default is 'ERROR')
--logfile [FILE] Full PATH to logfile. (Default is '${HOME}/logs/$(basename "$0").log')
-n, --dryrun Non-destructive. Makes no permanent changes.
-q, --quiet Quiet (no output)
-v, --verbose Output more information. (Items echoed to 'verbose')
--force Skip all user interaction. Implied 'Yes' to all actions.
${bold}Example Usage:${reset}
$ $(basename "$0") -vn --logfile "/path/to/file.log" --loglevel 'WARN'
EOF
}
# Initialize and run the script
# ################################## INITIALIZE AND RUN THE SCRIPT
# (Comment or uncomment the lines below to customize script behavior)
trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' \
EXIT INT TERM SIGINT SIGQUIT
set -o errtrace # Trap errors in subshells and functions
@@ -391,7 +403,7 @@ IFS=$' \n\t' # Set IFS to preferred implementation
set -o nounset # Disallow expansion of unset variables
# [[ $# -eq 0 ]] && _parseOptions_ "-h" # Force arguments when invoking the script
_parseOptions_ "$@" # Parse arguments passed to script
# _makeTempDir_ "$(basename "$0")" # Create a temp directory '$tmpDir'
# _makeTempDir_ "$(basename "$0")" # Create a temp directory '$TMP_DIR'
# _acquireScriptLock_ # Acquire script lock
_mainScript_ # Run the main logic script
_safeExit_ # Exit cleanly