|
| 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." |
0 commit comments