SHELL := /bin/sh

GOFILES := $(shell find . -type f -name '*.go' -not -path './.git/*' -not -path './vendor/*' | sort)

.PHONY: verify fmt fmt-check test test-race mod-download clean-testcache

verify:
	sh scripts/verify.sh

fmt:
	@if [ -n "$(GOFILES)" ]; then \
		gofmt -w $(GOFILES); \
	fi

fmt-check:
	@if [ -n "$(GOFILES)" ]; then \
		unformatted="$$(gofmt -l $(GOFILES))"; \
		if [ -n "$$unformatted" ]; then \
			printf '%s\n' "The following Go files are not gofmt-formatted:" >&2; \
			printf '%s\n' "$$unformatted" >&2; \
			exit 1; \
		fi; \
	fi

mod-download:
	go mod download

clean-testcache:
	go clean -testcache

test: mod-download clean-testcache
	CGO_ENABLED=$${CGO_ENABLED:-1} go test -count=1 ./...

test-race: mod-download clean-testcache
	CGO_ENABLED=$${CGO_ENABLED:-1} go test -race -count=1 ./...
