Skip to content

Commit dfe3746

Browse files
mairasclaude
andcommitted
feat: add hostname lint check
Add pre-commit hook to detect hard-coded hostname references. Relates to halos-org/halos#61 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 37b0695 commit dfe3746

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Lint check for hard-coded hostname references.
4+
# See docs/HOSTNAME_POLICY.md in halos-distro for the full policy.
5+
#
6+
# Usage: Run from repository root.
7+
#
8+
9+
set -o errexit
10+
set -o pipefail
11+
set -o nounset
12+
13+
# Build pattern from parts to avoid self-detection
14+
HOSTNAME_PATTERN="halos\.(local|hal)"
15+
16+
# Detect if we're in halos-pi-gen (exempt repository)
17+
REPO_NAME=$(basename "$(git rev-parse --show-toplevel 2>/dev/null || pwd)")
18+
if [[ "$REPO_NAME" == "halos-pi-gen" ]]; then
19+
echo "Skipping hostname check: halos-pi-gen is exempt (default system hostname)"
20+
exit 0
21+
fi
22+
23+
# Get list of tracked files, excluding:
24+
# - Markdown files (documentation is allowed)
25+
# - This script itself
26+
SCRIPT_NAME=".github/scripts/check-hardcoded-hostnames.sh"
27+
files=$(git ls-files --cached | grep -v '\.md$' | grep -v "$SCRIPT_NAME" || true)
28+
29+
if [[ -z "$files" ]]; then
30+
echo "No files to check."
31+
exit 0
32+
fi
33+
34+
# Search for hard-coded hostnames
35+
violations=""
36+
while IFS= read -r file; do
37+
if [[ -f "$file" ]] && grep -q -E "$HOSTNAME_PATTERN" "$file" 2>/dev/null; then
38+
violations="${violations}${file}"$'\n'
39+
fi
40+
done <<< "$files"
41+
42+
if [[ -n "$violations" ]]; then
43+
echo "ERROR: Hard-coded hostname references found in non-documentation files:"
44+
echo ""
45+
echo "$violations" | while IFS= read -r file; do
46+
if [[ -n "$file" ]]; then
47+
echo " $file:"
48+
grep -n -E "$HOSTNAME_PATTERN" "$file" | sed 's/^/ /'
49+
fi
50+
done
51+
echo ""
52+
echo "Policy: These hostnames are only allowed in:"
53+
echo " - *.md documentation files"
54+
echo " - halos-pi-gen repository (default system hostname)"
55+
echo ""
56+
echo "Fix: Use environment variables or configuration instead."
57+
echo " - For defaults: Require explicit configuration (no hard-coded fallback)"
58+
echo " - For tests: Read from environment (e.g., HALOS_TEST_HOST)"
59+
echo ""
60+
exit 1
61+
fi
62+
63+
echo "Hostname check passed: no hard-coded hostname references in source files."

lefthook.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ pre-commit:
1010
glob: "*.rs"
1111
run: cargo clippy --all-targets -- -D warnings
1212
fail_text: "Fix clippy warnings before committing."
13+
14+
check-hostnames:
15+
run: .github/scripts/check-hardcoded-hostnames.sh
16+
fail_text: "Hard-coded hostname references found. See error above for details."

0 commit comments

Comments
 (0)