Improve shellcheck compatibility

This commit is contained in:
Nathaniel Landau
2021-10-28 17:59:34 -04:00
parent f44ea3d2e6
commit 86300b68c4
15 changed files with 286 additions and 180 deletions

View File

@@ -21,7 +21,8 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _gitDiffTmp="${TMP_DIR}/diff.txt"
local _gitDiffTmp
_gitDiffTmp="${TMP_DIR}/diff.txt"
if [ -f "${STOP_WORD_FILE}" ]; then
@@ -32,7 +33,7 @@ _mainScript_() {
debug "$(basename "${1}"): Checking for stop words..."
# remove blank lines from stopwords file
cat "${STOP_WORD_FILE}" | sed '/^$/d' >"${TMP_DIR}/pattern_file.txt"
sed '/^$/d' "${STOP_WORD_FILE}" >"${TMP_DIR}/pattern_file.txt"
# Add diff to a temporary file
git diff --cached -- "${1}" | grep '^+' >"${_gitDiffTmp}"
@@ -60,9 +61,11 @@ _mainScript_() {
# USAGE:
# _ignoreSymlinks_
local _gitIgnore="${GITROOT}/.gitignore"
local _haveSymlink=false
local _gitIgnore
local _haveSymlink
local _f
_gitIgnore="${GITROOT}/.gitignore"
_haveSymlink=false
debug "Checking for symlinks..."
@@ -81,7 +84,7 @@ _mainScript_() {
done
# Work on files that were mistakenly staged
for f in $(git status --porcelain | grep '^A' | sed 's/^A //'); do
for _f in $(git status --porcelain | grep '^A' | sed 's/^A //'); do
if [ -L "${_f}" ]; then
if ! grep "${_f}" "${_gitIgnore}"; then
if printf "\n%s" "${_f}" >>"${_gitIgnore}"; then
@@ -115,7 +118,8 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _filename="$(_fileName_ "${1}")"
local _filename
_filename="$(_fileName_ "${1}")"
if command -v yaml-lint >/dev/null; then
debug "${_filename}: Linting YAML..."
@@ -159,14 +163,22 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _filename="$(_fileName_ "${1}")"
local _filename
_filename="$(_fileName_ "${1}")"
if command -v shellcheck >/dev/null; then
debug "${_filename}: Linting shellscript..."
if shellcheck --exclude=2016,2059,2001,2002,2148,1090,2162,2005,2034,2154,2086,2155,2181,2164,2120,2119,1083,1117,2207,1091 "${1}"; then
return 0
if [[ ${_filename} == "*.j2" ]]; then
if shellcheck -x --exclude=1009,1054,1056,1072,1073,1083,2001,2148 "${1}"; then
return 0
else
return 1
fi
else
return 1
if shellcheck -x --exclude=2001,2148 "${1}"; then
return 0
else
return 1
fi
fi
else
notice "Shellcheck not installed. Continuing..."
@@ -187,9 +199,11 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _filename="$(_fileName_ "${1}")"
local _filename
_filename="$(_fileName_ "${1}")"
debug "${_filename}: Runing bats tests..."
if bats -t $1; then
if bats -t "$1"; then
return 0
else
return 1
@@ -198,7 +212,7 @@ _mainScript_() {
_lintAnsible_() {
# DESC:
# Lint Ansible YMAL files staged for commit. Requires ansible-lint to be installed.
# Lint Ansible YAML files staged for commit. Requires ansible-lint to be installed.
# ARGS:
# $1 (Required): Path to file
# OUTS:
@@ -209,7 +223,8 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _filename="$(_fileName_ "${1}")"
local _filename
_filename="$(_fileName_ "${1}")"
if ! command -v ansible-lint &>/dev/null; then
notice "ansible-lint not intstalled. Continuing..."
@@ -409,7 +424,7 @@ _setColors_() {
# OUTS:
# None
# USAGE:
# echo "${blue}Some text${reset}"
# printf "%s\n" "${blue}Some text${reset}"
if tput setaf 1 >/dev/null 2>&1; then
bold=$(tput bold)
@@ -440,6 +455,7 @@ _setColors_() {
bold="\033[4;37m"
reset="\033[0m"
underline="\033[4;37m"
# shellcheck disable=SC2034
reverse=""
white="\033[0;37m"
blue="\033[0;34m"
@@ -497,9 +513,9 @@ _alert_() {
_color="${purple}"
elif [ "${_alertType}" == "header" ]; then
_color="${bold}${white}${underline}"
elif [ ${_alertType} == "notice" ]; then
elif [ "${_alertType}" == "notice" ]; then
_color="${bold}"
elif [ ${_alertType} == "input" ]; then
elif [ "${_alertType}" == "input" ]; then
_color="${bold}${underline}"
elif [ "${_alertType}" = "dryrun" ]; then
_color="${blue}"
@@ -534,9 +550,10 @@ _alert_() {
[[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}"
# Don't use colors in logs
local cleanmessage="$(echo "${_message}" | sed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
local _cleanmessage
_cleanmessage="$(printf "%s" "${_message}" | sed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
# Print message to log file
printf "%s [%7s] %s %s\n" "$(date +"%b %d %R:%S")" "${_alertType}" "[$(/bin/hostname)]" "${cleanmessage}" >>"${LOGFILE}"
printf "%s [%7s] %s %s\n" "$(date +"%b %d %R:%S")" "${_alertType}" "[$(/bin/hostname)]" "${_cleanmessage}" >>"${LOGFILE}"
}
# Write specified log level data to logfile
@@ -611,7 +628,7 @@ _printFuncStack_() {
_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]}")
_funcStackResponse+=("${FUNCNAME[$_i]}:$(basename "${BASH_SOURCE[$_i]}"):${BASH_LINENO[_i - 1]}")
done
printf "( "
printf %s "${_funcStackResponse[0]}"
@@ -645,7 +662,7 @@ _safeExit_() {
fi
trap - INT TERM EXIT
exit ${1:-0}
exit "${1:-0}"
}
_trapCleanup_() {
@@ -659,7 +676,7 @@ _trapCleanup_() {
# $5: Scriptname
# $6: $BASH_SOURCE
# USAGE:
# trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' EXIT INT TERM SIGINT SIGQUIT SIGTERM
# trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' EXIT INT TERM SIGINT SIGQUIT SIGTERM ERR
# OUTS:
# Exits script with error code 1
@@ -671,7 +688,9 @@ _trapCleanup_() {
local _sourced="${6:-}"
if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then
_funcstack="'$(echo "${_funcstack}" | sed -E 's/ / < /g')'"
_funcstack="'$(printf "%s" "${_funcstack}" | sed -E 's/ / < /g')'"
if [[ ${_script##*/} == "${_sourced##*/}" ]]; then
fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]"
else
@@ -760,7 +779,7 @@ _setPATH_() {
for _newPath in "$@"; do
if [ -d "${_newPath}" ]; then
if ! echo "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then
if ! printf "%s" "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then
if PATH="${_newPath}:${PATH}"; then
debug "Added '${_newPath}' to PATH"
else
@@ -850,6 +869,7 @@ _parseOptions_() {
unset _options
# Read the options and set stuff
# shellcheck disable=SC2034
while [[ ${1:-} == -?* ]]; do
case $1 in
# Custom options