-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
291 lines (244 loc) · 10.3 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
ENV_FILE = ./docker/.dev.env
include $(ENV_FILE)
POSTGRES_CONNECTION = postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):$(POSTGRES_PORT)/$(POSTGRES_DB)?sslmode=disable
ENTRY_PATH = ./cmd/main.go
MIGRATIONS_PATH = ./internal/db/$*/migrations
BINARY_PATH = ./bin/main
DOCKER_COMPOSE_PATH = ./docker/docker-compose.$(ENV).yaml
GOLANGCI_LINT_PATH = ./.golangci.yaml
# use `gawk` on mac os
AWK := awk
ifeq ($(shell uname -s), Darwin)
AWK = gawk
ifeq (, $(shell which gawk 2> /dev/null))
$(error "gawk not found")
endif
endif
################################################################################
# Miscellaneous
################################################################################
.PHONY: help
## (default) Show help page.
help:
@echo "$$(tput bold)Available rules:$$(tput sgr0)";echo;sed -ne"/^## /{h;s/.*//;:d" -e"H;n;s/^## //;td" -e"s/:.*//;G;s/\\n## /---/;s/\\n/ /g;p;}" ${MAKEFILE_LIST}|awk -F --- -v n=$$(tput cols) -v i=29 -v a="$$(tput setaf 6)" -v z="$$(tput sgr0)" '{printf"%s%*s%s ",a,-i,$$1,z;m=split($$2,w," ");l=n-i;for(j=1;j<=m;j++){l-=length(w[j])+1;if(l<= 0){l=n-i-length(w[j])-1;printf"\n%*s ",-i," ";}printf"%s ",w[j];}printf"\n\n";}'
.PHONY: api
## Generate api docs in swagger format.
api:
@swag init -g $(ENTRY_PATH)
################################################################################
# Local Development
################################################################################
.PHONY: tidy
## Remove unused and add missing dependencies.
tidy:
@go mod tidy
.PHONY: vendor
## Create `vendor` directory that contains copies of all dependencies
## listed in `go.mod` file.
vendor:
@go mod vendor
.PHONY: download
## Download modules specified in `go.mod` file.
download:
@go mod download
.PHONY: build
##@ Build microservice locally.
build: tidy vendor
@go build -mod vendor -o $(BINARY_PATH) $(ENTRY_PATH)
.PHONY: run
## Run microservice locally.
run:
@mkdir -p bin
@go run $(ENTRY_PATH)
.PHONY: exec
## Build microservice locally and run binary.
exec: build
@$(BINARY_PATH)
################################################################################
# Testing
################################################################################
.PHONY: test
## Run unit tests and generate code coverage report (`./coverage.out`).
test:
@go clean -testcache
@go test ./... -coverprofile=coverage.out
.PHONY: --check-coverage
--check-coverage:
@if [ ! -f coverage.out ]; then \
echo "coverage does not exist, running tests..."; \
$(MAKE) test; \
fi
.PHONY: view-coverage
## View code coverage report if it exists otherwise generate.
view-coverage: --check-coverage
@go test -coverpkg=./... -coverprofile=coverage.out.tmp ./...
@cat coverage.out.tmp | grep -v "mock\|cmd\|config\|internal\|docs\|metrics\|pkg\|routes\|proto\|easyjson" > coverage.out
@go tool cover -func=coverage.out
.PHONY: view-coverage-html
## View code coverage report in browser if it exists otherwise generate.
view-coverage-html: --check-coverage
@go tool cover -html=coverage.out -o coverage.html
@open coverage.html
################################################################################
# Formatting & Linting
################################################################################
.PHONY: format
## Format source code.
format:
@gofmt -s -w .
.PHONY: lint
## Check source code with `golangci-lint` linter.
lint:
@golangci-lint run --config $(GOLANGCI_LINT_PATH) ./...
################################################################################
# Containers
################################################################################
.PHONY: docker-build
## Build docker container with microservice binary.
docker-build:
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build
.PHONY: docker-migrate
## Start docker compose service of database and apply migrations.
docker-migrate:
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) up -d migrations
.PHONY: docker-start
## Start docker compose containers (all by default).
## Format: `docker-start [compose=<docker-compose-service>]`.
## Example: `docker-start`, `docker-stop compose=postgres`.
docker-start:
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) up -d $(compose)
.PHONY: docker-build-start
## Build docker container and start containers within one command.
docker-build-start: docker-build docker-start
.PHONY: docker-stop
## Stop docker compose containers (all by default).
## Format: `docker-stop [compose=<docker-compose-service>]`.
## Example: `docker-stop`, `docker-stop compose=postgres`.
docker-stop:
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) stop $(compose)
.PHONY: docker-ash
## Run `ash` in docker container of microservice.
docker-ash:
@docker exec -it $(SERVICE_NAME) /bin/ash
.PHONY: docker-psql
## Run `psql` in docker container of postgres.
docker-psql:
@docker exec -it $(SERVICE_NAME)-postgres psql $(POSTGRES_CONNECTION)
.PHONY: docker-clean
## Remove containers, networks, volumes, and images created by `make docker-start`.
docker-clean:
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) down
.PHONY: build-image
## Build docker image of microservice with name.
build-image:
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build $(SERVICE_NAME)-user
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build $(SERVICE_NAME)-playlist
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build $(SERVICE_NAME)-artist
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build $(SERVICE_NAME)-album
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build $(SERVICE_NAME)-track
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build $(SERVICE_NAME)-csat
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build $(SERVICE_NAME)-genre
.PHONY: push-image
## Push docker image of microservice to the docker hub.
push-image:
@docker push daronenko/$(SERVICE_NAME)-user:$(USER_VERSION)
@docker push daronenko/$(SERVICE_NAME)-playlist:$(PLAYLIST_VERSION)
@docker push daronenko/$(SERVICE_NAME)-artist:$(CSAT_VERSION)
@docker push daronenko/$(SERVICE_NAME)-album:$(ARTIST_VERSION)
@docker push daronenko/$(SERVICE_NAME)-track:$(ALBUM_VERSION)
@docker push daronenko/$(SERVICE_NAME)-csat:$(GENRE_VERSION)
@docker push daronenko/$(SERVICE_NAME)-genre:$(TRACK_VERSION)
################################################################################
# Cleaning
################################################################################
.PHONY: clean
## Remove object, cached, and binary files of microservice.
clean:
@go clean
@rm -rf $(BINARY_PATH)
.PHONY: clean-test
## Remove test cache and code coverage report.
clean-test:
@go clean -testcache
@rm -f coverage.*
.PHONY: clean-all
## Run `make clean` and `make clean-test` commands.
clean-all: clean clean-test
################################################################################
# Database Migrations
################################################################################
.PHONY: %-state
## Show the list of applied migrations to the database.
## Available databases: postgres, mysql, sqlite3, mssql, redshift, tidb,
## clickhouse, vertica, ydb.
## Format: `<database>-state`.
## Example: `postgres-state`.
%-state:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) status
.PHONY: %-migration
## Create migrations with specifed name and type (`sql` - default, `go`).
## Available databases: postgres, mysql, sqlite3, mssql, redshift, tidb,
## clickhouse, vertica, ydb.
## Format: `<database>-migration name=<name> [type=<sql|go>]`.
## Example: `postgres-migration name=add_some_column type=sql`,
## `postgres-migration name=create_table type=go`.
%-migration:
$(eval type := $(or $(type), sql))
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) create $(name) $(type)
.PHONY: %-migrate
## Apply all available migrations to the database.
## Available databases: postgres, mysql, sqlite3, mssql, redshift, tidb,
## clickhouse, vertica, ydb.
## Format: `<database>-migrate`.
## Example: `postgres-migrate`.
%-migrate:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) up
.PHONY: %-migrate-to
## Migrate up to a specific version.
## Available databases: postgres, mysql, sqlite3, mssql, redshift, tidb,
## clickhouse, vertica, ydb.
## Format: `<database>-migrate-to version=<version>`.
## Example: `postgres-migrate-to version=20170506082420`.
%-migrate-to:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) up-to $(version)
.PHONY: %-rollback
## Roll back a single migration from the current version.
## Available databases: postgres, mysql, sqlite3, mssql, redshift, tidb,
## clickhouse, vertica, ydb.
## Format: `<database>-rollback`.
## Example: `postgres-rollback`.
%-rollback:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) down
.PHONY: %-rollback-to
## Roll back migrations to a specific version.
## Available databases: postgres, mysql, sqlite3, mssql, redshift, tidb,
## clickhouse, vertica, ydb.
## Format: `<database>-rollback-to version=20170506082527`.
## Example: `postgres-rollback-to version=20170506082527`.
%-rollback-to:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) down-to $(version)
.PHONY: %-reset
## Roll back all migrations.
## Available databases: postgres, mysql, sqlite3, mssql, redshift, tidb,
## clickhouse, vertica, ydb.
## Format: `<database>-reset`.
## Example: `postgres-reset`.
%-reset:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) reset
################################################################################
# Codegen
################################################################################
.PHONY: generate
## Create easyjson unmarshalers for structs with ////easyjson:json flag
generate:
@find . -type f -name '*_easyjson.go' -delete
@FILES=$$(find . -type f -name "*dto.go" -o -wholename "*/models/*.go" -o -wholename "*/utils/response.go"); \
for file in $$FILES; do \
easyjson $$file; \
done