-
Notifications
You must be signed in to change notification settings - Fork 10
/
Makefile
251 lines (204 loc) · 13.8 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
242
243
244
245
246
247
248
249
250
251
## Set defaults
export GO111MODULE := on
# Fetch OS info
GOVERSION=$(shell go version)
UNAME_OS=$(shell go env GOOS)
UNAME_ARCH=$(shell go env GOARCH)
METRO_OUT := "bin/metro"
METRO_MAIN_FILE := "cmd/service/main.go"
GIT_HOOKS_DIR := "scripts/git_hooks"
TMP_DIR := ".tmp"
DOCS_DIR := "docs"
UML_OUT_FILE := "uml_graph.puml"
PKG_LIST_TMP_FILE := "app.packages"
UNIT_COVERAGE_TMP_FILE := "app-unit.cov"
INTG_COVERAGE_TMP_FILE := "app-integration.cov"
UNIT_TEST_EXCLUSIONS_FILE := "unit-test.exclusions"
# Proto gen info
PROTO_ROOT := "metro-proto/"
RPC_ROOT := "rpc/"
BUF_BIN := /usr/local/bin
BUF_VERSION := 0.43.2
BUF_BINARY_NAME := buf
BUF_UNAME_OS := $(shell uname -s)
BUF_UNAME_ARCH := $(shell uname -m)
# go binary. Change this to experiment with different versions of go.
GO = go
VERBOSE = 0
Q = $(if $(filter 1,$VERBOSE),,@)
M = $(shell printf "\033[34;1m▶\033[0m")
BIN = $(CURDIR)/bin
PKGS = $(or $(PKG),$(shell $(GO) list ./...))
$(BIN)/%: | $(BIN) ; $(info $(M) building package: $(PACKAGE)…)
tmp=$$(mktemp -d); \
env GOBIN=$(BIN) go get $(PACKAGE) \
|| ret=$$?; \
rm -rf $$tmp ; exit $$ret
$(BIN)/goimports: PACKAGE=golang.org/x/tools/cmd/[email protected]
GOIMPORTS = $(BIN)/goimports
GOFILES ?= $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./statik/*" -not -path "./rpc/*" -not -path "*/mocks/*")
.PHONY: goimports ## Run goimports and format files
goimports: | $(GOIMPORTS) ; $(info $(M) running goimports…) @
$Q $(GOIMPORTS) -w $(GOFILES)
.PHONY: goimports-check ## Check goimports without modifying the files
goimports-check: | $(GOIMPORTS) ; $(info $(M) running goimports -l …) @
$(eval FILES=$(shell sh -c '$(GOIMPORTS) -l $(GOFILES)'))
@$(if $(strip $(FILES)), echo $(FILES); exit 1, echo "goimports check passed")
$(BIN)/golint: PACKAGE=golang.org/x/lint/golint
GOLINT = $(BIN)/golint
.PHONY: lint-check ## Run golint check
lint-check: | $(GOLINT) ; $(info $(M) running golint…) @
$Q $(GOLINT) -set_exit_status $(PKGS)
.PHONY: setup-git-hooks ## First time setup
setup-git-hooks:
@chmod +x $(GIT_HOOKS_DIR)/*
@git config core.hooksPath $(GIT_HOOKS_DIR)
.PHONY: deps ## Fetch dependencies
deps: buf-deps
@echo "\n + Fetching buf dependencies \n"
# https://github.com/johanbrandhorst/grpc-gateway-boilerplate/blob/master/Makefile
@go install google.golang.org/protobuf/cmd/[email protected]
@go install google.golang.org/grpc/cmd/[email protected]
@go install github.com/grpc-ecosystem/grpc-gateway/v2/[email protected]
@go install github.com/grpc-ecosystem/grpc-gateway/v2/[email protected]
@go install github.com/rakyll/[email protected]
@go install golang.org/x/lint/golint@latest
@go install github.com/bykof/[email protected]
@go install github.com/golang/mock/[email protected]
buf-deps:
curl -sSL \
https://github.com/bufbuild/buf/releases/download/v${BUF_VERSION}/${BUF_BINARY_NAME}-$(BUF_UNAME_OS)-$(BUF_UNAME_ARCH) \
-o ${BUF_BIN}/${BUF_BINARY_NAME} && \
chmod +x ${BUF_BIN}/${BUF_BINARY_NAME}
.PHONY: proto-generate ## Compile protobuf to pb files
proto-generate:
@echo "\n + Generating pb language bindings\n"
@buf generate --path ./metro-proto/metro/proto
# Substitute {param=...} path params into {param} to solve swagger substitution problem
# We add #header tags to work around OpenAPI limitations in detecting dunamic paths
@sed -E "s/\{([a-zA-Z0-9\.]+)=([a-zA-Z0-9]+)([/])([\*])\/([a-zA-Z0-9]+)([/])([\*])(.*)\}([:a-zA-Z0-9]*)/{\1}\9#\5/g" third_party/OpenAPI/proto/v1/spec.swagger.json > third_party/OpenAPI/proto/v1/spec.swagger.json.tmp
@rm third_party/OpenAPI/proto/v1/spec.swagger.json
@mv third_party/OpenAPI/proto/v1/spec.swagger.json.tmp third_party/OpenAPI/proto/v1/spec.swagger.json
# Generate static assets for OpenAPI UI
@statik -m -f -src third_party/OpenAPI/proto/v1
.PHONY: proto-clean ## Clean generated proto files
proto-clean:
@rm -rf $(RPC_ROOT)
.PHONY: proto-refresh ## Re-compile protobuf
proto-refresh: clean proto-generate
.PHONY: build-info
build-info:
@echo "\nBuild Info:\n"
@echo "\t\033[33mOS\033[0m: $(UNAME_OS)"
@echo "\t\033[33mArch\033[0m: $(UNAME_ARCH)"
@echo "\t\033[33mGo Version\033[0m: $(GOVERSION)\n"
.PHONY: go-build-metro ## Build the binary file for API server
go-build-metro:
@CGO_ENABLED=1 GOOS=$(UNAME_OS) GOARCH=$(UNAME_ARCH) go build -tags musl -v -o $(METRO_OUT) $(METRO_MAIN_FILE)
.PHONY: clean ## Remove mocks, previous builds, protobuf files, and proto compiled code
clean: mock-gen-clean proto-clean
@echo " + Removing cloned and generated files\n"
##- todo: use go clean here
@rm -rf $(METRO_OUT) $(TMP_DIR)/* $(DOCS_DIR)/$(UML_OUT_FILE)
.PHONY: dev-docker-up ## Bring up docker compose for local dev-setup
dev-docker-up:
docker compose -f deployment/dev/monitoring/docker-compose.yml up -d
docker compose -f deployment/dev/docker-compose.yml up -d --build
.PHONY: dev-docker-datastores-up ## Bring up datastore containers
dev-docker-datastores-up:
docker compose -f deployment/dev/docker-compose-datastores.yml up -d
.PHONY: dev-docker-datastores-down ## Shut down datastore containers
dev-docker-datastores-down:
docker compose -f deployment/dev/docker-compose-datastores.yml down
.PHONY: dev-docker-down ## Shutdown docker-compose for local dev-setup
dev-docker-down:
@docker compose -f deployment/dev/docker-compose.yml down
@docker compose -f deployment/dev/monitoring/docker-compose.yml down
.PHONY: dev-docker-emulator-up ## Bring up google pub/sub emulator
dev-docker-emulator-up:
docker compose -f deployment/dev/docker-compose-emulator.yml up -d
.PHONY: dev-docker-emulator-down ## Bring down google pub/sub emulator
dev-docker-emulator-down:
docker compose -f deployment/dev/docker-compose-emulator.yml down
.PHONY: docker-build-metro
docker-build-metro:
@docker build . -f build/docker/Dockerfile --build-arg GIT_TOKEN=${GIT_TOKEN} -t razorpay/metro:latest
.PHONY: dev-up # Run metro component without docker
dev-up: go-build-metro
APP_ENV=dev ./bin/metro --component $(app)
.PHONY: mock-gen ## Generates mocks
mock-gen:
@go generate ./...
@mockgen --build_flags='--tags=musl' -destination=internal/brokerstore/mocks/mock_brokerstore.go -package=mocks github.com/razorpay/metro/internal/brokerstore IBrokerStore
@mockgen --build_flags='--tags=musl' -destination=pkg/messagebroker/mocks/mock_broker.go -package=mocks github.com/razorpay/metro/pkg/messagebroker Broker
@mockgen --build_flags='--tags=musl' -destination=pkg/messagebroker/mocks/mock_admin.go -package=mocks github.com/razorpay/metro/pkg/messagebroker Admin
@mockgen --build_flags='--tags=musl' -destination=pkg/messagebroker/mocks/mock_consumer.go -package=mocks github.com/razorpay/metro/pkg/messagebroker Consumer
@mockgen --build_flags='--tags=musl' -destination=pkg/messagebroker/mocks/mock_producer.go -package=mocks github.com/razorpay/metro/pkg/messagebroker Producer
@mockgen --build_flags='--tags=musl' -destination=internal/topic/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/topic ICore
@mockgen --build_flags='--tags=musl' -destination=internal/topic/mocks/repo/mock_repo.go -package=mocks github.com/razorpay/metro/internal/topic IRepo
@mockgen --build_flags='--tags=musl' -destination=internal/subscription/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/subscription ICore
@mockgen --build_flags='--tags=musl' -destination=internal/subscription/mocks/repo/mock_repo.go -package=mocks github.com/razorpay/metro/internal/subscription IRepo
@mockgen --build_flags='--tags=musl' -destination=internal/subscriber/mocks/mock_core.go -package=mocks github.com/razorpay/metro/internal/subscriber ICore
@mockgen --build_flags='--tags=musl' -destination=internal/subscriber/mocks/mock_subscriber.go -package=mocks github.com/razorpay/metro/internal/subscriber ISubscriber
@mockgen --build_flags='--tags=musl' -destination=internal/project/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/project ICore
@mockgen --build_flags='--tags=musl' -destination=internal/project/mocks/repo/mock_repo.go -package=mocks github.com/razorpay/metro/internal/project IRepo
@mockgen --build_flags='--tags=musl' -destination=internal/offset/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/offset ICore
@mockgen --build_flags='--tags=musl' -destination=internal/offset/mocks/repo/mock_repo.go -package=mocks github.com/razorpay/metro/internal/offset IRepo
@mockgen --build_flags='--tags=musl' -destination=internal/node/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/node ICore
@mockgen --build_flags='--tags=musl' -destination=internal/node/mocks/repo/mock_repo.go -package=mocks github.com/razorpay/metro/internal/node IRepo
@mockgen --build_flags='--tags=musl' -destination=internal/nodebinding/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/nodebinding ICore
@mockgen --build_flags='--tags=musl' -destination=internal/nodebinding/mocks/repo/mock_repo.go -package=mocks github.com/razorpay/metro/internal/nodebinding IRepo
@mockgen --build_flags='--tags=musl' -destination=internal/credentials/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/credentials ICore
@mockgen --build_flags='--tags=musl' -destination=internal/credentials/mocks/repo/mock_repo.go -package=mocks github.com/razorpay/metro/internal/credentials IRepo
@mockgen --build_flags='--tags=musl' -destination=internal/publisher/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/publisher ICore
@mockgen --build_flags='--tags=musl' -destination=internal/publisher/mocks/publisher/mock_publisher.go -package=mocks github.com/razorpay/metro/internal/publisher IPublisher
@mockgen --build_flags='--tags=musl' -destination=internal/health/mocks/core/mock_core.go -package=mocks github.com/razorpay/metro/internal/health ICore
@mockgen --build_flags='--tags=musl' -destination=pkg/registry/mocks/mock_registry.go -package=mocks github.com/razorpay/metro/pkg/registry IRegistry
@mockgen --build_flags='--tags=musl' -destination=pkg/registry/mocks/mock_watcher.go -package=mocks github.com/razorpay/metro/pkg/registry IWatcher
@mockgen --build_flags='--tags=musl' -destination=pkg/cache/mocks/mock_cache.go -package=mocks github.com/razorpay/metro/pkg/cache ICache
@mockgen --build_flags='--tags=musl' -destination=service/web/stream/mocks/manager/mock_manager.go -package=mocks github.com/razorpay/metro/service/web/stream IManager
@mockgen --build_flags='--tags=musl' -destination=internal/scheduler/mocks/mock_scheduler.go -package=mocks github.com/razorpay/metro/internal/scheduler IScheduler
@mockgen --build_flags='--tags=musl' -destination=internal/tasks/mocks/mock_task.go -package=mocks github.com/razorpay/metro/internal/tasks ITask
@mockgen --build_flags='--tags=musl' -destination=internal/subscriber/retry/mocks/mock_handler.go -package=mocks github.com/razorpay/metro/internal/subscriber/retry MessageHandler
@mockgen --build_flags='--tags=musl' -destination=internal/subscriber/retry/mocks/mock_retrier.go -package=mocks github.com/razorpay/metro/internal/subscriber/retry IRetrier
.PHONY: mock-gen-clean ## Clean up all mockgen generated mocks directories
mock-gen-clean:
@find . -type d -name 'mocks' | xargs rm -rf
.PHONY: docs-uml ## Generates UML file
docs-uml:
@go-plantuml generate --recursive --directories cmd --directories internal --directories pkg --out $(DOCS_DIR)/$(UML_OUT_FILE)
docs-gh:
ruby -v
bundle -v
cd docs; bundle install
cd docs; bundle exec jekyll serve
.PHONY: test-functional-ci ## run functional tests on ci (github actions)
test-functional-ci:
@METRO_TEST_HOST=metro-web APP_ENV=dev_docker go test ./tests/functional/... -tags=functional,musl
.PHONY: test-functional ## run integration tests locally (metro service needs to be up)
test-functional:
@METRO_TEST_HOST=localhost go test --count=1 ./tests/functional/... -tags=functional,musl
.PHONY: test-integration-ci ## run integration tests on ci (github actions)
test-integration-ci: test-unit-prepare
@METRO_TEST_HOST=metro-web KAFKA_TEST_HOST=kafka-broker go test ./tests/integration/... -tags=integration,musl -timeout 2m -coverpkg=$(shell comm -23 $(TMP_DIR)/$(PKG_LIST_TMP_FILE) $(UNIT_TEST_EXCLUSIONS_FILE) | xargs | sed -e 's/ /,/g') -coverprofile=$(TMP_DIR)/$(INTG_COVERAGE_TMP_FILE)
.PHONY: test-integration ## run integration tests locally (metro service needs to be up)
test-integration: test-unit-prepare
@METRO_TEST_HOST=localhost KAFKA_TEST_HOST=localhost go test ./tests/integration/... --count=1 -tags=integration,musl -timeout 2m -coverpkg=$(shell comm -23 $(TMP_DIR)/$(PKG_LIST_TMP_FILE) $(UNIT_TEST_EXCLUSIONS_FILE) | xargs | sed -e 's/ /,/g') -coverprofile=$(TMP_DIR)/$(INTG_COVERAGE_TMP_FILE)
.PHONY: test-compat-ci ## run compatibility tests on ci (github actions)
test-compat-ci:
@METRO_TEST_HOST=metro-web PUBSUB_TEST_HOST=pubsub APP_ENV=dev_docker go test -v ./tests/compatibility/... -tags=compatibility,musl
.PHONY: test-compat ## run compatibility tests locally (metro service and pubsub emulator needs to be up)
test-compat:
@METRO_TEST_HOST=localhost PUBSUB_TEST_HOST=localhost go test --count=1 -v ./tests/compatibility/... -tags=compatibility,musl
.PHONY: test-unit-prepare
test-unit-prepare:
@mkdir -p $(TMP_DIR)
@go list ./... | grep -Ev 'tests|mocks|statik|rpc' > $(TMP_DIR)/$(PKG_LIST_TMP_FILE)
.PHONY: test-unit ## Run unit tests
test-unit: test-unit-prepare
@APP_ENV=dev_docker go test --count=1 -tags=unit,musl -timeout 2m -coverpkg=$(shell comm -23 $(TMP_DIR)/$(PKG_LIST_TMP_FILE) $(UNIT_TEST_EXCLUSIONS_FILE) | xargs | sed -e 's/ /,/g') -coverprofile=$(TMP_DIR)/$(UNIT_COVERAGE_TMP_FILE) ./...
@go tool cover -func=$(TMP_DIR)/$(UNIT_COVERAGE_TMP_FILE)
.PHONY: help ## Display this help screen
help:
@echo "Usage:"
@grep -E '^\.PHONY: [a-zA-Z_-]+.*?## .*$$' $(MAKEFILE_LIST) | sort | sed 's/\.PHONY\: //' | awk 'BEGIN {FS = " ## "}; {printf "\t\033[36m%-30s\033[0m %s\n", $$1, $$2}'