From 1a94aa8578a4ba7494d82066dbe3a2aca2b2c502 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Wed, 19 Oct 2022 16:18:47 -0700 Subject: [PATCH] make/lint: Check that files are formatted (#18) This verifies that all Go files are properly formatted regardless of whether they're generated or not. Depends on #17 Resolves #13 --- Makefile | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 01dd87f..1a10e92 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,10 @@ CFF = $(GOBIN)/cff MOCKGEN = $(GOBIN)/mockgen STATICCHECK = $(GOBIN)/staticcheck -SRC_FILES = $(shell find . '(' -path '*test*' -o -path '*/examples/*' -prune ')' -o -name '*.go' -print) +SRC_FILES = $(shell find . '(' -path './.*' -o -path '*test*' -o -path '*/examples/*' -prune ')' -o -name '*.go' -print) + +# All go files are in scope for formatting -- even if they're generated. +GOFMT_FILES = $(shell find . -path './.*' -prune -o -name '*.go' -print) .PHONY: build build: $(CFF) @@ -48,13 +51,26 @@ tidy: go mod tidy \ ) &&) true +.PHONY: fmt +fmt: + @gofmt -w -l $(GOFMT_FILES) + .PHONY: lint -lint: staticcheck +lint: staticcheck checkfmt .PHONY: staticcheck staticcheck: $(STATICCHECK) $(STATICCHECK) ./... +.PHONY: checkfmt +checkfmt: + @DIFF=$$(gofmt -d $(GOFMT_FILES)); \ + if [[ -n "$$DIFF" ]]; then \ + echo "--- gofmt would cause changes:"; \ + echo "$$DIFF"; \ + exit 1; \ + fi + $(CFF): $(SRC_FILES) go install go.uber.org/cff/cmd/cff