From 8a4329ae9f018ab6cf89b0a66424d18fa62d0187 Mon Sep 17 00:00:00 2001 From: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:22:46 +0200 Subject: [PATCH] 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. --- .github/workflows/linting.yml | 23 ++----- test/linting/lint.sh | 109 ++++++++++------------------------ 2 files changed, 37 insertions(+), 95 deletions(-) diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index bacf96b6..a5d462b3 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -1,11 +1,9 @@ -name: "Lint" +name: Lint on: pull_request: - branches: [ "*" ] push: - branches: - - master + branches: [ master ] permissions: contents: read @@ -16,23 +14,12 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 - with: - submodules: recursive - name: Hadolint - run: | - make hadolint - env: - HADOLINT_VERSION: 2.4.1 + run: make hadolint - name: ShellCheck - run: | - make shellcheck - env: - SHELLCHECK_VERSION: 0.8.0 + run: make shellcheck - name: ECLint - run: | - make eclint - env: - ECLINT_VERSION: 2.3.5 + run: make eclint diff --git a/test/linting/lint.sh b/test/linting/lint.sh index 191313c2..48d68269 100755 --- a/test/linting/lint.sh +++ b/test/linting/lint.sh @@ -1,97 +1,52 @@ #! /bin/bash -# version v0.2.0 unstable -# executed by Make during CI or manually +# version v0.3.0 +# executed by Make (during CI or manually) # 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 shopt -s inherit_errexit -trap '__log_err "${FUNCNAME[0]:-?}" "${BASH_COMMAND:-?}" ${LINENO:-?} ${?:-?}' ERR -function __log_err -{ - printf "\n--- \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \ - " - script = ${SCRIPT:-${0}}" \ - " - function = ${1} / ${2}" \ - " - line = ${3}" \ - " - exit code = ${4}" -} +REPOSITORY_ROOT=$(realpath "$(dirname "$(readlink -f "${0}")")"/../../) +LOG_LEVEL=${LOG_LEVEL:-debug} +HADOLINT_VERSION='2.9.2' +ECLINT_VERSION='2.4.0' +SHELLCHECK_VERSION='0.8.0' -function __log_info -{ - 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 ; -} +# shellcheck source=./../../target/scripts/helpers/log.sh +source "${REPOSITORY_ROOT}/target/scripts/helpers/log.sh" function _eclint { - local SCRIPT='EDITORCONFIG LINTER' - if docker run --rm --tty \ - --volume "${REPO_ROOT}:/ci:ro" \ - --workdir "/ci" \ - --name eclint \ - "mstruebing/editorconfig-checker:${ECLINT_VERSION}" ec -config "/ci/test/linting/.ecrc.json" + --volume "${REPOSITORY_ROOT}:/ci:ro" \ + --workdir "/ci" \ + --name eclint \ + "mstruebing/editorconfig-checker:${ECLINT_VERSION}" ec -config "/ci/test/linting/.ecrc.json" then - __log_success + _log 'info' 'ECLint succeeded' else - __log_failure + _log 'error' 'ECLint failed' return 1 fi } function _hadolint { - local SCRIPT='HADOLINT' - if docker run --rm --tty \ - --volume "${REPO_ROOT}:/ci:ro" \ - --workdir "/ci" \ - "hadolint/hadolint:v${HADOLINT_VERSION}-alpine" hadolint --config "/ci/test/linting/.hadolint.yaml" Dockerfile + --volume "${REPOSITORY_ROOT}:/ci:ro" \ + --workdir "/ci" \ + "hadolint/hadolint:v${HADOLINT_VERSION}-alpine" hadolint --config "/ci/test/linting/.hadolint.yaml" Dockerfile then - __log_success + _log 'info' 'Hadolint succeeded' else - __log_failure + _log 'error' 'Hadolint failed' return 1 fi } function _shellcheck { - local SCRIPT='SHELLCHECK' - # File paths for shellcheck: F_SH=$(find . -type f -iname '*.sh' \ -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. # shellcheck disable=SC2068 if docker run --rm --tty \ - --volume "${REPO_ROOT}:/ci:ro" \ - --workdir "/ci" \ - "koalaman/shellcheck-alpine:v${SHELLCHECK_VERSION}" ${CMD_SHELLCHECK[@]} + --volume "${REPOSITORY_ROOT}:/ci:ro" \ + --workdir "/ci" \ + "koalaman/shellcheck-alpine:v${SHELLCHECK_VERSION}" ${CMD_SHELLCHECK[@]} then - __log_success + _log 'info' 'ShellCheck succeeded' else - __log_failure + _log 'error' 'ShellCheck failed' return 1 fi } -function __main +function _main { case "${1:-}" in - 'eclint' ) _eclint ;; - 'hadolint' ) _hadolint ;; - 'shellcheck' ) _shellcheck ;; - *) - __log_failure "'${1:-}' is not a command nor an option." + ( 'eclint' ) _eclint ;; + ( 'hadolint' ) _hadolint ;; + ( 'shellcheck' ) _shellcheck ;; + ( * ) + _log 'error' "'${1:-}' is not a command nor an option" return 3 ;; esac } -__main "${@}" || exit ${?} +_main "${@}" || exit ${?}