-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
245 lines (200 loc) · 7.87 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
#
# swis-api (swapi) / Makefile
#
#
# VARS
#
# load example variables/constants, override by .env file if exists
include .env.example
-include .env
APP_ENVIRONMENT?=development
PROJECT_NAME?=${APP_NAME}
DOCKER_COMPOSE_FILE?=deployments/docker-compose.yml
DOCKER_COMPOSE_OVERRIDE?=deployments/docker-compose.override.yml
DOCKER_COMPOSE_DEV_FILE?=deployments/docker-compose.dev.yml
DOCKER_COMPOSE_DEV_OVERRIDE?=deployments/docker-compose.dev.override.yml
SWAG_BINARY?=~/go/bin/swag
ALPINE_VERSION?=3.19
GOLANG_VERSION?=1.22
APP_URL?=swapi.example.com
LOKI_URL?=loki.example.com/loki/api/v1/push
ROOT_TOKEN?=${ROOT_TOKEN_DEFAULT}
GIN_MODE?=debug
#CF_API_EMAIL?=
#CF_API_TOKEN?=
#CF_BEARER_TOKEN?=
GOARCH := $(shell go env GOARCH)
GOCACHE?=/home/${USER}/.cache/go-build
GOMODCACHE?=/home/${USER}/go/pkg/mod
GOOS := $(shell go env GOOS)
PATH := ${PATH}:/usr/bin
# test env
POSTMAN_COLLECTION_FILE=test/postman/swapi_E2E_dish.postman_collection.json
HOSTNAME?=localhost
ROOT_TOKEN_TEST=fsdFD33FdsfK3dcc00Wef223kffDSrrrr
# define standard colors
# https://gist.github.com/rsperl/d2dfe88a520968fbc1f49db0a29345b9
ifneq (,$(findstring xterm,${TERM}))
BLACK := $(shell tput -Txterm setaf 0)
RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
LIGHTPURPLE := $(shell tput -Txterm setaf 4)
PURPLE := $(shell tput -Txterm setaf 5)
BLUE := $(shell tput -Txterm setaf 6)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
else
BLACK := ""
RED := ""
GREEN := ""
YELLOW := ""
LIGHTPURPLE := ""
PURPLE := ""
BLUE := ""
WHITE := ""
RESET := ""
endif
export
#
# TARGETS
#
.PHONY: info
info:
@echo -e "\n${GREEN} ${PROJECT_NAME} / Makefile ${RESET}\n"
@echo -e "${YELLOW} make${RESET} --- ${BLUE}show this helper ${RESET}\n"
@echo -e "${YELLOW} make fmt${RESET} --- ${BLUE}reformat the go source (gofmt) ${RESET}"
@echo -e "${YELLOW} make docs${RESET} --- ${BLUE}render documentation from code (swagger OA docs) ${RESET}\n"
@echo -e "${YELLOW} make build${RESET} --- ${BLUE}build project (docker image) ${RESET}"
@echo -e "${YELLOW} make run${RESET} --- ${BLUE}run project ${RESET}"
@echo -e "${YELLOW} make logs${RESET} --- ${BLUE}fetch container's logs ${RESET}"
@echo -e "${YELLOW} make stop${RESET} --- ${BLUE}stop and purge project (only docker containers!) ${RESET}\n"
@echo -e "${YELLOW} make import_dump${RESET} --- ${BLUE}import dumped data (locally) into runtime ${RESET}"
@echo -e "${YELLOW} make dump${RESET} --- ${BLUE}dump runtime data to DUMP_DIR ${RESET}"
@echo -e "${YELLOW} make backup${RESET} --- ${BLUE}execute data dump and tar/gzip data backup ${RESET}"
@echo -e ""
.PHONY: config
config:
@echo -e "\n${YELLOW} Installing additional tools and packages... ${RESET}\n"
@go install github.com/swaggo/swag/cmd/swag@latest
.PHONY: version
version:
@echo -e "\n${YELLOW} Updating app's version (docs) according to dot-env file... ${RESET}\n"
@/usr/bin/sed -i 's/\(\/\/[ ]@version\) .*/\1 ${APP_VERSION}/' cmd/swis-api/main.go
.PHONY: fmt
fmt:
@echo -e "\n${YELLOW} Code reformating (gofmt)... ${RESET}\n"
@/usr/local/go/bin/gofmt -s -w .
#@find . -name "*.go" -exec gofmt {} \;
.PHONY: build
build: version
@echo -e "\n${YELLOW} Building project (docker compose build)... ${RESET}\n"
@docker compose --file $(DOCKER_COMPOSE_FILE) build
#@docker compose --file $(DOCKER_COMPOSE_FILE) build --no-cache
.PHONY: test
test:
@echo -e "\n${YELLOW} Running tests in all packages (go test)... ${RESET}\n"
go test -count=1 -v ./...
.PHONY: unit
unit: test
.PHONY: bench
bench:
@echo -e "\n${YELLOW} Running benchmark tests (go test)... ${RESET}\n"
go test -bench=. ./...
.PHONY: coverage
coverage:
@echo -e "\n${YELLOW} Running code coverage evaluation (go test)... ${RESET}\n"
go test -coverprofile -v ./...
go tool cover -html=coverage.out
.PHONY: test_deploy
test_deploy:
@echo -e "\n${YELLOW} Starting temporary test container... ${RESET}\n"
@docker run --rm --detach \
--name ${DOCKER_TEST_CONTAINER_NAME} \
-p ${DOCKER_TEST_PORT}:${DOCKER_TEST_PORT} \
-e ROOT_TOKEN=${ROOT_TOKEN_TEST} \
-e SERVER_PORT=${DOCKER_TEST_PORT} \
${REGISTRY}${DOCKER_IMAGE_TAG}
.PHONY: e2e
e2e:
@echo -e "\n${YELLOW} Running Postman collection... ${RESET}\n"
@postman collection run ${POSTMAN_COLLECTION_FILE} --timeout-request 5000 --env-var "token=${ROOT_TOKEN_TEST}" --env-var "baseUrl=${HOSTNAME}:${DOCKER_TEST_PORT}"; \
docker stop ${DOCKER_TEST_CONTAINER_NAME}
APP_URLS_TRAEFIK?=`${APP_URL}`
.PHONY: run
run:
@echo -e "\n${YELLOW} Starting project (docker compose up)... ${RESET}\n"
@[ -f "${DOCKER_COMPOSE_DEV_OVERRIDE}" ] \
&& docker compose --file $(DOCKER_COMPOSE_FILE) --file ${DOCKER_COMPOSE_OVERRIDE} up --force-recreate --remove-orphans --detach \
|| docker compose --file $(DOCKER_COMPOSE_FILE) up --force-recreate --remove-orphans --detach
.PHONY: logs
logs:
@echo -e "\n${YELLOW} Fetching container's logs (CTRL-C to exit)... ${RESET}\n"
@docker logs ${DOCKER_CONTAINER_NAME} --follow
.PHONY: stop
stop:
@echo -e "\n${YELLOW} Stopping and purging project (docker compose down)... ${RESET}\n"
@docker compose --file $(DOCKER_COMPOSE_FILE) down
.PHONY: dev
dev: fmt
@echo -e "\n${YELLOW} Starting local swapi instance... ${RESET}\n"
@[ -f "${DOCKER_COMPOSE_DEV_OVERRIDE}" ] \
&& /usr/bin/docker compose --file $(DOCKER_COMPOSE_DEV_FILE) --file ${DOCKER_COMPOSE_DEV_OVERRIDE} up --force-recreate --remove-orphans --build \
|| /usr/bin/docker compose --file $(DOCKER_COMPOSE_DEV_FILE) up --force-recreate --remove-orphans --build
.PHONY: dump
dump:
@echo -e "\n${YELLOW} Dumping prod data to ${DUMP_DIR}... ${RESET}\n"
@scripts/dump_prod_data.sh
.PHONY: backup
backup: dump
@echo -e "\n${YELLOW} Archiving and compressing dumped data... ${RESET}\n"
@scripts/backup_dumped_files.sh
.PHONY: import_dump
import_dump:
@echo -e "\n${YELLOW} Importing stored data (${DUMP_DIR}) to backend... ${RESET}\n"
@scripts/import_prod_data.sh
.PHONY: migrations
migrations:
@echo -e "\n${YELLOW} Runing migrations... ${RESET}\n"
@scripts/migrate.sh
.PHONY: push
push:
@echo -e "\n${YELLOW} (re)tagging project and pushing to $(git branch --show-current) branch... ${RESET}\n"
@/usr/bin/git tag -fa v${APP_VERSION} -m "v${APP_VERSION}"
@/usr/bin/git push --set-upstream origin $(git branch --show-current) --follow-tags --force
.PHONY: docs
docs:
@echo -e "\n${YELLOW} Regenerating documentation for swagger and rebuilding binary file... ${RESET}\n"
@go install github.com/swaggo/swag/cmd/swag@latest
@${SWAG_BINARY} init --parseDependency -ot json -g ./cmd/swis-api/main.go --dir . && mv docs/swagger.json api/
@docker compose --file $(DOCKER_COMPOSE_FILE) up swagger_ui --detach --force-recreate
.PHONY: sh
sh:
@echo -e "\n${YELLOW} Attaching container's (${DOCKER_CONTAINER_NAME}) shell... ${RESET}\n"
@docker exec -it ${DOCKER_CONTAINER_NAME} sh
.PHONY: push_to_registry
push_to_registry:
@echo -e "\n${YELLOW} Pushing new image to registry... ${RESET}\n"
@[ -n "${REGISTRY}" ] && \
docker push ${REGISTRY}${DOCKER_IMAGE_TAG}
USER_TOKEN?=xxx
TARGET_INSTANCE_URL?=http://localhost:${DOCKER_EXTERNAL_PORT}
URL_PATH?=/
METHOD?=GET
FLAGS?=-sL
.PHONY: raw
raw:
#@echo -e "\n${YELLOW} Executing a raw cURL request based on .env variables... ${RESET}\n"
@/usr/bin/curl ${FLAGS} -X ${METHOD} -H "X-Auth-Token: ${USER_TOKEN}" ${TARGET_INSTANCE_URL}${URL_PATH}
.PHONY: fetch_facts
fetch_facts:
@echo -e "\n${YELLOW} Executing a raw cURL request based on .env variables... ${RESET}\n"
@scripts/fetch_facts.sh
.PHONY: compose_host_vars
compose_host_vars:
@echo -e "\n${YELLOW} Fetching hosts configuration, exporting as YAML host_vars... ${RESET}\n"
@scripts/compose_host_vars.sh
.PHONY: go2ts
go2ts:
@echo -e "\n${YELLOW} Regenerating swapi-types: Go structs models into TS code... ${RESET}\n"
@scripts/go2ts.sh