-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
79 lines (56 loc) · 2.85 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# --- Global -------------------------------------------------------------------
O = out
all: build test check-coverage lint ## build, test, check coverage and lint
@if [ -e .git/rebase-merge ]; then git --no-pager log -1 --pretty='%h %s'; fi
@echo '$(COLOUR_GREEN)Success$(COLOUR_NORMAL)'
clean:: ## Remove generated files
-rm -rf $(O)
.PHONY: all clean
# --- Build --------------------------------------------------------------------
# Build all subdirs of ./cmd, excluding those with a leading underscore.
CMDDIRS = $(filter-out ./cmd/_%,$(wildcard ./cmd/*))
build: | $(O) ## Build binaries of directories in ./cmd to out/
go build -o $(O) $(CMDDIRS)
install: ## Build and install binaries in $GOBIN or $GOPATH/bin
go install $(CMDDIRS)
.PHONY: build install
# --- Test ---------------------------------------------------------------------
COVERFILE = $(O)/coverage.txt
COVERAGE = 100
test: build | $(O) ## Run tests and generate a coverage file
go test -coverprofile=$(COVERFILE) ./...
./cmd/timeout/test.sh
check-coverage: test ## Check that test coverage meets the required level
@go tool cover -func=$(COVERFILE) | $(CHECK_COVERAGE) || $(FAIL_COVERAGE)
cover: test ## Show test coverage in your browser
go tool cover -html=$(COVERFILE)
CHECK_COVERAGE = awk -F '[ \t%]+' '/^total:/ {print; if ($$3 < $(COVERAGE)) exit 1}'
FAIL_COVERAGE = { echo '$(COLOUR_RED)FAIL - Coverage below $(COVERAGE)%$(COLOUR_NORMAL)'; exit 1; }
.PHONY: check-coverage cover test
# --- Lint ---------------------------------------------------------------------
GOLINT_VERSION = 1.30.0
GOLINT_INSTALLED_VERSION = $(or $(word 4,$(shell golangci-lint --version 2>/dev/null)),0.0.0)
GOLINT_MIN_VERSION = $(shell printf '%s\n' $(GOLINT_VERSION) $(GOLINT_INSTALLED_VERSION) | sort -V | head -n 1)
GOPATH1 = $(firstword $(subst :, ,$(GOPATH)))
LINT_TARGET = $(if $(filter $(GOLINT_MIN_VERSION),$(GOLINT_VERSION)),lint-with-local,lint-with-docker)
lint: $(LINT_TARGET) ## Lint source code
lint-with-local: ## Lint source code with locally installed golangci-lint
golangci-lint run
lint-with-docker: ## Lint source code with docker image of golangci-lint
docker run --rm -w /src \
-v $(shell pwd):/src -v $(GOPATH1):/go -v $(HOME)/.cache:/root/.cache \
golangci/golangci-lint:v$(GOLINT_VERSION) \
golangci-lint run
lint-sh: ## Lint shell scripts
shellcheck cmd/timeout/test.sh
.PHONY: lint lint-sh lint-with-local lint-with-docker
# --- Utilities ----------------------------------------------------------------
COLOUR_NORMAL = $(shell tput sgr0 2>/dev/null)
COLOUR_RED = $(shell tput setaf 1 2>/dev/null)
COLOUR_GREEN = $(shell tput setaf 2 2>/dev/null)
COLOUR_WHITE = $(shell tput setaf 7 2>/dev/null)
help:
@awk -F ':.*## ' 'NF == 2 && $$1 ~ /^[A-Za-z0-9_-]+$$/ { printf "$(COLOUR_WHITE)%-30s$(COLOUR_NORMAL)%s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort
$(O):
@mkdir -p $@
.PHONY: help