MODULE = $(shell $(GO) list -m) VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2> /dev/null || \ cat .version 2> /dev/null || echo v0) PKGS = $(or $(PKG),$(shell $(GO) list ./...)) BIN = bin GO = go TIMEOUT = 15 V = 0 Q = $(if $(filter 1,$V),,@) M = $(shell if [ "$$(tput colors 2> /dev/null || echo 0)" -ge 8 ]; then printf "\033[34;1m▶\033[0m"; else printf "▶"; fi) GENERATED = # List of generated files .SUFFIXES: .PHONY: all all: fmt lint $(GENERATED) | $(BIN) ; $(info $(M) building executable…) @ ## Build program binary $Q $(GO) build \ -tags release \ -ldflags '-X $(MODULE)/cmd.Version=$(VERSION)' \ -o $(BIN)/$(basename $(MODULE)) main.go $(BIN): @mkdir -p $@ # Generate # Tests TEST_TARGETS := test-short test-race .PHONY: $(TEST_TARGETS) check test tests test-short: ARGS=-short ## Run only short tests test-race: ARGS=-race ## Run tests with race detector $(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%) $(TEST_TARGETS): test check test tests: fmt lint $(GENERATED) ; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests $Q mkdir -p test $Q $(GO) tool gotestsum --junitfile test/tests.xml -- -timeout $(TIMEOUT)s $(ARGS) $(PKGS) .PHONY: test-bench test-bench: $(GENERATED) ; $(info $(M) running benchmarks…) @ ## Run benchmarks $Q $(GO) tool gotestsum -f standard-quiet -- --timeout $(TIMEOUT)s -run=__absolutelynothing__ -bench=. $(PKGS) COVERAGE_MODE = atomic .PHONY: test-coverage test-coverage: fmt lint $(GENERATED) test-coverage: ; $(info $(M) running coverage tests…) @ ## Run coverage tests $Q mkdir -p test $Q $(GO) tool gotestsum -- \ -coverpkg=$(shell echo $(PKGS) | tr ' ' ',') \ -covermode=$(COVERAGE_MODE) \ -coverprofile=test/profile.out $(PKGS) $Q $(GO) tool cover -html=test/profile.out -o test/coverage.html $Q $(GO) tool gocover-cobertura < test/profile.out > test/coverage.xml # Inaccurate? @printf "Code coverage: "; \ go tool cover -func test/profile.out | awk '($$1 == "total:") { print $$NF}' .PHONY: lint lint: ; $(info $(M) running golint…) @ ## Run golint $Q $(GO) tool revive -formatter friendly -set_exit_status ./... .PHONY: fmt fmt: ; $(info $(M) running gofmt…) @ ## Run gofmt on all source files $Q $(GO) tool goimports -local $(MODULE) -w $(shell $(GO) list -f '{{$$d := .Dir}}{{range $$f := .GoFiles}}{{printf "%s/%s\n" $$d $$f}}{{end}}{{range $$f := .CgoFiles}}{{printf "%s/%s\n" $$d $$f}}{{end}}{{range $$f := .TestGoFiles}}{{printf "%s/%s\n" $$d $$f}}{{end}}' $(PKGS)) # Misc .PHONY: clean clean: ; $(info $(M) cleaning…) @ ## Cleanup everything @rm -rf $(BIN) test $(GENERATED) .PHONY: help help: @grep -hE '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-17s\033[0m %s\n", $$1, $$2}' .PHONY: version version: @echo $(VERSION)