.PHONY: build test lint fmt fmt-check vet tidy tidy-check vuln release-check snapshot check run

# Without the leading v, as goreleaser's {{ .Version }} reports it.
VERSION ?= $(patsubst v%,%,$(shell git describe --tags --always --dirty 2>/dev/null || echo dev))
COMMIT  ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo none)
LDFLAGS := -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT)

build:
	go build -trimpath -ldflags "$(LDFLAGS)" -o bin/ ./cmd/...

# The limit is per package: a hung test fails with every goroutine's stack.
test:
	go test -race -timeout 5m ./...

lint:
	go tool staticcheck ./...

# gofmt and goimports take paths, not ./... package patterns.
fmt:
	gofmt -w .
	go tool goimports -w .

fmt-check:
	@test -z "$$(gofmt -l .)" || (echo "gofmt needed:"; gofmt -l .; exit 1)
	@test -z "$$(go tool goimports -l .)" || (echo "goimports needed:"; go tool goimports -l .; exit 1)

vet:
	go vet ./...

tidy:
	go mod tidy

# Prints the change and fails, without writing, if go.mod or go.sum is not tidy.
tidy-check:
	go mod tidy -diff

vuln:
	go tool govulncheck ./...

# Validates .goreleaser.yaml without building anything.
release-check:
	goreleaser check

# Cross-compiles every shipped target and packs the release archives into
# dist/, publishing nothing. Run it after changing .goreleaser.yaml or
# packaging/; CI builds binaries only for release tags.
snapshot:
	goreleaser release --snapshot --clean

# The gate .gitea/workflows/test.yml runs, in the same order.
# .gitea/workflows/security.yml runs vuln.
check: fmt-check tidy-check vet lint test release-check

run:
	go run ./cmd/uberbringer --config config.toml
