#!/bin/sh
# script/fmt-check-go: fail unless every Go source is formatted the way
# script/fmt would leave it, and name the files that are not. Read-only.
#
# Its own script because the Dockerfile's lint stage runs this half
# alone: there is no docker inside a docker build to run the markdown
# half in.
set -eu

ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"

# goimports v0.42.0, 2026-08-07. Must match script/fmt.
GOIMPORTS_REF="golang.org/x/tools/cmd/goimports@009367f5c17a8d4c45a961a3a509277190a9a6f0"

main() {
    cd "$ROOT"
    files="$(gofmt -s -l .)"
    if [ -n "$files" ]; then
        echo "gofmt: files not formatted:" >&2
        echo "$files" >&2
        exit 1
    fi
    files="$(go run "$GOIMPORTS_REF" -l .)"
    if [ -n "$files" ]; then
        echo "goimports: files not formatted:" >&2
        echo "$files" >&2
        exit 1
    fi
}

main "$@"
