-
Notifications
You must be signed in to change notification settings - Fork 220
/
Makefile
137 lines (108 loc) · 3.47 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Basic Makefile for Golang project
# Includes GRPC Gateway, Protocol Buffers
SERVICE ?= $(shell basename `go list`)
VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2> /dev/null || cat $(PWD)/.version 2> /dev/null || echo v0)
PACKAGE ?= $(shell go list)
PACKAGES ?= $(shell go list ./...)
FILES ?= $(shell find . -type f -name '*.go' -not -path "./vendor/*")
BUILD_DIR ?= build
# Binaries
PROTOC ?= protoc
.PHONY: help clean fmt lint vet test test-cover build build-docker all
default: help
# show this help
help:
@echo 'usage: make [target] ...'
@echo ''
@echo 'targets:'
@egrep '^(.+)\:\ .*#\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*#/#/' | column -t -c 2 -s '#'
# clean, format, build and unit test
all:
make clean-all
make gofmt
make build
make test
# build and install go application executable
install:
go install -v ./...
# Print useful environment variables to stdout
env:
echo $(CURDIR)
echo $(SERVICE)
echo $(PACKAGE)
echo $(VERSION)
# go clean
clean:
go clean
# remove all generated artifacts and clean all build artifacts
clean-all:
go clean -i ./...
rm -fr build
# fetch and install all required tools
tools:
go get -u golang.org/x/tools/cmd/goimports
go get -u github.com/golang/lint/golint
go get github.com/golang/mock/[email protected]
# Reqire when adding proto and grpc compilation
# go get -u github.com/golang/protobuf/protoc-gen-go
# go get -u github.com/mwitkow/go-proto-validators/protoc-gen-govalidators
# go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
# go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
# format the go source files
fmt:
goimports -w $(FILES)
# run go lint on the source files
lint:
golint $(PACKAGES)
# run go vet on the source files
vet:
go vet ./...
# generate godocs and start a local documentation webserver on port 8085
doc:
godoc -http=:8085 -index
# update golang dependencies
update-dependencies:
go mod tidy
# generate grpc, grpc-gw files and swagger docs
generate-grpc: compile-proto generate-grpcgw generate-swagger
# compile protobuf definitions into golang source
compile-proto:
echo 'To Be implemented'
# generate grpc-gw reverse proxy code
generate-grpcgw:
echo 'To Be implemented'
# generate swagger docs from the proto files
generate-swagger:
echo 'To Be implemented'
# generate mock code
generate-mocks:
@./mock_gen.sh
go-generate:
go generate ./...
run: go-generate
go run ./cmd/gmqttd start -c ./cmd/gmqttd/default_config.yml
# generate all grpc files and mocks and build the go code
build: go-generate
go build -o $(BUILD_DIR)/gmqttd ./cmd/gmqttd
# generate mocks and run short tests
test: generate-mocks
go test -v -race ./...
# run benchmark tests
test-bench: generate-mocks
go test -bench ./...
# Generate test coverage
# Run test coverage and generate html report
test-cover:
rm -fr coverage
mkdir coverage
go list -f '{{if gt (len .TestGoFiles) 0}}"go test -covermode count -coverprofile {{.Name}}.coverprofile -coverpkg ./... {{.ImportPath}}"{{end}}' ./... | xargs -I {} bash -c {}
echo "mode: count" > coverage/cover.out
grep -h -v "^mode:" *.coverprofile >> "coverage/cover.out"
rm *.coverprofile
go tool cover -html=coverage/cover.out -o=coverage/cover.html
test-all: test test-bench test-cover
# Build Golang application binary with settings to enable it to run in a Docker scratch container.
binary: go-generate
CGO_ENABLED=0 GOOS=linux go build -ldflags '-s' -o $(BUILD_DIR)/gmqttd ./cmd/gmqttd
build-docker:
docker build -t gmqtt/gmqttd .