Makefile: use `go fmt`

fmt-check no runs go fmt and then a git diff; this should also fail on line ending diffs
This commit is contained in:
Adrien Delorme 2019-05-27 15:37:03 +02:00
parent 768736425e
commit deb2212e9e
2 changed files with 13 additions and 13 deletions

View File

@ -27,6 +27,11 @@ jobs:
- checkout
- run: GO111MODULE=on go run . --help
- run: make check-vendor-vs-mod
check-fmt:
<<: *golang
steps:
- checkout
- run: make fmt-check
build_linux:
<<: *golang
environment:
@ -74,6 +79,7 @@ workflows:
jobs:
- build
- check-vendor-vs-mod
- check-fmt
- build_linux:
filters:
tags:

View File

@ -8,9 +8,6 @@ GOOS=$(shell go env GOOS)
GOARCH=$(shell go env GOARCH)
GOPATH=$(shell go env GOPATH)
# gofmt
UNFORMATTED_FILES=$(shell find . -not -path "./vendor/*" -name "*.go" | xargs gofmt -s -l)
EXECUTABLE_FILES=$(shell find . -type f -executable | egrep -v '^\./(website/[vendor|tmp]|vendor/|\.git|bin/|scripts/|pkg/)' | egrep -v '.*(\.sh|\.bats|\.git)' | egrep -v './provisioner/(ansible|inspec)/test-fixtures/exit1')
# Get the git commit
@ -64,17 +61,14 @@ dev: ## Build and install a development build
@cp $(GOPATH)/bin/packer pkg/$(GOOS)_$(GOARCH)
fmt: ## Format Go code
@gofmt -w -s main.go $(UNFORMATTED_FILES)
@go fmt ./...
fmt-check: ## Check go code formatting
@echo "==> Checking that code complies with gofmt requirements..."
@if [ ! -z "$(UNFORMATTED_FILES)" ]; then \
echo "gofmt needs to be run on the following files:"; \
echo "$(UNFORMATTED_FILES)" | xargs -n1; \
fmt-check: fmt ## Check go code formatting
@echo "==> Checking that code complies with go fmt requirements..."
@git diff --exit-code; if [ $$? -eq 1 ]; then \
echo "Found files that are not fmt'ed."; \
echo "You can use the command: \`make fmt\` to reformat code."; \
exit 1; \
else \
echo "Check passed."; \
fi
mode-check: ## Check that only certain files are executable
@ -97,9 +91,9 @@ fmt-examples:
# source files.
generate: install-gen-deps ## Generate dynamically generated code
go generate ./...
gofmt -w common/bootcommand/boot_command.go
go fmt -w common/bootcommand/boot_command.go
goimports -w common/bootcommand/boot_command.go
gofmt -w command/plugin.go
go fmt -w command/plugin.go
test: fmt-check mode-check vet ## Run unit tests
@go test $(TEST) $(TESTARGS) -timeout=3m