scripts: revised linting script (#2737)

The new version uses our `log.sh` helper to simplify logging
significantly. Moreover, the script was adjusted to the current style
and the GitHub workflow was streamlined. The workflow is ot providing
the version anymore (which was useless anyway), and has been compacted.
This commit is contained in:
Georg Lauterbach 2022-08-22 16:22:46 +02:00 committed by GitHub
parent 26d241381f
commit 8a4329ae9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 95 deletions

View File

@ -1,11 +1,9 @@
name: "Lint" name: Lint
on: on:
pull_request: pull_request:
branches: [ "*" ]
push: push:
branches: branches: [ master ]
- master
permissions: permissions:
contents: read contents: read
@ -16,23 +14,12 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
with:
submodules: recursive
- name: Hadolint - name: Hadolint
run: | run: make hadolint
make hadolint
env:
HADOLINT_VERSION: 2.4.1
- name: ShellCheck - name: ShellCheck
run: | run: make shellcheck
make shellcheck
env:
SHELLCHECK_VERSION: 0.8.0
- name: ECLint - name: ECLint
run: | run: make eclint
make eclint
env:
ECLINT_VERSION: 2.3.5

View File

@ -1,97 +1,52 @@
#! /bin/bash #! /bin/bash
# version v0.2.0 unstable # version v0.3.0
# executed by Make during CI or manually # executed by Make (during CI or manually)
# task checks files against linting targets # task checks files against linting targets
SCRIPT="lint.sh"
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
REPO_ROOT=$(realpath "${SCRIPT_DIR}"/../../)
HADOLINT_VERSION=2.8.0
ECLINT_VERSION=2.3.5
SHELLCHECK_VERSION=0.8.0
set -eEuo pipefail set -eEuo pipefail
shopt -s inherit_errexit shopt -s inherit_errexit
trap '__log_err "${FUNCNAME[0]:-?}" "${BASH_COMMAND:-?}" ${LINENO:-?} ${?:-?}' ERR
function __log_err REPOSITORY_ROOT=$(realpath "$(dirname "$(readlink -f "${0}")")"/../../)
{ LOG_LEVEL=${LOG_LEVEL:-debug}
printf "\n--- \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \ HADOLINT_VERSION='2.9.2'
" - script = ${SCRIPT:-${0}}" \ ECLINT_VERSION='2.4.0'
" - function = ${1} / ${2}" \ SHELLCHECK_VERSION='0.8.0'
" - line = ${3}" \
" - exit code = ${4}"
}
function __log_info # shellcheck source=./../../target/scripts/helpers/log.sh
{ source "${REPOSITORY_ROOT}/target/scripts/helpers/log.sh"
printf "\n--- \e[34m%s\e[0m\n%s\n%s\n\n" \
"${SCRIPT:-${0}}" \
" - type = INFO" \
" - version = ${*}"
}
function __log_failure
{
printf "\n--- \e[91m%s\e[0m\n%s\n%s\n\n" \
"${SCRIPT:-${0}}" \
" - type = FAILURE" \
" - message = ${*:-errors encountered}"
}
function __log_success
{
printf "\n--- \e[32m%s\e[0m\n%s\n%s\n\n" \
"${SCRIPT}" \
" - type = SUCCESS" \
" - message = no errors detected"
}
function __in_path
{
command -v "${@}" &>/dev/null && return 0 ; return 1 ;
}
function _eclint function _eclint
{ {
local SCRIPT='EDITORCONFIG LINTER'
if docker run --rm --tty \ if docker run --rm --tty \
--volume "${REPO_ROOT}:/ci:ro" \ --volume "${REPOSITORY_ROOT}:/ci:ro" \
--workdir "/ci" \ --workdir "/ci" \
--name eclint \ --name eclint \
"mstruebing/editorconfig-checker:${ECLINT_VERSION}" ec -config "/ci/test/linting/.ecrc.json" "mstruebing/editorconfig-checker:${ECLINT_VERSION}" ec -config "/ci/test/linting/.ecrc.json"
then then
__log_success _log 'info' 'ECLint succeeded'
else else
__log_failure _log 'error' 'ECLint failed'
return 1 return 1
fi fi
} }
function _hadolint function _hadolint
{ {
local SCRIPT='HADOLINT'
if docker run --rm --tty \ if docker run --rm --tty \
--volume "${REPO_ROOT}:/ci:ro" \ --volume "${REPOSITORY_ROOT}:/ci:ro" \
--workdir "/ci" \ --workdir "/ci" \
"hadolint/hadolint:v${HADOLINT_VERSION}-alpine" hadolint --config "/ci/test/linting/.hadolint.yaml" Dockerfile "hadolint/hadolint:v${HADOLINT_VERSION}-alpine" hadolint --config "/ci/test/linting/.hadolint.yaml" Dockerfile
then then
__log_success _log 'info' 'Hadolint succeeded'
else else
__log_failure _log 'error' 'Hadolint failed'
return 1 return 1
fi fi
} }
function _shellcheck function _shellcheck
{ {
local SCRIPT='SHELLCHECK'
# File paths for shellcheck: # File paths for shellcheck:
F_SH=$(find . -type f -iname '*.sh' \ F_SH=$(find . -type f -iname '*.sh' \
-not -path './test/bats/*' \ -not -path './test/bats/*' \
@ -132,28 +87,28 @@ function _shellcheck
# `source=relative/path/to/file.sh` will check the source value in each source-path as well. # `source=relative/path/to/file.sh` will check the source value in each source-path as well.
# shellcheck disable=SC2068 # shellcheck disable=SC2068
if docker run --rm --tty \ if docker run --rm --tty \
--volume "${REPO_ROOT}:/ci:ro" \ --volume "${REPOSITORY_ROOT}:/ci:ro" \
--workdir "/ci" \ --workdir "/ci" \
"koalaman/shellcheck-alpine:v${SHELLCHECK_VERSION}" ${CMD_SHELLCHECK[@]} "koalaman/shellcheck-alpine:v${SHELLCHECK_VERSION}" ${CMD_SHELLCHECK[@]}
then then
__log_success _log 'info' 'ShellCheck succeeded'
else else
__log_failure _log 'error' 'ShellCheck failed'
return 1 return 1
fi fi
} }
function __main function _main
{ {
case "${1:-}" in case "${1:-}" in
'eclint' ) _eclint ;; ( 'eclint' ) _eclint ;;
'hadolint' ) _hadolint ;; ( 'hadolint' ) _hadolint ;;
'shellcheck' ) _shellcheck ;; ( 'shellcheck' ) _shellcheck ;;
*) ( * )
__log_failure "'${1:-}' is not a command nor an option." _log 'error' "'${1:-}' is not a command nor an option"
return 3 return 3
;; ;;
esac esac
} }
__main "${@}" || exit ${?} _main "${@}" || exit ${?}