2020-11-06 13:04:23 +00:00
|
|
|
|
#! /bin/bash
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
2021-01-16 09:16:05 +00:00
|
|
|
|
# version v0.1.3 stable
|
|
|
|
|
# executed by CI / manually (via Make)
|
2020-10-04 17:34:15 +00:00
|
|
|
|
# task checks files agains linting targets
|
|
|
|
|
|
2020-12-10 09:30:39 +00:00
|
|
|
|
SCRIPT="lint.sh"
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
|
|
|
|
function _get_current_directory
|
|
|
|
|
{
|
|
|
|
|
if dirname "$(readlink -f "${0}")" &>/dev/null
|
|
|
|
|
then
|
2021-01-16 09:16:05 +00:00
|
|
|
|
CDIR="$(dirname "$(readlink -f "${0}")")"
|
2020-10-04 17:34:15 +00:00
|
|
|
|
elif realpath -e -L "${0}" &>/dev/null
|
|
|
|
|
then
|
|
|
|
|
CDIR="$(realpath -e -L "${0}")"
|
|
|
|
|
CDIR="${CDIR%/setup.sh}"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CDIR="$(pwd)"
|
|
|
|
|
_get_current_directory
|
|
|
|
|
|
|
|
|
|
# ? ––––––––––––––––––––––––––––––––––––––––––––– ERRORS
|
|
|
|
|
|
|
|
|
|
set -eEuo pipefail
|
2021-01-28 13:50:28 +00:00
|
|
|
|
trap '__log_err ${FUNCNAME[0]:-"?"} ${BASH_COMMAND:-"?"} ${LINENO:-"?"} ${?:-"?"}' ERR
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
|
|
|
|
function __log_err
|
|
|
|
|
{
|
|
|
|
|
printf "\n––– \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \
|
2020-12-22 19:07:08 +00:00
|
|
|
|
" – script = ${SCRIPT:-${0}}" \
|
|
|
|
|
" – function = ${1} / ${2}" \
|
|
|
|
|
" – line = ${3}" \
|
|
|
|
|
" – exit code = ${4}"
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
|
|
|
|
unset CDIR SCRIPT OS VERSION
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# ? ––––––––––––––––––––––––––––––––––––––––––––– LOG
|
|
|
|
|
|
|
|
|
|
function __log_info
|
|
|
|
|
{
|
|
|
|
|
printf "\n––– \e[34m%s\e[0m\n%s\n%s\n\n" \
|
2020-12-22 19:07:08 +00:00
|
|
|
|
"${SCRIPT:-${0}}" \
|
2020-10-04 17:34:15 +00:00
|
|
|
|
" – type = INFO" \
|
|
|
|
|
" – message = ${*}"
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 09:30:39 +00:00
|
|
|
|
function __log_failure
|
2020-10-04 17:34:15 +00:00
|
|
|
|
{
|
|
|
|
|
printf "\n––– \e[91m%s\e[0m\n%s\n%s\n\n" \
|
2020-12-22 19:07:08 +00:00
|
|
|
|
"${SCRIPT:-${0}}" \
|
2020-12-10 09:30:39 +00:00
|
|
|
|
" – type = FAILURE" \
|
2020-12-22 19:07:08 +00:00
|
|
|
|
" – message = ${*:-'errors encountered'}"
|
2020-10-04 17:34:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function __log_success
|
|
|
|
|
{
|
|
|
|
|
printf "\n––– \e[32m%s\e[0m\n%s\n%s\n\n" \
|
|
|
|
|
"${SCRIPT}" \
|
|
|
|
|
" – type = SUCCESS" \
|
2020-12-22 19:07:08 +00:00
|
|
|
|
" – message = no errors detected"
|
2020-10-04 17:34:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function __in_path { __which "${@}" && return 0 ; return 1 ; }
|
|
|
|
|
function __which { command -v "${@}" &>/dev/null ; }
|
|
|
|
|
|
|
|
|
|
function _eclint
|
|
|
|
|
{
|
2020-10-21 16:16:32 +00:00
|
|
|
|
local SCRIPT='EDITORCONFIG LINTER'
|
|
|
|
|
local LINT=(eclint -exclude "(.*\.git.*|.*\.md$|\.bats$|\.cf$|\.conf$|\.init$)")
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
|
|
|
|
if ! __in_path "${LINT[0]}"
|
|
|
|
|
then
|
2020-12-10 09:30:39 +00:00
|
|
|
|
__log_failure 'linter not in PATH'
|
|
|
|
|
return 2
|
2020-10-04 17:34:15 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2020-11-05 12:32:42 +00:00
|
|
|
|
__log_info 'linter version:' "$(${LINT[0]} --version)"
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
|
|
|
|
if "${LINT[@]}"
|
|
|
|
|
then
|
2020-12-22 19:07:08 +00:00
|
|
|
|
__log_success
|
2020-10-04 17:34:15 +00:00
|
|
|
|
else
|
2020-12-10 09:30:39 +00:00
|
|
|
|
__log_failure
|
|
|
|
|
return 1
|
2020-10-04 17:34:15 +00:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _hadolint
|
|
|
|
|
{
|
2020-10-21 16:16:32 +00:00
|
|
|
|
local SCRIPT='HADOLINT'
|
2020-10-04 17:34:15 +00:00
|
|
|
|
local LINT=(hadolint -c "${CDIR}/.hadolint.yaml")
|
|
|
|
|
|
|
|
|
|
if ! __in_path "${LINT[0]}"
|
|
|
|
|
then
|
2020-12-10 09:30:39 +00:00
|
|
|
|
__log_failure 'linter not in PATH'
|
|
|
|
|
return 2
|
2020-10-04 17:34:15 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2020-11-05 11:43:03 +00:00
|
|
|
|
__log_info 'linter version:' \
|
2020-11-05 12:32:42 +00:00
|
|
|
|
"$(${LINT[0]} --version | grep -E -o "v[0-9\.]*")"
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
|
|
|
|
if git ls-files --exclude='Dockerfile*' --ignored | \
|
|
|
|
|
xargs --max-lines=1 "${LINT[@]}"
|
|
|
|
|
then
|
2020-12-22 19:07:08 +00:00
|
|
|
|
__log_success
|
2020-10-04 17:34:15 +00:00
|
|
|
|
else
|
2020-12-10 09:30:39 +00:00
|
|
|
|
__log_failure
|
|
|
|
|
return 1
|
2020-10-04 17:34:15 +00:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _shellcheck
|
|
|
|
|
{
|
2020-10-21 16:16:32 +00:00
|
|
|
|
local SCRIPT='SHELLCHECK'
|
|
|
|
|
local ERR=0
|
2021-02-23 10:03:14 +00:00
|
|
|
|
local LINT=(shellcheck -x -S style -Cauto -o all -e SC2154 -W 50)
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
|
|
|
|
if ! __in_path "${LINT[0]}"
|
|
|
|
|
then
|
2020-12-10 09:30:39 +00:00
|
|
|
|
__log_failure 'linter not in PATH'
|
|
|
|
|
return 2
|
2020-10-04 17:34:15 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2020-11-05 11:43:03 +00:00
|
|
|
|
__log_info 'linter version:' \
|
2020-11-05 12:32:42 +00:00
|
|
|
|
"$(${LINT[0]} --version | grep -m 2 -o "[0-9.]*")"
|
2020-10-04 17:34:15 +00:00
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
|
# an overengineered solution to allow shellcheck -x to
|
|
|
|
|
# properly follow `source=<SOURCE FILE>` when sourcing
|
|
|
|
|
# files with `. <FILE>` in shell scripts.
|
|
|
|
|
while read -r FILE
|
|
|
|
|
do
|
|
|
|
|
if ! (
|
|
|
|
|
cd "$(realpath "$(dirname "$(readlink -f "${FILE}")")")"
|
|
|
|
|
if ! "${LINT[@]}" "$(basename -- "${FILE}")"
|
|
|
|
|
then
|
2020-12-10 09:30:39 +00:00
|
|
|
|
exit 1
|
2020-10-21 16:16:32 +00:00
|
|
|
|
fi
|
|
|
|
|
)
|
|
|
|
|
then
|
|
|
|
|
ERR=1
|
|
|
|
|
fi
|
|
|
|
|
done < <(find . -type f -iname "*.sh" \
|
|
|
|
|
-not -path "./test/bats/*" \
|
|
|
|
|
-not -path "./test/test_helper/*" \
|
|
|
|
|
-not -path "./target/docker-configomat/*")
|
|
|
|
|
|
|
|
|
|
# the same for executables in target/bin/
|
|
|
|
|
while read -r FILE
|
|
|
|
|
do
|
|
|
|
|
if ! (
|
|
|
|
|
cd "$(realpath "$(dirname "$(readlink -f "${FILE}")")")"
|
|
|
|
|
if ! "${LINT[@]}" "$(basename -- "${FILE}")"
|
|
|
|
|
then
|
2020-12-10 09:30:39 +00:00
|
|
|
|
exit 1
|
2020-10-21 16:16:32 +00:00
|
|
|
|
fi
|
|
|
|
|
)
|
|
|
|
|
then
|
|
|
|
|
ERR=1
|
|
|
|
|
fi
|
|
|
|
|
done < <(find target/bin -executable -type f)
|
|
|
|
|
|
2020-11-05 11:43:03 +00:00
|
|
|
|
# the same for all test files
|
2020-11-05 12:32:42 +00:00
|
|
|
|
while read -r FILE
|
|
|
|
|
do
|
|
|
|
|
if ! (
|
|
|
|
|
cd "$(realpath "$(dirname "$(readlink -f "${FILE}")")")"
|
|
|
|
|
if ! "${LINT[@]}" "$(basename -- "${FILE}")"
|
|
|
|
|
then
|
2020-12-10 09:30:39 +00:00
|
|
|
|
exit 1
|
2020-11-05 12:32:42 +00:00
|
|
|
|
fi
|
|
|
|
|
)
|
|
|
|
|
then
|
|
|
|
|
ERR=1
|
|
|
|
|
fi
|
|
|
|
|
done < <(find test/ -maxdepth 1 -type f -iname "*.bats")
|
2020-11-05 11:43:03 +00:00
|
|
|
|
|
2020-11-06 13:04:23 +00:00
|
|
|
|
if [[ ${ERR} -eq 1 ]]
|
2020-10-04 17:34:15 +00:00
|
|
|
|
then
|
2020-12-10 09:30:39 +00:00
|
|
|
|
__log_failure 'errors encountered'
|
|
|
|
|
return 1
|
2020-10-04 17:34:15 +00:00
|
|
|
|
else
|
2020-12-22 19:07:08 +00:00
|
|
|
|
__log_success
|
2020-10-04 17:34:15 +00:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _main
|
|
|
|
|
{
|
2021-01-16 09:16:05 +00:00
|
|
|
|
case ${1:-} in
|
2020-10-04 17:34:15 +00:00
|
|
|
|
'eclint' ) _eclint ;;
|
|
|
|
|
'hadolint' ) _hadolint ;;
|
|
|
|
|
'shellcheck' ) _shellcheck ;;
|
|
|
|
|
*)
|
2020-12-10 09:30:39 +00:00
|
|
|
|
__log_failure \
|
2021-01-16 09:16:05 +00:00
|
|
|
|
"${SCRIPT}: '${1}' is not a command nor an option."
|
2020-12-10 09:30:39 +00:00
|
|
|
|
exit 3
|
2020-10-04 17:34:15 +00:00
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 10:03:14 +00:00
|
|
|
|
# prefer linters installed in tools
|
|
|
|
|
PATH="$(pwd)/tools:${PATH}"
|
|
|
|
|
export PATH
|
|
|
|
|
|
2020-10-04 17:34:15 +00:00
|
|
|
|
_main "${@}" || exit ${?}
|