43 lines
1.3 KiB
Bash
43 lines
1.3 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||
|
|
FAILED=0
|
||
|
|
|
||
|
|
check_pattern() {
|
||
|
|
local pattern="$1"
|
||
|
|
local scope="$2"
|
||
|
|
local label="$3"
|
||
|
|
shift 3
|
||
|
|
set +e
|
||
|
|
rg -n "${pattern}" "${scope}" "$@" >/tmp/soteria_smell_hits.txt
|
||
|
|
local status=$?
|
||
|
|
set -e
|
||
|
|
if [[ ${status} -eq 0 ]]; then
|
||
|
|
echo "Code smell check failed: ${label}"
|
||
|
|
cat /tmp/soteria_smell_hits.txt
|
||
|
|
FAILED=1
|
||
|
|
elif [[ ${status} -eq 2 ]]; then
|
||
|
|
echo "Code smell check failed: invalid regex for ${label}"
|
||
|
|
FAILED=1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
check_pattern 'fmt\.Print(f|ln)?\(' "${ROOT_DIR}/cmd" \
|
||
|
|
"use structured logging instead of fmt.Print* in cmd code" --glob '!**/*_test.go'
|
||
|
|
check_pattern 'fmt\.Print(f|ln)?\(' "${ROOT_DIR}/internal" \
|
||
|
|
"use structured logging instead of fmt.Print* in internal code" --glob '!**/*_test.go'
|
||
|
|
check_pattern 'panic\(' "${ROOT_DIR}/cmd" \
|
||
|
|
"avoid panic in production cmd code" --glob '!**/*_test.go'
|
||
|
|
check_pattern 'panic\(' "${ROOT_DIR}/internal" \
|
||
|
|
"avoid panic in production internal code" --glob '!**/*_test.go'
|
||
|
|
check_pattern 'log\.Fatalf\(' "${ROOT_DIR}" \
|
||
|
|
"keep log.Fatalf limited to cmd/soteria/main.go" \
|
||
|
|
--glob '!cmd/soteria/main.go' --glob '!**/*_test.go'
|
||
|
|
|
||
|
|
if [[ "${FAILED}" -ne 0 ]]; then
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "Code smell checks: ok"
|