-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
93 lines (77 loc) · 2.76 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
GOCMD ?= go
GOTEST ?= $(GOCMD) test
GOVET ?= $(GOCMD) vet
GOFMT ?= gofmt
BINARY ?= api-server
VERSION ?= $(shell git describe --tags --dirty --match='v*' 2> /dev/null || echo dev)
FILES ?= $(shell find . -type f -name '*.go')
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
CYAN := $(shell tput -Txterm setaf 6)
RESET := $(shell tput -Txterm sgr0)
.PHONY: all test build coverage
default: help
## Build:
build: ## Build your project and put the output binary in build/
$(GOCMD) build -ldflags "-s -w -X 'main.Version=$(VERSION)'" -o build/$(BINARY) .
install: build ## Build your project and install the binary in $GOPATH/bin/
rm -f "$(shell go env GOPATH)/bin/$(BINARY)"
cp build/$(BINARY) "$(shell go env GOPATH)/bin/$(BINARY)"
clean: ## Remove build related file
rm -fr ./build
rm -fr ./coverage
clean-all: ## Remove build related file and installed binary
rm -fr ./build
rm -fr ./coverage
rm -f "$(shell go env GOPATH)/bin/$(BINARY)"
fmt: ## Format your code with gofmt
$(GOFMT) -w .
## Test:
test: ## Run the tests of the project (fastest)
$(GOVET) ./...
$(GOTEST) -v ./...
test-ci: ## Run ALL the tests of the project (+race)
$(GOVET) ./...
$(GOTEST) -v -race ./...
test-coverage: ## Run the tests of the project and export the coverage
rm -fr coverage && mkdir coverage
$(GOTEST) -cover -covermode=atomic -coverprofile=coverage/coverage.out ./...
@echo ""
$(GOCMD) tool cover -func=coverage/coverage.out
@echo ""
$(GOCMD) tool cover -func=coverage/coverage.out -o coverage/coverage.txt
$(GOCMD) tool cover -html=coverage/coverage.out -o coverage/coverage.html
coverage: test-coverage ## Run test-coverage and open coverage in your browser
$(GOCMD) tool cover -html=coverage/coverage.out
## Lint:
lint: lint-go ## Run all available linters
lint-go: ## Use golintci-lint on your project
ifneq (, $(shell $(GOFMT) -l . ))
@echo "This files are not gofmt compliant:"
@$(GOFMT) -l .
@echo "Please run 'make fmt' to format your code"
@exit 1
endif
ifeq (, $(shell which staticcheck))
go install honnef.co/go/tools/cmd/staticcheck@latest
endif
staticcheck ./...
## Help:
help: ## Show this help.
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
env: ## Print useful environment variables to stdout
@echo '$$(GOCMD) :' $(GOCMD)
@echo '$$(GOTEST) :' $(GOTEST)
@echo '$$(GOVET) :' $(GOVET)
@echo '$$(BINARY) :' $(BINARY)
@echo '$$(VERSION) :' $(VERSION)
@echo '$$(FILES#) :' $(shell echo $(FILES) | wc -w)