Skip to content

Commit

Permalink
shellcheck improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
natelandau committed Oct 29, 2021
1 parent 86300b6 commit 1377697
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 197 deletions.
16 changes: 7 additions & 9 deletions .hooks/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ _setColors_() {
white=$(tput setaf 231)
blue=$(tput setaf 38)
yellow=$(tput setaf 11)
tan=$(tput setaf 3)
green=$(tput setaf 82)
red=$(tput setaf 1)
purple=$(tput setaf 171)
Expand All @@ -445,7 +444,6 @@ _setColors_() {
white=$(tput setaf 7)
blue=$(tput setaf 38)
yellow=$(tput setaf 3)
tan=$(tput setaf 3)
green=$(tput setaf 2)
red=$(tput setaf 1)
purple=$(tput setaf 13)
Expand All @@ -460,7 +458,6 @@ _setColors_() {
white="\033[0;37m"
blue="\033[0;34m"
yellow="\033[0;33m"
tan="\033[0;33m"
green="\033[1;32m"
red="\033[0;31m"
purple="\033[0;35m"
Expand Down Expand Up @@ -648,7 +645,7 @@ _safeExit_() {
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}'"
warning "Script lock could not be removed. Try manually deleting ${yellow}'${SCRIPT_LOCK}'"
fi
fi

Expand Down Expand Up @@ -730,6 +727,7 @@ _makeTempDir_() {
debug "\$TMP_DIR=${TMP_DIR}"
}

# shellcheck disable=SC2120
_acquireScriptLock_() {
# DESC:
# Acquire script lock to prevent running the same script a second time before the
Expand All @@ -746,18 +744,18 @@ _acquireScriptLock_() {
if [[ ${1:-} == 'system' ]]; then
_lockDir="${TMPDIR:-/tmp/}$(basename "$0").lock"
else
_lockDir="${TMPDIR:-/tmp/}$(basename "$0").$UID.lock"
_lockDir="${TMPDIR:-/tmp/}$(basename "$0").${UID}.lock"
fi

if command mkdir "${LOCK_DIR}" 2>/dev/null; then
if command mkdir "${_lockDir}" 2>/dev/null; then
readonly SCRIPT_LOCK="${_lockDir}"
debug "Acquired script lock: ${yellow}${SCRIPT_LOCK}${purple}"
else
if [ "$(declare -f "_safeExit_")" ]; then
error "Unable to acquire script lock: ${tan}${LOCK_DIR}${red}"
if declare -f "_safeExit_" &>/dev/null; then
error "Unable to acquire script lock: ${yellow}${_lockDir}${red}"
fatal "If you trust the script isn't running, delete the lock dir"
else
printf "%s\n" "ERROR: Could not acquire script lock. If you trust the script isn't running, delete: ${LOCK_DIR}"
printf "%s\n" "ERROR: Could not acquire script lock. If you trust the script isn't running, delete: ${_lockDir}"
exit 1
fi

Expand Down
50 changes: 26 additions & 24 deletions template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _mainScript_() {
input "This is input text"

}

#/_mainsScript_()

# ################################## Flags and defaults
Expand Down Expand Up @@ -52,7 +53,7 @@ _trapCleanup_() {
local _script="${5:-}"
local _sourced="${6:-}"

if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then
if declare -f "fatal" &>/dev/null && declare -f "_printFuncStack_" &>/dev/null; then

_funcstack="'$(printf "%s" "${_funcstack}" | sed -E 's/ / < /g')'"

Expand All @@ -65,7 +66,7 @@ _trapCleanup_() {
printf "%s\n" "Fatal error trapped. Exiting..."
fi

if [ "$(declare -f "_safeExit_")" ]; then
if declare -f _safeExit_ &>/dev/null; then
_safeExit_ 1
else
exit 1
Expand All @@ -87,9 +88,11 @@ _findBaseDir_() {
local _dir

# Is file sourced?
[[ $_ != "$0" ]] \
&& _source="${BASH_SOURCE[1]}" \
|| _source="${BASH_SOURCE[0]}"
if [[ ${_} != "${0}" ]]; then
_source="${BASH_SOURCE[1]}"
else
_source="${BASH_SOURCE[0]}"
fi

while [ -h "${_source}" ]; do # Resolve $SOURCE until the file is no longer a symlink
_dir="$(cd -P "$(dirname "${_source}")" && pwd)"
Expand All @@ -104,93 +107,92 @@ _sourceUtilities_() {
# Sources utility functions. Absolute paths are required for shellcheck to correctly
# parse the sourced files
# ARGS:
# NONE
# $1 (Required): Absolute path to the directory containing the utilities
# OUTS:
# 0: Success
# 1: Failure
# USAGE:
# _sourceUtilities_
# _sourceUtilities_ "$(_findBaseDir_)/../../shell-scripting-templates/utilities"

local _utilsPath
_utilsPath="$(_findBaseDir_)/../shell-scripting-templates/utilities/"
_utilsPath="${1}"

if [ -f "${_utilsPath}/alerts.bash" ]; then
source "${_utilsPath}/alerts.bash"
else
printf "%s\n" "ERROR: alerts.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/alerts.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/arrays.bash" ]; then
source "${_utilsPath}/arrays.bash"
else
printf "%s\n" "ERROR: arrays.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/arrays.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/checks.bash" ]; then
source "${_utilsPath}/checks.bash"
else
printf "%s\n" "ERROR: checks.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/checks.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/dates.bash" ]; then
source "${_utilsPath}/dates.bash"
else
printf "%s\n" "ERROR: dates.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/dates.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/debug.bash" ]; then
source "${_utilsPath}/debug.bash"
else
printf "%s\n" "ERROR: debug.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/debug.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/files.bash" ]; then
source "${_utilsPath}/files.bash"
else
printf "%s\n" "ERROR: files.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/files.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/macOS.bash" ]; then
source "${_utilsPath}/macOS.bash"
else
printf "%s\n" "ERROR: macOS.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/macOS.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/misc.bash" ]; then
source "${_utilsPath}/misc.bash"
else
printf "%s\n" "ERROR: misc.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/misc.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/services.bash" ]; then
source "${_utilsPath}/services.bash"
else
printf "%s\n" "ERROR: services.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/services.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/strings.bash" ]; then
source "${_utilsPath}/strings.bash"
else
printf "%s\n" "ERROR: strings.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/strings.bash not found"
exit 1
fi

if [ -f "${_utilsPath}/template_utils.bash" ]; then
source "${_utilsPath}/template_utils.bash"
else
printf "%s\n" "ERROR: template_utils.bash not found"
printf "%s\n" "ERROR: ${_utilsPath}/template_utils.bash not found"
exit 1
fi

}

_parseOptions_() {
Expand Down Expand Up @@ -219,7 +221,7 @@ _parseOptions_() {
_options+=("-${_c}") # Add current char to options
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ ${_optstring} == *"${_c}:"* && ${1:i+1} ]]; then
if [[ ${_optstring} == *"${_c}:"* && -n ${1:i+1} ]]; then
_options+=("${1:i+1}")
break
fi
Expand Down Expand Up @@ -265,10 +267,10 @@ _parseOptions_() {
break
;;
*)
if [ "$(declare -f "_safeExit_")" ]; then
if declare -f _safeExit_ &>/dev/null; then
fatal "invalid option: $1"
else
printf "%s\n" "Invalid option: $1"
printf "%s\n" "ERROR: Invalid option: $1"
exit 1
fi
;;
Expand Down Expand Up @@ -337,7 +339,7 @@ IFS=$' \n\t'
# set -o xtrace

# Source utility functions
_sourceUtilities_
_sourceUtilities_ "$(_findBaseDir_)/../shell-scripting-templates/utilities"

# Initialize color constants
_setColors_
Expand Down
41 changes: 23 additions & 18 deletions template_standalone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ _setColors_() {
white=$(tput setaf 231)
blue=$(tput setaf 38)
yellow=$(tput setaf 11)
tan=$(tput setaf 3)
green=$(tput setaf 82)
red=$(tput setaf 1)
purple=$(tput setaf 171)
Expand All @@ -61,7 +60,6 @@ _setColors_() {
white=$(tput setaf 7)
blue=$(tput setaf 38)
yellow=$(tput setaf 3)
tan=$(tput setaf 3)
green=$(tput setaf 2)
red=$(tput setaf 1)
purple=$(tput setaf 13)
Expand All @@ -76,7 +74,6 @@ _setColors_() {
white="\033[0;37m"
blue="\033[0;34m"
yellow="\033[0;33m"
tan="\033[0;33m"
green="\033[1;32m"
red="\033[0;31m"
purple="\033[0;35m"
Expand Down Expand Up @@ -241,10 +238,17 @@ _printFuncStack_() {
# NOTE:
# Does not print functions from the alert class
local _i
_funcStackResponse=()
declare -a _funcStackResponse=()
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do
case "${FUNCNAME[$_i]}" in "_alert_" | "_trapCleanup_" | fatal | error | warning | notice | info | debug | dryrun | header | success) continue ;; esac
_funcStackResponse+=("${FUNCNAME[$_i]}:$(basename "${BASH_SOURCE[$_i]}"):${BASH_LINENO[_i - 1]}")
case "${FUNCNAME[${_i}]}" in
_alert_ | _trapCleanup_ | fatal | error | warning | notice | info | debug | dryrun | header | success)
continue
;;
*)
_funcStackResponse+=("${FUNCNAME[${_i}]}:$(basename "${BASH_SOURCE[${_i}]}"):${BASH_LINENO[_i - 1]}")
;;
esac

done
printf "( "
printf %s "${_funcStackResponse[0]}"
Expand All @@ -264,7 +268,7 @@ _safeExit_() {
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}'"
warning "Script lock could not be removed. Try manually deleting ${yellow}'${SCRIPT_LOCK}'"
fi
fi

Expand Down Expand Up @@ -303,7 +307,7 @@ _trapCleanup_() {
local _script="${5:-}"
local _sourced="${6:-}"

if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then
if declare -f "fatal" &>/dev/null && declare -f "_printFuncStack_" &>/dev/null; then

_funcstack="'$(printf "%s" "${_funcstack}" | sed -E 's/ / < /g')'"

Expand All @@ -316,7 +320,7 @@ _trapCleanup_() {
printf "%s\n" "Fatal error trapped. Exiting..."
fi

if [ "$(declare -f "_safeExit_")" ]; then
if declare -f _safeExit_ &>/dev/null; then
_safeExit_ 1
else
exit 1
Expand Down Expand Up @@ -346,6 +350,7 @@ _makeTempDir_() {
debug "\$TMP_DIR=${TMP_DIR}"
}

# shellcheck disable=SC2120
_acquireScriptLock_() {
# DESC:
# Acquire script lock to prevent running the same script a second time before the
Expand All @@ -362,18 +367,18 @@ _acquireScriptLock_() {
if [[ ${1:-} == 'system' ]]; then
_lockDir="${TMPDIR:-/tmp/}$(basename "$0").lock"
else
_lockDir="${TMPDIR:-/tmp/}$(basename "$0").$UID.lock"
_lockDir="${TMPDIR:-/tmp/}$(basename "$0").${UID}.lock"
fi

if command mkdir "${LOCK_DIR}" 2>/dev/null; then
if command mkdir "${_lockDir}" 2>/dev/null; then
readonly SCRIPT_LOCK="${_lockDir}"
debug "Acquired script lock: ${yellow}${SCRIPT_LOCK}${purple}"
else
if [ "$(declare -f "_safeExit_")" ]; then
error "Unable to acquire script lock: ${tan}${LOCK_DIR}${red}"
if declare -f "_safeExit_" &>/dev/null; then
error "Unable to acquire script lock: ${yellow}${_lockDir}${red}"
fatal "If you trust the script isn't running, delete the lock dir"
else
printf "%s\n" "ERROR: Could not acquire script lock. If you trust the script isn't running, delete: ${LOCK_DIR}"
printf "%s\n" "ERROR: Could not acquire script lock. If you trust the script isn't running, delete: ${_lockDir}"
exit 1
fi

Expand Down Expand Up @@ -426,7 +431,7 @@ _useGNUutils_() {
# NOTES:
# GNU utilities can be added to MacOS using Homebrew

[ ! "$(declare -f "_setPATH_")" ] && fatal "${FUNCNAME[0]} needs function _setPATH_"
! declare -f "_setPATH_" &>/dev/null && fatal "${FUNCNAME[0]} needs function _setPATH_"

if _setPATH_ \
"/usr/local/opt/gnu-tar/libexec/gnubin" \
Expand Down Expand Up @@ -466,7 +471,7 @@ _parseOptions_() {
_options+=("-${_c}") # Add current char to options
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ ${_optstring} == *"${_c}:"* && ${1:i+1} ]]; then
if [[ ${_optstring} == *"${_c}:"* && -n ${1:i+1} ]]; then
_options+=("${1:i+1}")
break
fi
Expand Down Expand Up @@ -512,10 +517,10 @@ _parseOptions_() {
break
;;
*)
if [ "$(declare -f "_safeExit_")" ]; then
if declare -f _safeExit_ &>/dev/null; then
fatal "invalid option: $1"
else
printf "%s\n" "Invalid option: $1"
printf "%s\n" "ERROR: Invalid option: $1"
exit 1
fi
;;
Expand Down
Loading

0 comments on commit 1377697

Please sign in to comment.