Files
homelab-ansible/.claude/hooks/check-on-stop.sh
T

27 lines
1.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Stop hook: makes Claude fix lint errors before it can finish a reply.
#
# When YAML files have uncommitted changes, run yamllint and ansible-lint from
# .venv. On failure, exit 2 sends the errors back to Claude. It never contacts a
# host. If .venv doesn't exist it does nothing, and CI still runs both linters.
input=$(cat)
# Set when Claude is already continuing because of this hook; stopping here
# prevents an endless loop if it can't fix the error.
[[ "$(jq -r '.stop_hook_active // false' <<<"$input")" == "true" ]] && exit 0
cd "$CLAUDE_PROJECT_DIR" || exit 0
[[ -x .venv/bin/yamllint && -x .venv/bin/ansible-lint ]] || exit 0
# -uall lists files inside new folders, not just the folder.
git status --porcelain -uall | grep -qE '\.ya?ml$|\.j2$|\.ansible-lint$' || exit 0
failed=0
for check in ".venv/bin/yamllint --strict ." ".venv/bin/ansible-lint"; do
if ! out=$($check 2>&1); then
echo "'$check' failed, fix before finishing:" >&2
tail -n 30 <<<"$out" >&2
failed=1
fi
done
[[ $failed -eq 1 ]] && exit 2
exit 0