-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
229 lines (186 loc) · 7.23 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
# Check and create .env file if not present using a shell command
.env:
@echo ""
@echo "Read .env file not found. Copying from .example.env..."
@echo ""
@cp .example.env .env
include .env
# Set GOPATH to your desired Go path, or it defaults to the system GOPATH if not set
GOPATH ?= $(shell go env GOPATH)
GOPATH_BIN := $(GOPATH)/bin
# Prepend GOPATH/bin to PATH if not already present
ifneq ($(filter $(GOPATH_BIN), $(PATH)),)
export PATH := $(PATH)
else
export PATH := $(GOPATH_BIN):$(PATH)
endif
# List all proto files in the proto directory
PROTO_FILES := $(wildcard $(APP_PROTO_DIR)/*.proto)
help:
@echo "Application Available Commands"
@echo ""
@echo "Usage: make [commands]"
@echo ""
@echo "Commands:"
@echo " setup Make setup your project workspace"
@echo " migrate-help Make migarte help command"
@echo " pkl-gen Make pkl help you to create configuration"
@echo " go-help Make go help command"
@echo " print-path Make print current path variable"
@echo ""
@echo "Examples:"
@echo " make setup"
@echo " make pkl-gen"
@echo " make go-help"
@echo " make migrate-help"
print-path:
@echo "Current PATH: $(PATH)"
# Command Setup
setup:
@echo "- Install Protoc Generate GO Tool"
@go install google.golang.org/protobuf/cmd/protoc-gen-go@latest || { echo 'Error: protoc-gen-go installation failed.'; exit 1; }
@echo "- Install Protoc Generate GO GRPC Tool"
@go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest || { echo 'Error: protoc-gen-go-grpc installation failed.'; exit 1; }
@echo "- Install Goose For Migration & Seeder Tool"
@go install github.com/pressly/goose/v3/cmd/goose@latest || { echo 'Error: goose installation failed.'; exit 1; }
@echo "- Install PKL GO Configuration Generator Tool"
@go install github.com/apple/pkl-go/cmd/pkl-gen-go@v0.8.0 || { echo 'Error: pkl-lang configuration generator for golang installation failed.'; exit 1; }
@echo "- Checking installed tools in GOPATH/bin..."
@echo ""
@ls $(shell go env GOPATH)/bin || { echo 'Error: check installation tools in GOPATH/bin.'; exit 1; }
@echo ""
@echo "Setup Installation Tools Is Complete!"
# Command to build config commands
pkl-gen:
@rm -rf ./gen/pkl
@pkl-gen-go ./pkl/AppConfig.pkl --base-path $(APP_BASE_MODULE)
# Command to run golang commands
go-tidy:
@go mod tidy
go-run:
@if [ -z "$(cfg)" ]; then \
go run ./cmd/$(app); \
else \
go run ./cmd/$(app) -cfg=$(cfg); \
fi
go-build:
@go build -o ./bin/$(app) ./cmd/$(app)
go-gen-proto:
@echo "Generating Go code from .proto files..."
@for proto_file in $(PROTO_FILES); do \
echo "Processing: $$proto_file"; \
DIR_NAME=$$(basename $$proto_file .proto); \
mkdir -p $(APP_PROTO_GEN_DIR)/$$DIR_NAME; \
protoc --go_out=$(APP_PROTO_GEN_DIR)/$$DIR_NAME --go_opt=paths=source_relative \
--go-grpc_out=$(APP_PROTO_GEN_DIR)/$$DIR_NAME --go-grpc_opt=paths=source_relative \
-I $(APP_PROTO_DIR) $$proto_file; \
done
@echo "Go code generation complete!"
go-help:
@echo "Application Golang Commands"
@echo ""
@echo "Usage: make [commands] [OPTIONS]"
@echo ""
@echo "Commands:"
@echo " go-tidy Go module tidy"
@echo " go-run APPLICATION Go run application"
@echo " go-build APPLICATION Go build application"
@echo " go-gen-proto Go generate grpc protobuff"
@echo ""
@echo "Examples:"
@echo " make go-tidy"
@echo " make go-run app=restapi"
@echo " make go-run app=restapi cfg=<your_pkl_config_file>"
@echo " make go-build app=restapi"
@echo " make go-gen-proto"
# Command to run goose with the specified options
GOOSE_MIGRATE_CMD = GOOSE_DRIVER=$(DB_GOOSE_DRIVER) GOOSE_DBSTRING=$(DB_GOOSE_DBSTRING) GOOSE_MIGRATION_DIR=$(DB_GOOSE_MIGRATION_DIR) goose -table $(DB_GOOSE_MIGRATION_TABLE)
# Migration commands
migrate-up:
@$(GOOSE_MIGRATE_CMD) up
migrate-up-by-one:
@$(GOOSE_MIGRATE_CMD) up-by-one
migrate-up-to:
@$(GOOSE_MIGRATE_CMD) up-to $(v)
migrate-down:
@$(GOOSE_MIGRATE_CMD) down
migrate-down-to:
@$(GOOSE_MIGRATE_CMD) down-to $(v)
migrate-create:
@$(GOOSE_MIGRATE_CMD) create $(n) $(t)
migrate-redo:
@$(GOOSE_MIGRATE_CMD) redo
migrate-reset:
@$(GOOSE_MIGRATE_CMD) reset
migrate-status:
@$(GOOSE_MIGRATE_CMD) status
migrate-version:
@$(GOOSE_MIGRATE_CMD) version
migrate-fix:
@$(GOOSE_MIGRATE_CMD) fix
migrate-validate:
@$(GOOSE_MIGRATE_CMD) validate
# Command to run goose with the specified options
GOOSE_SEEDER_CMD = GOOSE_DRIVER=$(DB_GOOSE_DRIVER) GOOSE_DBSTRING=$(DB_GOOSE_DBSTRING) GOOSE_MIGRATION_DIR=$(DB_GOOSE_MIGRATION_SEEDER_DIR) goose -table $(DB_GOOSE_MIGRATION_SEEDER_TABLE)
# Seeders commands
seeder-up:
@$(GOOSE_SEEDER_CMD) up
seeder-up-by-one:
@$(GOOSE_SEEDER_CMD) up-by-one
seeder-up-to:
@$(GOOSE_SEEDER_CMD) up-to $(v)
seeder-down:
@$(GOOSE_SEEDER_CMD) down
seeder-down-to:
@$(GOOSE_SEEDER_CMD) down-to $(v)
seeder-create:
@$(GOOSE_SEEDER_CMD) create $(n) $(t)
seeder-redo:
@$(GOOSE_SEEDER_CMD) redo
seeder-reset:
@$(GOOSE_SEEDER_CMD) reset
seeder-status:
@$(GOOSE_SEEDER_CMD) status
seeder-version:
@$(GOOSE_SEEDER_CMD) version
seeder-fix:
@$(GOOSE_SEEDER_CMD) fix
seeder-validate:
@$(GOOSE_SEEDER_CMD) validate
# Help messages
seeder-help:
@make migrate-help
migrate-help:
@echo "Migrations and Seeders Docs"
@echo ""
@echo "Usage: make [migrate | seeder]-[command] [OPTIONS]"
@echo ""
@echo "Commands:"
@echo " ( migrate | seeder )-up Migrate the DB to the most recent version available"
@echo " ( migrate | seeder )-up-by-one Migrate the DB up by 1"
@echo " ( migrate | seeder )-up-to VERSION Migrate the DB to a specific VERSION"
@echo " ( migrate | seeder )-down Roll back the version by 1"
@echo " ( migrate | seeder )-down-to VERSION Roll back to a specific VERSION"
@echo " ( migrate | seeder )-create NAME [sql|go] Creates new migration file with the current timestamp"
@echo " ( migrate | seeder )-redo Re-run the latest migration"
@echo " ( migrate | seeder )-reset Roll back all migrations"
@echo " ( migrate | seeder )-status Dump the migration status for the current DB"
@echo " ( migrate | seeder )-version Print the current version of the database"
@echo " ( migrate | seeder )-fix Apply sequential ordering to migrations"
@echo " ( migrate | seeder )-validate Check migration files without running them"
@echo ""
@echo "Options by env file:"
@echo " DB_GOOSE_DRIVER Database driver (postgres, mysql, sqlite3, mssql, redshift, tidb, clickhouse, vertica, ydb, turso)"
@echo " DB_GOOSE_DBSTRING Connection string for the database"
@echo " DB_GOOSE_MIGRATION_DIR Directory for migration files (default: current directory)"
@echo ""
@echo "Examples:"
@echo " make ( migrate | seeder )-up"
@echo " make ( migrate | seeder )-up-by-one"
@echo " make ( migrate | seeder )-up-to v=20240922160357"
@echo " make ( migrate | seeder )-down"
@echo " make ( migrate | seeder )-down-to v=20240922160357"
@echo " make ( migrate | seeder )-status"
@echo " make ( migrate | seeder )-version"
@echo " make ( migrate | seeder )-create n=<migration_name> t=<sql|go>"
@echo " make ( migrate | seeder )-validate"