-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
124 lines (85 loc) · 4.59 KB
/
Makefile
File metadata and controls
124 lines (85 loc) · 4.59 KB
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
# 讀取 .env 檔案
include .env
# ===================================================================================
# Default
# ===================================================================================
.DEFAULT_GOAL := help
# ===================================================================================
# Variables
# ===================================================================================
pes_parent_dir:=$(shell pwd)/$(lastword $(MAKEFILE_LIST))
pes_parent_dir:=$(shell dirname $(pes_parent_dir))
DockerImageNameMigrate='migrate/migrate:v4.19.0'
DockerImageNameSwaggerGenerate='ghcr.io/swaggo/swag:v1.16.6'
MigrationFilePath=$(pes_parent_dir)/deployments/migrations
LocalDatabase='mysql://$(MYSQL_USER):$(MYSQL_PASSWORD)@tcp($(MYSQL_HOST):$(MYSQL_PORT))/$(MYSQL_DATABASE)'
TestConvertFileDir=$(pes_parent_dir)/test
TestConvertFilePath=$(pes_parent_dir)/test/test-cover.txt
# ==============================================================================
# Help
# ==============================================================================
.PHONY: help
help: ## Show this help message
@echo "Usage: make <target>"
@echo ""
@echo "Targets:"
@awk 'BEGIN {FS = ":.*?## " } /^[a-zA-Z_-]+:.*?## / {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
# ==============================================================================
# Development
# ==============================================================================
.PHONY: run lint fmt dockerUp dockerDown dockerDownClean dockerLogs
run: ## Run the application
@go run ./cmd/api/main.go
lint: ## Run linter
@golangci-lint run ./...
fmt: ## Format code
@gofumpt -w .
dockerUp: ## 啟動並建置 Docker 容器
docker compose up -d --build
dockerDown: ## 停止並移除 Docker 容器
docker compose down
dockerDownClean: ## 停止並移除 Docker 容器和具名儲存卷
docker compose down -v
dockerLogs: ## 查看 app 服務的日誌
docker compose logs -f app
## ==============================================================================
# Swagger
# ==============================================================================
.PHONY: swaggerGenerateDoc
swaggerGenerateDoc: ## Generate API Swagger documentation
@docker run --rm -v $(pes_parent_dir):/code ${DockerImageNameSwaggerGenerate} init -o ./docs/api --ot json -g cmd/api/main.go -t !z
# ==============================================================================
# Database
# ==============================================================================
.PHONY: databaseMigrateCreate databaseMigrateUp databaseMigrateRollback
databaseMigrateCreate: ## Create a new database migration file. Usage: make databaseMigrateCreate name="migration_name"
ifndef name
$(error name is undefined. Usage: make databaseMigrateCreate name="migration_name")
endif
@mkdir -p $(MigrationFilePath)
@docker run --rm -v $(MigrationFilePath):/migrations --network host $(DockerImageNameMigrate) create -seq -ext sql -dir /migrations $(name)
databaseMigrateUp: ## Migrate database to the latest version
@docker run --rm -v $(MigrationFilePath):/migrations --network host $(DockerImageNameMigrate) -path=/migrations/ -database $(LocalDatabase) up
databaseMigrateRollback: ## Rollback database by one version
@docker run --rm -v $(MigrationFilePath):/migrations --network host $(DockerImageNameMigrate) -path=/migrations/ -database $(LocalDatabase) down 1
# ==============================================================================
# JWT Secret
# ==============================================================================
.PHONY: genJWTSecretToEnv
genJWTSecretToEnv: ## Generate a new JWT secret key and append it to the .env file
@openssl ecparam -genkey -name secp521r1 -noout | tee -a | awk '{printf "%s""\\n",$$0}' | rev | cut -c3- | rev | awk '{printf "\nTOKEN_SECRET=\"%s\"\n",$$0}' >> .env
# ==============================================================================
# Test
# ==============================================================================
.PHONY: buildMock unitTest showCodeCoverage showTestFailure
buildMock: ## Build mock files
@go generate ./...
unitTest: ## Run unit tests and show coverage
@if ! [ -d "$(TestConvertFileDir)" ]; then mkdir $(TestConvertFileDir); fi
@go test ./... -count=1 -coverprofile=$(TestConvertFilePath) && make showCodeCoverage || make showTestFailure
showCodeCoverage:
@echo "\033[31m\033[1m"
@go tool cover -func=$(TestConvertFilePath) | tail -n 1 | awk '{print $$3}' | xargs echo "Test Coverage:"
@echo "\033[0m"
showTestFailure:
@echo "\033[1;33;41m\033[1m🥶🥶🥶🥶 Test Failure 🥶🥶🥶🥶\033[0m"