|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# standard bash error handling |
| 4 | +set -o nounset # treat unset variables as an error and exit immediately. |
| 5 | +set -o errexit # exit immediately when a command fails. |
| 6 | +set -E # needs to be set if we want the ERR trap |
| 7 | + |
| 8 | +CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 9 | +ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd) |
| 10 | +GOLANGCI_LINT_VERSION="v1.55.2" |
| 11 | +TMP_DIR=$(mktemp -d) |
| 12 | + |
| 13 | +readonly CURRENT_DIR |
| 14 | +readonly GOLANGCI_LINT_VERSION |
| 15 | +readonly ROOT_PATH |
| 16 | +readonly TMP_DIR |
| 17 | + |
| 18 | +# shellcheck source=./hack/lib/utilities.sh |
| 19 | +source "${CURRENT_DIR}/lib/utilities.sh" || { |
| 20 | + echo 'Cannot load CI utilities.' |
| 21 | + exit 1 |
| 22 | +} |
| 23 | + |
| 24 | +host::install::golangci() { |
| 25 | + mkdir -p "${TMP_DIR}/bin" |
| 26 | + export PATH="${TMP_DIR}/bin:${PATH}" |
| 27 | + |
| 28 | + shout "Install the golangci-lint ${GOLANGCI_LINT_VERSION} locally to a tempdir..." |
| 29 | + curl -sSfL -o "${TMP_DIR}/golangci-lint.sh" https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh |
| 30 | + chmod 700 "${TMP_DIR}/golangci-lint.sh" |
| 31 | + |
| 32 | + "${TMP_DIR}/golangci-lint.sh" -b "${TMP_DIR}/bin" ${GOLANGCI_LINT_VERSION} |
| 33 | + |
| 34 | + echo -e "${GREEN}√ install golangci-lint${NC}" |
| 35 | +} |
| 36 | + |
| 37 | +golangci::run_checks() { |
| 38 | + if [ -z "$(command -v golangci-lint)" ]; then |
| 39 | + echo "golangci-lint not found locally. Execute script with env variable INSTALL_DEPS=true" |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | + |
| 43 | + GOT_VER=$(golangci-lint version --format=short 2>&1) |
| 44 | + if [[ "v${GOT_VER}" != "${GOLANGCI_LINT_VERSION}" ]]; then |
| 45 | + echo -e "${RED}✗ golangci-lint version mismatch, expected ${GOLANGCI_LINT_VERSION}, available ${GOT_VER} ${NC}" |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + |
| 49 | + shout "Run golangci-lint checks" |
| 50 | + |
| 51 | + # shellcheck disable=SC2046 |
| 52 | + golangci-lint run $(golangci::fix_if_requested) "${ROOT_PATH}/..." |
| 53 | + |
| 54 | + echo -e "${GREEN}√ run golangci-lint${NC}" |
| 55 | +} |
| 56 | + |
| 57 | +golangci::fix_if_requested() { |
| 58 | + if [[ "${LINT_FORCE_FIX:-x}" == "true" ]]; then |
| 59 | + echo "--fix" |
| 60 | + fi |
| 61 | +} |
| 62 | + |
| 63 | +docker::run_dockerfile_checks() { |
| 64 | + shout "Run hadolint Dockerfile checks" |
| 65 | + docker run --rm -i hadolint/hadolint <"${ROOT_PATH}/Dockerfile" |
| 66 | + echo -e "${GREEN}√ run hadolint${NC}" |
| 67 | +} |
| 68 | + |
| 69 | +shellcheck::run_checks() { |
| 70 | + shout "Run shellcheck checks" |
| 71 | + docker run --rm -v "$ROOT_PATH":/mnt koalaman/shellcheck:stable -x ./hack/*.sh |
| 72 | + echo -e "${GREEN}√ run shellcheck${NC}" |
| 73 | +} |
| 74 | + |
| 75 | +main() { |
| 76 | + if [[ "${INSTALL_DEPS:-x}" == "true" ]]; then |
| 77 | + host::install::golangci |
| 78 | + fi |
| 79 | + |
| 80 | + golangci::run_checks |
| 81 | + |
| 82 | + docker::run_dockerfile_checks |
| 83 | + |
| 84 | + shellcheck::run_checks |
| 85 | +} |
| 86 | + |
| 87 | +main |
0 commit comments