-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
492 lines (403 loc) Β· 16.5 KB
/
Makefile
File metadata and controls
492 lines (403 loc) Β· 16.5 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
.PHONY: help migrate-up migrate-down migrate-status migrate-create test build run clean docker-build docker-up docker-down deploy-staging deploy-production rollback-staging rollback-production health-check-staging health-check-prod install-hooks reset-staging seed-staging validate-staging admin-install admin-dev admin-build admin-clean
# Tool paths
SWAG := go run github.com/swaggo/swag/cmd/swag
LINT := go run github.com/golangci/golangci-lint/cmd/golangci-lint
MOCKERY := go run github.com/vektra/mockery/v2
SQLC := go run github.com/sqlc-dev/sqlc/cmd/sqlc
# Load environment variables from .env if it exists
ifneq (,$(wildcard ./.env))
include .env
export
endif
# Default target
help:
@echo "BrandishBot_Go - Makefile Commands"
@echo ""
@echo "Migration Commands:"
@echo " make migrate-up - Run all pending migrations"
@echo " make migrate-down - Rollback the last migration"
@echo " make migrate-status - Show migration status"
@echo " make migrate-create NAME= - Create a new migration file"
@echo ""
@echo "Development Commands:"
@echo " make test - Run all tests with coverage"
@echo " make test-coverage - Generate HTML coverage report"
@echo " make test-coverage-check - Verify 80%+ coverage threshold"
@echo " make lint - Run code linters"
@echo " make lint-fix - Run linters with auto-fix"
@echo " make build - Build all binaries to bin/"
@echo " make clean - Remove build artifacts (bin/)"
@echo " make run - Run the application from bin/app"
@echo " make swagger - Generate Swagger docs"
@echo " make generate - Generate sqlc code"
@echo " make install-hooks - Install git hooks (pre-commit formatting)"
@echo " make setup - Setup development environment (deps, docker, db, migrations)"
@echo ""
@echo "Benchmark Commands:"
@echo " make bench - Run all benchmarks"
@echo " make bench-hot - Run hot path benchmarks only"
@echo " make bench-save - Run benchmarks and save timestamped results"
@echo " make bench-baseline - Set current results as baseline"
@echo " make bench-compare - Compare current benchmarks to baseline"
@echo " make bench-profile - Profile hot paths (CPU + memory)"
@echo ""
@echo "Docker Commands:"
@echo " make docker-up - Start services with Docker Compose"
@echo " make docker-down - Stop services"
@echo " make docker-build - Rebuild Docker images (no cache, slower but clean)"
@echo " make docker-build-fast - Build Docker images (with cache, faster for dev)"
@echo " make docker-version - Show version of local Docker image"
@echo ""
@echo "Monitoring Commands:"
@echo " make monitoring-up - Start Prometheus + Grafana"
@echo " make monitoring-down - Stop monitoring stack"
@echo " make monitoring-restart - Restart monitoring services"
@echo " make monitoring-logs - View monitoring logs"
@echo " make monitoring-status - Check monitoring health"
@echo " make prometheus-reload - Hot reload Prometheus config"
@echo ""
@echo "Test Database Commands:"
@echo " make test-integration - Run integration tests (uses testcontainers)"
@echo " make test-staging - Run staging integration tests"
@echo " make db-test-up - Start test database on port 5433"
@echo " make db-test-down - Stop test database"
@echo " make migrate-up-test - Run migrations on test database"
@echo " make db-seed-test - Load test seed data"
@echo " make db-export - Export production DB to backup.sql"
@echo " make db-import - Import backup.sql to test DB"
@echo " make db-clean-test - Clean test database"
@echo ""
@echo "Deployment Commands:"
@echo " make deploy-staging - Deploy to staging environment"
@echo " make deploy-production - Deploy to production environment (requires confirmation)"
@echo " make rollback-staging - Rollback staging to previous version"
@echo " make rollback-production - Rollback production to previous version"
@echo " make health-check-staging - Check staging environment health"
@echo " make health-check-prod - Check production environment health"
@echo " make reset-staging - Full staging reset (down + volume rm + up)"
@echo " make seed-staging - Seed staging with test data"
@echo " make validate-staging - Run validation tests against staging"
@echo ""
@echo "Audit & Security:"
@echo " make test-migrations - Test migration up/down/idempotency"
@echo " make test-security - Run security integration tests"
@echo " make check-deps - Check for required dependencies"
@echo " make check-db - Ensure Docker database is running"
# Database connection string from environment
DB_URL ?= postgres://$(DB_USER):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=disable
# Base reference for smart testing (defaults to origin/develop)
BASE_REF ?= origin/develop
# Migration commands
migrate-up:
@echo "Running migrations..."
@go run ./cmd/devtool migrate up
migrate-down:
@echo "Rolling back migration..."
@go run ./cmd/devtool migrate down
migrate-status:
@echo "Migration status:"
@go run ./cmd/devtool migrate status
migrate-create:
@if [ -z "$(NAME)" ]; then \
echo "Error: NAME is required. Usage: make migrate-create NAME=your_migration_name"; \
exit 1; \
fi
@echo "Creating migration: $(NAME)"
@go run ./cmd/devtool migrate create $(NAME)
# Development commands
test:
@go run ./cmd/devtool test
test-smart:
@echo "Running smart tests (changed packages vs $(BASE_REF))..."
@go run ./cmd/devtool check-coverage -smart -base $(BASE_REF) -exclude ./cmd/devtool -run -threshold 0
unit:
@echo "Running unit tests (fast)..."
@go test -short ./...
watch:
@echo "Watching for changes to run tests (smart mode)..."
@mkdir -p logs
@go run ./cmd/devtool check-coverage -watch -smart -base $(BASE_REF) -exclude ./cmd/devtool -run -file logs/coverage.out -threshold 80
test-coverage:
@go run ./cmd/devtool check-coverage -html -file logs/coverage.out -threshold 0
test-coverage-check:
@go run ./cmd/devtool check-coverage -file logs/coverage.out -threshold 80
lint:
@echo "Running linters..."
@$(LINT) run ./...
@go run ./cmd/devtool check-comments
lint-fix:
@echo "Running linters with auto-fix..."
@$(LINT) run --fix ./...
@npm run format
format:
@echo "Formatting with Prettier..."
@npm run format
@echo "Formatting Go code..."
@go fmt ./...
format-check:
@echo "Checking formatting with Prettier..."
@npm run format:check
install-hooks:
@echo "Installing git hooks..."
@go run ./cmd/devtool install-hooks
# Benchmark commands
.PHONY: bench bench-hot bench-save bench-baseline bench-compare bench-profile
bench:
@go run ./cmd/devtool bench run
bench-hot:
@go run ./cmd/devtool bench hot
bench-save:
@go run ./cmd/devtool bench save
bench-baseline:
@go run ./cmd/devtool bench baseline
bench-compare:
@go run ./cmd/devtool bench compare
bench-profile:
@go run ./cmd/devtool bench profile
# Build targets
build:
@go run ./cmd/devtool build
# Discord bot - Run locally
.PHONY: discord-run
discord-run:
@echo "Starting Discord bot..."
@./bin/discord_bot
# Discord bot - View logs (Docker)
.PHONY: discord-logs
discord-logs:
@docker-compose logs -f discord
# Discord - Build Docker image
.PHONY: docker-discord-build
docker-discord-build:
@echo "Building Discord bot Docker image..."
@docker build -f Dockerfile.discord -t brandishbot-discord:dev .
@echo "β Built: brandishbot-discord:dev"
# Discord - Start Discord service only
.PHONY: docker-discord-up
docker-discord-up:
@echo "Starting Discord bot service..."
@docker-compose up -d discord
@echo "β Discord bot started"
# Discord - Restart Discord service
.PHONY: docker-discord-restart
docker-discord-restart:
@echo "Restarting Discord bot..."
@docker-compose restart discord
@echo "β Discord bot restarted"
# Development shortcuts
setup:
@echo "π Starting environment setup..."
@if [ ! -f .env ]; then \
echo "Creating .env from .env.example..."; \
cp .env.example .env; \
fi
@$(MAKE) check-deps
@$(MAKE) docker-up
@$(MAKE) check-db
@$(MAKE) migrate-up
@$(MAKE) generate
@echo "β
Setup complete!"
run:
@echo "Starting BrandishBot from bin/app..."
@./bin/app
clean:
@echo "Cleaning build artifacts..."
@rm -rf bin/
@echo "β Removed bin/ directory"
swagger:
@echo "Generating Swagger documentation..."
@$(SWAG) init -g cmd/app/main.go --output ./docs/swagger
@echo "Swagger docs updated: docs/swagger/"
generate: generate-swagger generate-sqlc generate-progression generate-mocks generate-tidy format
generate-swagger: swagger
generate-sqlc:
@echo "Generating sqlc code..."
@$(SQLC) generate
@echo "β sqlc code generated"
generate-progression:
@echo "Generating progression keys from config..."
@go run ./cmd/gen-progression-keys -config configs/progression_tree.json -output internal/progression/keys.go
@echo "β progression keys generated"
generate-mocks:
@go run ./cmd/devtool mocks
generate-tidy:
@echo "Running go mod tidy..."
@go mod tidy
@echo "β go mod tidy complete"
# Docker commands
docker-up:
@echo "Starting Docker services..."
@docker compose up -d
docker-down:
@echo "Stopping Docker services..."
@docker compose down
docker-build:
@echo "Rebuilding Docker images (no cache)..."
@VERSION=$$(git describe --tags --always --dirty 2>/dev/null || echo "dev"); \
BUILD_TIME=$$(date -u '+%Y-%m-%d_%H:%M'); \
GIT_COMMIT=$$(git rev-parse --short HEAD 2>/dev/null || echo "unknown"); \
echo "Building with VERSION=$$VERSION BUILD_TIME=$$BUILD_TIME GIT_COMMIT=$$GIT_COMMIT"; \
VERSION=$$VERSION BUILD_TIME=$$BUILD_TIME GIT_COMMIT=$$GIT_COMMIT docker compose build --no-cache
@echo "Docker images rebuilt successfully"
docker-build-fast:
@echo "Building Docker images (with cache, faster)..."
@VERSION=$$(git describe --tags --always --dirty 2>/dev/null || echo "dev"); \
BUILD_TIME=$$(date -u '+%Y-%m-%d_%H:%M'); \
GIT_COMMIT=$$(git rev-parse --short HEAD 2>/dev/null || echo "unknown"); \
VERSION=$$VERSION BUILD_TIME=$$BUILD_TIME GIT_COMMIT=$$GIT_COMMIT DOCKER_BUILDKIT=1 docker compose build
@echo "Docker images built successfully"
docker-version:
@echo "Local Docker image version info:"
@echo "================================"
@docker inspect brandishbot:dev --format='Version: {{index .Config.Labels "org.opencontainers.image.version"}}' 2>/dev/null || echo "Image not found. Run 'make docker-build' first."
@docker inspect brandishbot:dev --format='Git Commit: {{index .Config.Labels "org.opencontainers.image.revision"}}' 2>/dev/null
@docker inspect brandishbot:dev --format='Built: {{index .Config.Labels "org.opencontainers.image.created"}}' 2>/dev/null
@echo "================================"
docker-logs:
@docker compose logs -f
push-staging:
@echo "Pushing staging image..."
@go run ./cmd/devtool push staging $$(git describe --tags --always --dirty)
push-production:
@echo "Pushing production image..."
@go run ./cmd/devtool push production $$(git describe --tags --always --dirty)
# Test database commands
test-integration:
@echo "Running integration tests..."
@go test ./internal/database/postgres -v -timeout=60s
test-staging:
@echo "Running staging integration tests..."
@echo "Target: $${API_URL:-http://localhost:8081}"
@API_URL=$${API_URL:-http://localhost:8081} go test -tags=staging -v ./tests/staging
db-test-up:
@echo "Starting test database..."
@docker-compose -f docker-compose.test.yml up -d
@sleep 2
@echo "Test database ready on port 5433"
db-test-down:
@echo "Stopping test database..."
@docker-compose -f docker-compose.test.yml down
migrate-up-test:
@echo "Running migrations on test database..."
@DB_URL="postgres://testuser:testpass@localhost:5433/testdb?sslmode=disable" go run ./cmd/devtool migrate up
migrate-down-test:
@echo "Rolling back test database migration..."
@DB_URL="postgres://testuser:testpass@localhost:5433/testdb?sslmode=disable" go run ./cmd/devtool migrate down
migrate-status-test:
@echo "Test database migration status:"
@DB_URL="postgres://testuser:testpass@localhost:5433/testdb?sslmode=disable" go run ./cmd/devtool migrate status
db-seed-test:
@echo "Seeding test database..."
@DB_URL="postgres://testuser:testpass@localhost:5433/testdb?sslmode=disable" go run ./cmd/devtool seed test
@echo "Test database seeded successfully"
db-export:
@echo "Exporting production database..."
@docker compose exec -T db pg_dump -U $(DB_USER) -d $(DB_NAME) > backup.sql
@echo "Database exported to backup.sql"
db-import:
@echo "Importing data into test database..."
@docker exec -i brandishbot_test_db psql -U testuser -d testdb < backup.sql
@echo "Data imported successfully"
db-clean-test:
@echo "Cleaning test database..."
@docker exec brandishbot_test_db psql -U testuser -d testdb -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
@echo "Test database cleaned"
@echo "Run 'make migrate-up-test' to reinitialize schema"
# Deployment commands
deploy-staging:
@echo "Deploying to staging..."
@go run ./cmd/devtool deploy staging $$(git describe --tags --always)
deploy-production:
@echo "Deploying to production..."
@go run ./cmd/devtool deploy production $$(git describe --tags --always)
rollback-staging:
@echo "Rolling back staging..."
@go run ./cmd/devtool rollback staging
rollback-production:
@echo "Rolling back production..."
@go run ./cmd/devtool rollback production
health-check-staging:
@go run ./cmd/devtool health-check staging
health-check-prod:
@go run ./cmd/devtool health-check production
# Staging reset and validation targets
reset-staging:
@echo "π Resetting staging environment..."
@echo "Stopping staging containers..."
@docker compose -f docker-compose.staging.yml down -v
@echo "Starting fresh staging environment..."
@docker compose -f docker-compose.staging.yml up -d
@echo "Waiting for services to be ready..."
@sleep 15
@$(MAKE) health-check-staging
@echo "β
Staging reset complete"
seed-staging:
@echo "π± Seeding staging database..."
@DB_URL="postgres://$(DB_USER):$(DB_PASSWORD)@localhost:5433/$(DB_NAME)?sslmode=disable" go run ./cmd/devtool seed staging
@echo "β
Staging seeded"
validate-staging:
@echo "π Validating staging environment..."
@API_URL=http://localhost:8081 $(MAKE) test-staging
@echo "β
Staging validation complete"
# Audit & Security targets
test-migrations:
@go run ./cmd/devtool test-migrations
test-security:
@go run ./cmd/devtool test-security
check-deps:
@go run ./cmd/devtool check-deps
check-db:
@go run ./cmd/devtool check-db
# Admin dashboard commands
admin-install:
@echo "Installing admin dashboard dependencies..."
@cd web/admin && npm ci
@echo "β Admin dependencies installed"
admin-dev:
@echo "Starting admin dashboard dev server..."
@cd web/admin && npm run dev
admin-build:
@echo "Building admin dashboard..."
@cd web/admin && npm ci && npm run build
@rm -rf internal/admin/dist
@cp -r web/admin/dist internal/admin/dist
@echo "β Admin dashboard built and copied to internal/admin/dist"
admin-clean:
@echo "Cleaning admin dashboard artifacts..."
@rm -rf web/admin/dist web/admin/node_modules
@echo "β Admin artifacts cleaned"
# Mock generation
.PHONY: mocks clean-mocks
mocks:
@go run ./cmd/devtool mocks
clean-mocks:
@go run ./cmd/devtool mocks -clean
# Monitoring Commands
.PHONY: monitoring-up monitoring-down monitoring-restart monitoring-logs monitoring-status prometheus-reload
monitoring-up:
@echo "Starting monitoring stack (Prometheus + Grafana)..."
@docker-compose up -d prometheus grafana
@echo "β Monitoring stack started"
@echo " - Prometheus: http://localhost:9090"
@echo " - Grafana: http://localhost:3000 (admin/admin)"
monitoring-down:
@echo "Stopping monitoring stack..."
@docker-compose stop prometheus grafana
@echo "β Monitoring stack stopped"
monitoring-restart:
@echo "Restarting monitoring stack..."
@docker-compose restart prometheus grafana
@echo "β Monitoring stack restarted"
monitoring-logs:
@echo "Showing monitoring logs (Ctrl+C to exit)..."
@docker-compose logs -f prometheus grafana
monitoring-status:
@echo "Checking monitoring stack health..."
@echo -n "Prometheus: "
@curl -s http://localhost:9090/-/healthy || echo "NOT HEALTHY"
@echo -n "Grafana: "
@curl -s http://localhost:3000/api/health | grep -q '"database":"ok"' && echo "Healthy" || echo "NOT HEALTHY"
prometheus-reload:
@echo "Reloading Prometheus configuration..."
@curl -X POST http://localhost:9090/-/reload
@echo "β Prometheus configuration reloaded"