ananke/scripts/lint.sh

50 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${REPO_DIR}"
export PATH="$(go env GOPATH)/bin:${PATH}"
STATICCHECK_VERSION="${ANANKE_STATICCHECK_VERSION:-2025.1.1}"
run_with_retry() {
local attempts="$1"
shift
local try=1
local delay=3
local rc=0
while true; do
"$@" && return 0
rc=$?
if [[ "${try}" -ge "${attempts}" ]]; then
return "${rc}"
fi
echo "[lint] retry ${try}/${attempts} after rc=${rc}: $*" >&2
sleep "${delay}"
delay=$((delay * 2))
try=$((try + 1))
done
}
if ! command -v staticcheck >/dev/null 2>&1 || ! staticcheck -version 2>/dev/null | grep -q "${STATICCHECK_VERSION}"; then
echo "[lint] installing staticcheck ${STATICCHECK_VERSION}"
run_with_retry 4 go install "honnef.co/go/tools/cmd/staticcheck@${STATICCHECK_VERSION}"
fi
echo "[lint] go vet"
go vet ./...
echo "[lint] staticcheck (pedantic code-smell pass)"
set +e
staticcheck_output="$(staticcheck ./... 2>&1)"
staticcheck_rc=$?
set -e
if [[ "${staticcheck_rc}" -ne 0 ]]; then
printf '%s\n' "${staticcheck_output}" >&2
if grep -q "Staticcheck was built with go" <<<"${staticcheck_output}" \
&& [[ "${ANANKE_STATICCHECK_REQUIRED:-0}" != "1" ]]; then
echo "[lint] warning: skipping staticcheck because the host staticcheck binary was built with an older Go toolchain" >&2
else
exit "${staticcheck_rc}"
fi
fi