-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
241 lines (207 loc) · 8.58 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
ifndef VERBOSE
.SILENT:
endif
override CURRENT_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
override DOCKER_MOUNT_SUFFIX ?= consistent
override DOCKER_COMPOSE_ARGS ?= -f deployments/docker-compose/docker-compose.yml -f deployments/docker-compose/docker-compose-local.yml
override DOCKER_BUILD_ARGS ?= -f ${ROOT_DIR}/build/docker/app/Dockerfile
override GO_PATH = $(shell echo "$(GOPATH)" | cut -d';' -f1 | sed -e "s~^\(.\):~/\1~g" -e "s~\\\~/~g" )
TAG ?= latest
CACHE_TAG ?= unknown_cache
GOOS ?= linux
GOARCH ?= amd64
CGO_ENABLED ?= 0
DIND_UID ?= 0
DING_GUID ?= 0
ifeq ($(GO111MODULE),auto)
override GO111MODULE = on
endif
ifeq ($(OS),Windows_NT)
override ROOT_DIR = $(shell echo $(CURRENT_DIR) | sed -e "s:^/./:\U&:g")
else
override ROOT_DIR = $(CURRENT_DIR)
endif
define go_docker
if [ "${GO_PATH}" != "" ]; then VOLUME_PKG_MOD="-v /${GO_PATH}/pkg/mod:/${GO_PATH}/pkg/mod:${DOCKER_MOUNT_SUFFIX}"; fi ;\
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
docker run --rm \
-v /${ROOT_DIR}:/${ROOT_DIR}:${DOCKER_MOUNT_SUFFIX} \
$${VOLUME_PKG_MOD} \
-w /${ROOT_DIR} \
-e GOPATH=/${GO_PATH}:/go \
$${GO_IMAGE}:$${GO_IMAGE_TAG} \
sh -c 'GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=${CGO_ENABLED} TAG=${TAG} $(subst ",,${1}); if [ "${DIND_UID}" != "0" ]; then chown -R ${DIND_UID}:${DIND_GUID} ${ROOT_DIR}; fi'
endef
up: clean ## initialize required tools
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
(docker network inspect $${DOCKER_NETWORK} &>/dev/null && echo "Docker network \"$${DOCKER_NETWORK}\" already created") || \
(echo "Create docker network \"$${DOCKER_NETWORK}\"" && docker network create $${DOCKER_NETWORK})
if [ "${DIND}" != "1" ]; then \
go get github.com/google/wire/cmd/[email protected] && \
go get github.com/99designs/[email protected] && \
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint && \
go get github.com/vektah/[email protected] ;\
fi;
.PHONY: up
down: dev-docker-compose-down clean ## reset to zero setting
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
(docker network inspect $${DOCKER_NETWORK} &>/dev/null && \
(echo "Delete docker network" && docker network rm $${DOCKER_NETWORK}) || echo "Docker network \"$${DOCKER_NETWORK}\" already deleted")
.PHONY: down
build: ## build application
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make build") ;\
else \
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=${CGO_ENABLED} \
go build -ldflags "-X $${GO_PKG}/cmd/version.appVersion=$(TAG)-$$(date -u +%Y%m%d%H%M)" -o "$(ROOT_DIR)/bin" main.go ;\
fi;
.PHONY: build
clean: ## remove generated files, tidy vendor dependencies
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make clean") ;\
else \
go mod tidy ;\
rm -rf *.out generated/* vendor bin ;\
fi;
.PHONY: clean
dev-build-up: build docker-image-cache dev-docker-compose-up ## create new build and recreate containers in docker-compose
.PHONY: dev-build-up
dev-docker-compose-down: ## stop and remove containers, networks, images, and volumes
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
TAG=${TAG} DOCKER_NETWORK=$${DOCKER_NETWORK} docker-compose -p $${PROJECT_NAME} ${DOCKER_COMPOSE_ARGS} down -v
.PHONY: dev-docker-compose-down
dev-docker-compose-up: ## create and start containers
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
TAG=${TAG} DOCKER_NETWORK=$${DOCKER_NETWORK} docker-compose -p $${PROJECT_NAME} ${DOCKER_COMPOSE_ARGS} up -d
.PHONY: dev-docker-compose-up
dev-test: test lint ## test application in dev env with race and lint
.PHONY: dev-test
dind: ## useful for windows
if [ "${GO_PATH}" != "" ]; then VOLUME_PKG_MOD="-v /${GO_PATH}/pkg/mod:/${GO_PATH}/pkg/mod:${DOCKER_MOUNT_SUFFIX}"; fi ;\
if [ "${DIND}" = "1" ]; then \
echo "Already in DIND" ;\
else \
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
docker rm -f dind-$${PROJECT_NAME} &>/dev/null ;\
docker run -it --rm --name dind-$${PROJECT_NAME} --privileged \
-v //var/run/docker.sock://var/run/docker.sock:${DOCKER_MOUNT_SUFFIX} \
-v /${ROOT_DIR}:/${ROOT_DIR}:${DOCKER_MOUNT_SUFFIX} \
$${VOLUME_PKG_MOD} \
-w /${ROOT_DIR} \
-e GOPATH=${GO_PATH} \
$${DIND_IMAGE}:$${DIND_IMAGE_TAG} ;\
fi;
.PHONY: dind
docker-clean: ## delete previous docker image build
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
docker rmi $${DOCKER_IMAGE}:${CACHE_TAG} || true ;\
docker rmi $${DOCKER_IMAGE}:${TAG} || true
.PHONY: docker-clean
docker-image-cache: ## build docker image and tagged as cache
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
docker build --cache-from $${DOCKER_IMAGE}:${CACHE_TAG} ${DOCKER_BUILD_ARGS} -t $${DOCKER_IMAGE}:${TAG} -t $${DOCKER_IMAGE}:${CACHE_TAG} ${ROOT_DIR}
.PHONY: docker-image-cache
docker-image: ## build docker image
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
docker build --cache-from $${DOCKER_IMAGE}:${CACHE_TAG} ${DOCKER_BUILD_ARGS} -t $${DOCKER_IMAGE}:${TAG} ${ROOT_DIR}
.PHONY: docker-image
docker-protoc-generate: ## generate proto, grpc client & server
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
docker run --rm \
-v /${ROOT_DIR}/api:/${ROOT_DIR}/api:${DOCKER_MOUNT_SUFFIX} \
-v /${ROOT_DIR}/generated:/${ROOT_DIR}/generated:${DOCKER_MOUNT_SUFFIX} \
-v /${ROOT_DIR}/configs/prototool.yaml:/${ROOT_DIR}/prototool.yaml:${DOCKER_MOUNT_SUFFIX} \
-w /${ROOT_DIR} \
$${PROTOTOOL_IMAGE}:$${PROTOTOOL_IMAGE_TAG} \
prototool generate api/proto
.PHONY: docker-protoc-generate
docker-push: ## push docker image to registry
. ${ROOT_DIR}/scripts/common.sh ${ROOT_DIR}/scripts ;\
docker push $${DOCKER_IMAGE}:${TAG}
.PHONY: docker-push
generate: docker-protoc-generate gqlgen-generate go-generate ## execute all generators
.PHONY: generate
github-build: docker-image docker-push docker-clean ## build application in CI
.PHONY: github-build
github-test: test-with-coverage ## test application in CI
.PHONY: github-test
go-depends: ## view final versions that will be used in a build for all direct and indirect dependencies
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make go-depends") ;\
else \
cd $(ROOT_DIR) ;\
go list -m all ;\
fi;
.PHONY: go-depends
go-generate: ## go generate
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make go-generate") ;\
else \
cd $(ROOT_DIR) ;\
go generate $$(go list ./...) || exit 1 ;\
fi;
.PHONY: go-generate
go-update-all: ## view available minor and patch upgrades for all direct and indirect
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make go-update-all") ;\
else \
cd $(ROOT_DIR) ;\
go list -u -m all ;\
fi;
.PHONY: go-update-all
gqlgen-generate: ## generate graphql server
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make gqlgen-generate") ;\
else \
go run github.com/99designs/gqlgen -v -c $(ROOT_DIR)/configs/gqlgen.yml ;\
fi;
.PHONY: gqlgen-generate
lint: ## execute linter
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make lint") ;\
else \
golangci-lint run --no-config --issues-exit-code=0 --deadline=30m \
--disable-all --enable=deadcode --enable=gocyclo --enable=golint --enable=varcheck \
--enable=structcheck --enable=maligned --enable=errcheck --enable=dupl --enable=ineffassign \
--enable=interfacer --enable=unconvert --enable=goconst --enable=gosec --enable=megacheck ./... ;\
fi;
.PHONY: lint
test-with-coverage: ## test application with race and total coverage
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make test-with-coverage") ;\
else \
export WD=$(ROOT_DIR) ;\
CGO_ENABLED=1 \
go test -v -race -covermode atomic -coverprofile coverage.out ${TEST_ARGS} ./... || exit 1 ;\
go tool cover -func=coverage.out ;\
fi;
.PHONY: test-with-coverage
test: ## test application with race
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make test") ;\
else \
export WD=$(ROOT_DIR) ;\
CGO_ENABLED=1 \
go test -race -v ${TEST_ARGS} ./... ;\
fi;
.PHONY: test
vendor: ## update vendor dependencies
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make vendor") ;\
else \
rm -rf $(ROOT_DIR)/vendor ;\
go mod vendor ;\
fi;
.PHONY: vendor
go-download-deps: ## download dependencies
if [ "${DIND}" = "1" ]; then \
$(call go_docker,"make go-download-deps") ;\
else \
go get -d ./... ;\
fi;
.PHONY: go-download-deps
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: help
.DEFAULT_GOAL := help