-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
712 lines (624 loc) · 28.6 KB
/
Makefile
File metadata and controls
712 lines (624 loc) · 28.6 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# Matcher Makefile
# Provides centralized commands for development, testing, and deployment.
# Lerian Studio - Matcher Project Management.
# Define the root directory of the project
MATCHER_ROOT := $(shell pwd)
# Worktree-safe: disable VCS stamping so that go build / test / vet succeed
# inside git worktrees where the .git directory is a file pointing elsewhere.
export GOFLAGS := -buildvcs=false
# Test targets use CLEAN_ENV to unset the full Matcher config env surface so
# tests are not polluted by host environment values.
CONFIG_ENV_KEYS := $(shell sed -n 's/^[[:space:]]*"\([A-Z0-9_]*\)",/\1/p' internal/bootstrap/config_test_helpers_test.go 2>/dev/null)
MATCHER_OVERRIDE_KEYS := $(shell sed -n 's/^[[:space:]]*"\(MATCHER_[A-Z0-9_]*\)",/\1/p' internal/bootstrap/config_override_env_keys_test.go 2>/dev/null)
TEST_ENV_KEYS := $(CONFIG_ENV_KEYS)
CLEAN_ENV := env $(foreach v,$(TEST_ENV_KEYS),-u $(v)) $(foreach v,$(MATCHER_OVERRIDE_KEYS),-u $(v))
# Load environment variables from config/.env if it exists.
# This is optional — all defaults are baked into the binary.
# Create config/.env only when you need to override defaults for local dev.
-include config/.env
ifneq ("$(wildcard config/.env)","")
ENV_VARS := $(shell sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' config/.env)
$(foreach v,$(ENV_VARS),$(eval export $(v)))
endif
# Directory configuration
CONFIG_DIR := ./config
# Binary configuration
BINARY_NAME ?= matcher
BIN_DIR ?= bin
GOLANGCI_LINT_VERSION ?= v2.10.1
GO_CI_PACKAGES := ./cmd/... ./internal/... ./migrations/... ./pkg/... ./tests/...
# Migration configuration
MIGRATE_PATH ?= migrations
POSTGRES_HOST ?= localhost
POSTGRES_PORT ?= 5432
POSTGRES_USER ?= matcher
POSTGRES_PASSWORD ?= # dev-only default; MUST be overridden in non-dev environments
POSTGRES_DB ?= matcher
# dev-only default; MUST be overridden in non-dev environments
POSTGRES_SSLMODE ?= disable
DATABASE_URL ?= postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):$(POSTGRES_PORT)/$(POSTGRES_DB)?sslmode=$(POSTGRES_SSLMODE)
# Coverage configuration
COVER_PROFILE_UNIT ?= coverage.unit.out
COVER_PROFILE_INT ?= coverage.int.out
COVER_PROFILE_E2E ?= coverage.e2e.out
COVER_PROFILE_TOTAL ?= coverage.total.out
COVER_PROFILE_TOOLS ?= coverage.tools.out
GOCOVMERGE ?= gocovmerge
GOTESTSUM ?= gotestsum
HAS_GOTESTSUM := $(shell command -v $(GOTESTSUM) >/dev/null 2>&1 && echo yes)
TEST_RUNNER := go test
ifneq ($(HAS_GOTESTSUM),)
TEST_RUNNER := $(GOTESTSUM) --format=pkgname-and-test-fails --hide-summary=skipped --
endif
# Choose docker compose command depending on installed version
DOCKER_CMD := $(shell if docker compose version >/dev/null 2>&1; then echo "docker compose"; else echo "docker-compose"; fi)
export DOCKER_CMD
#-------------------------------------------------------
# Utility Functions
#-------------------------------------------------------
define print_title
@echo ""
@echo "------------------------------------------"
@echo " 📝 $(1) "
@echo "------------------------------------------"
endef
define check_command
@if ! command -v $(1) >/dev/null 2>&1; then \
echo "Error: $(1) is not installed"; \
echo "To install: $(2)"; \
exit 1; \
fi
endef
define merge_coverage
@inputs=""; \
if [ -f "$(1)" ]; then inputs="$$inputs $(1)"; fi; \
if [ -f "$(2)" ]; then inputs="$$inputs $(2)"; fi; \
if [ -f "$(3)" ]; then inputs="$$inputs $(3)"; fi; \
if [ -f "$(4)" ]; then inputs="$$inputs $(4)"; fi; \
if [ -z "$$inputs" ]; then \
echo "No coverage profiles found; skipping merge."; \
exit 0; \
fi; \
if command -v $(GOCOVMERGE) >/dev/null 2>&1; then \
$(GOCOVMERGE) $$inputs > $(5); \
else \
go run github.com/wadey/gocovmerge@latest $$inputs > $(5); \
fi
endef
define show_coverage
@printf "Coverage summary (%s): " $(1); go tool cover -func=$(1) | tail -n 1
endef
#-------------------------------------------------------
# Help Command
#-------------------------------------------------------
.PHONY: help
help:
@echo ""
@echo ""
@echo "Matcher Project Management Commands"
@echo ""
@echo ""
@echo "Core Commands:"
@echo " make help - Display this help message"
@echo " make build - Build matcher binary"
@echo " make clean - Remove build artifacts and temp files"
@echo " make dev - Run with live reload (air)"
@echo " make tidy - Clean go module dependencies"
@echo " make dev-setup - Install all required dev tools (one-time onboarding)"
@echo " make check-tools - Verify all required dev tools are installed"
@echo ""
@echo ""
@echo "Code Quality Commands:"
@echo " make lint - Run golangci-lint"
@echo " make lint-fix - Run golangci-lint with auto-fix"
@echo " make lint-custom - Run custom Matcher linters (entity, observability, tx)"
@echo " make format - Format code in all packages"
@echo " make sec - Run security checks using gosec"
@echo " make check-tests - Verify test coverage for components"
@echo " make check-migrations - Verify migration pairs and sequential numbering"
@echo " make check-license - Verify every Go file has an Elastic License 2.0 header"
@echo " make check-coverage - Check coverage against thresholds"
@echo ""
@echo ""
@echo "CI Commands:"
@echo " make ci - Run local CI verification (lint, tests, security, metadata checks)"
@echo ""
@echo ""
@echo "Test Commands:"
@echo " make test - Run unit tests (alias for 'test-unit')"
@echo " make test-unit - Run unit tests"
@echo " make test-int - Run integration tests"
@echo " make test-e2e - Run e2e tests (requires local stack)"
@echo " make test-e2e-fast - Run e2e tests in quick mode"
@echo " make test-e2e-journeys - Run only e2e journey tests"
@echo " make test-e2e-discovery - Run discovery E2E tests with mock Fetcher"
@echo " make test-e2e-dashboard - Run 5k tx dashboard stresser (data preserved)"
@echo " make test-all - Run all tests (unit + int + e2e) with merged coverage"
@echo " make test-chaos - Run chaos/resilience tests"
@echo " make test-leak - Run goroutine-leak tests (unit+leak build tags)"
@echo ""
@echo ""
@echo "Coverage Commands:"
@echo " make cover - Run tests with coverage report"
@echo ""
@echo ""
@echo "Service Commands:"
@echo " make up - Start all services with Docker Compose"
@echo " make down - Stop all services"
@echo " make start - Start all containers"
@echo " make stop - Stop all containers"
@echo " make restart - Restart all containers"
@echo " make rebuild-up - Rebuild and restart all services"
@echo " make clean-docker - Clean all Docker resources"
@echo " make logs - Show logs for all services"
@echo ""
@echo ""
@echo "Docker Commands:"
@echo " make docker-build - Build Docker image"
@echo ""
@echo ""
@echo "Code Generation Commands:"
@echo " make generate - Run code generation (mocks, etc.)"
@echo " make generate-casdoor - Generate Casdoor RBAC seed data"
@echo " make generate-docs - Generate Swagger documentation"
@echo ""
@echo ""
@echo "Migration Commands:"
@echo " make migrate-up - Apply database migrations"
@echo " make migrate-down - Roll back last migration"
@echo " make migrate-to VERSION=<n> - Migrate to a specific version"
@echo " make migrate-create NAME=<name> - Create new migration files"
@echo " make migrate-version - Show current migration version"
@echo " make migrate-force VERSION=<n> - Force version (rescue stuck state; DOES NOT run SQL)"
@echo ""
@echo ""
#-------------------------------------------------------
# Core Commands
#-------------------------------------------------------
.PHONY: all build clean dev tidy
all: build
build:
$(call print_title,Building matcher binary)
@mkdir -p $(BIN_DIR)
@go build -o $(BIN_DIR)/$(BINARY_NAME) ./cmd/matcher
@echo "[ok] Binary built successfully at $(BIN_DIR)/$(BINARY_NAME)"
clean:
$(call print_title,Cleaning build artifacts)
@rm -rf $(BIN_DIR) tmp
@echo "[ok] Build artifacts cleaned successfully"
dev:
$(call print_title,Starting development server with live reload)
$(call check_command,air,"go install github.com/air-verse/air@latest")
@air
tidy:
$(call print_title,Tidying go modules)
@go mod tidy
@cd tools && go mod tidy
@echo "[ok] Go modules tidied successfully"
#-------------------------------------------------------
# Dev Tool Setup
#-------------------------------------------------------
.PHONY: dev-setup check-tools
# dev-setup installs every CLI tool the Makefile expects to find on PATH.
# golangci-lint is pinned to $(GOLANGCI_LINT_VERSION). Other tools use @latest
# to match the pattern already used by `make sec`, `make vulncheck`, etc.
dev-setup:
$(call print_title,Installing dev tools)
@echo "Installing golangci-lint $(GOLANGCI_LINT_VERSION)..."
@go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
@echo "Installing gosec..."
@go install github.com/securego/gosec/v2/cmd/gosec@latest
@echo "Installing govulncheck..."
@go install golang.org/x/vuln/cmd/govulncheck@latest
@echo "Installing swag..."
@go install github.com/swaggo/swag/cmd/swag@latest
@echo "Installing golang-migrate..."
@go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
@echo "Installing mockgen..."
@go install go.uber.org/mock/mockgen@latest
@echo "Installing goimports..."
@go install golang.org/x/tools/cmd/goimports@latest
@echo "Installing gci..."
@go install github.com/daixiang0/gci@latest
@echo "Installing air..."
@go install github.com/air-verse/air@latest
@echo "Installing gocovmerge..."
@go install github.com/wadey/gocovmerge@latest
@echo "[ok] dev-setup complete"
@echo ""
@echo "Run 'make check-tools' to verify every tool is on PATH."
# check-tools verifies every tool dev-setup installs is reachable on PATH.
# Fails fast with a single clear message naming the missing tool.
check-tools:
$(call print_title,Checking dev tools)
@missing=0; \
for tool in golangci-lint gosec govulncheck swag migrate mockgen goimports gci air gocovmerge; do \
if command -v $$tool >/dev/null 2>&1; then \
echo " [ok] $$tool"; \
else \
echo " [missing] $$tool"; \
missing=1; \
fi; \
done; \
if [ "$$missing" -ne 0 ]; then \
echo ""; \
echo "One or more dev tools are missing. Run: make dev-setup"; \
exit 1; \
fi
@echo "[ok] all dev tools present"
#-------------------------------------------------------
# Code Quality Commands
#-------------------------------------------------------
.PHONY: lint lint-fix lint-custom format sec vet vulncheck check-tests check-test-tags check-migrations check-generated-artifacts check-license check-coverage
vet:
$(call print_title,Running go vet)
@go vet $(GO_CI_PACKAGES)
@echo "[ok] go vet completed successfully"
lint:
$(call print_title,Running linters)
@if ! command -v golangci-lint >/dev/null 2>&1; then \
echo "golangci-lint not found, installing..."; \
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION); \
fi
@golangci-lint run $(GO_CI_PACKAGES)
@echo "[ok] Linting completed successfully"
lint-fix:
$(call print_title,Running linters with auto-fix on all packages)
$(call check_command,golangci-lint,"go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)")
@golangci-lint run --fix $(GO_CI_PACKAGES)
@echo "[ok] Lint auto-fix completed"
lint-custom:
$(call print_title,Running custom Matcher linters)
@mkdir -p $(BIN_DIR)
@cd tools && go build -o ../$(BIN_DIR)/matcherlint ./linters/matcherlint/...
@echo "Running entity constructor pattern checks..."
@go vet -vettool=$(BIN_DIR)/matcherlint ./internal/.../domain/entities/... 2>&1 || echo " ⚠️ Entity constructor violations found (see above)"
@echo ""
@echo "Running observability pattern checks..."
@go vet -vettool=$(BIN_DIR)/matcherlint ./internal/.../services/... 2>&1 || echo " ⚠️ Observability violations found (see above)"
@echo ""
@echo "Running repository transaction pattern checks..."
@go vet -vettool=$(BIN_DIR)/matcherlint ./internal/.../adapters/postgres/... 2>&1 || echo " ⚠️ Repository WithTx violations found (see above)"
@echo ""
@echo "Running determinism pattern checks (advisory)..."
@MATCHER_DETERMINISM_LINTER=1 go vet -tags=unit -vettool=$(BIN_DIR)/matcherlint ./internal/... 2>&1 || echo " ⚠️ Determinism violations found (see above) — advisory, not blocking"
@echo ""
@echo "[ok] Custom lint check completed (violations are warnings until cleanup)"
lint-custom-strict:
$(call print_title,Running custom Matcher linters - STRICT MODE)
@mkdir -p $(BIN_DIR)
@cd tools && go build -o ../$(BIN_DIR)/matcherlint ./linters/matcherlint/...
@MATCHER_GOLEAK_LINTER=1 go vet -tags="unit leak" -vettool=$(BIN_DIR)/matcherlint ./internal/.../domain/entities/...
@MATCHER_GOLEAK_LINTER=1 go vet -tags="unit leak" -vettool=$(BIN_DIR)/matcherlint ./internal/.../services/...
@MATCHER_GOLEAK_LINTER=1 go vet -tags="unit leak" -vettool=$(BIN_DIR)/matcherlint ./internal/.../adapters/postgres/...
@echo "Running goroutine-leak coverage checks..."
@MATCHER_GOLEAK_LINTER=1 go vet -tags="unit leak" -vettool=$(BIN_DIR)/matcherlint ./internal/...
@echo "[ok] Custom linting passed (strict mode)"
format:
$(call print_title,Formatting code)
@go fmt ./...
@echo "[ok] Formatting completed successfully"
sec:
$(call print_title,Running security checks using gosec)
@if ! command -v gosec >/dev/null 2>&1; then \
echo "Installing gosec..."; \
go install github.com/securego/gosec/v2/cmd/gosec@latest; \
fi
@gosec ./cmd/... ./internal/... ./pkg/... ./tests/...
@echo "[ok] Security checks completed"
vulncheck:
$(call print_title,Running vulnerability check)
@if ! command -v govulncheck >/dev/null 2>&1; then \
echo "Installing govulncheck..."; \
go install golang.org/x/vuln/cmd/govulncheck@latest; \
fi
@govulncheck $(GO_CI_PACKAGES)
@echo "[ok] Vulnerability check completed"
check-tests:
$(call print_title,Checking test coverage for components)
@./scripts/check-tests.sh
@echo "[ok] Test coverage check completed"
check-test-tags:
$(call print_title,Checking test build tags)
@./scripts/check-test-tags.sh
check-migrations:
$(call print_title,Checking migration file integrity)
@./scripts/check-migrations.sh
check-generated-artifacts:
$(call print_title,Checking generated artifacts)
@set -e; \
tmp_dir="$$(mktemp -d)"; \
trap 'rm -rf "$$tmp_dir"' EXIT; \
cp -R docs/swagger "$$tmp_dir/swagger-before"; \
$(MAKE) generate-docs >/dev/null; \
if ! diff -ru "$$tmp_dir/swagger-before" docs/swagger >/dev/null; then \
echo "❌ Swagger artifacts are out of date. Run: make generate-docs"; \
diff -ru "$$tmp_dir/swagger-before" docs/swagger || true; \
exit 1; \
fi; \
echo "[ok] Generated artifacts are up to date"
check-license:
$(call print_title,Checking Elastic License 2.0 headers)
@./scripts/add-license-headers.sh --check
check-coverage: test
$(call print_title,Checking coverage thresholds)
@if command -v go-test-coverage >/dev/null 2>&1; then \
go-test-coverage --config .github/.testcoverage.yml --profile $(COVER_PROFILE_TOTAL); \
else \
go run github.com/vladopajic/go-test-coverage/v2@latest --config .github/.testcoverage.yml --profile $(COVER_PROFILE_TOTAL); \
fi
@echo "[ok] Coverage thresholds verified"
#-------------------------------------------------------
# Test Commands
#-------------------------------------------------------
.PHONY: test test-unit coverage-unit test-int test-e2e test-e2e-discovery test-e2e-dashboard test-chaos test-leak test-all cover
test: test-unit
## CI-facing target: runs unit tests and produces coverage.txt for shared workflow artifact collection.
coverage-unit: test-unit
@cp $(COVER_PROFILE_UNIT) coverage.txt
test-unit:
$(call print_title,Running unit tests)
@$(CLEAN_ENV) $(TEST_RUNNER) -tags=unit -coverprofile=$(COVER_PROFILE_UNIT) -race -cover $(GO_CI_PACKAGES)
@cd tools && $(CLEAN_ENV) $(TEST_RUNNER) -tags=unit -coverprofile=../$(COVER_PROFILE_TOOLS) -race ./...
$(call show_coverage,$(COVER_PROFILE_UNIT))
@printf "Coverage summary (%s): " $(COVER_PROFILE_TOOLS); cd tools && go tool cover -func=../$(COVER_PROFILE_TOOLS) | tail -n 1
@echo "[ok] Unit tests passed"
test-int:
$(call print_title,Running integration tests)
@TESTCONTAINERS_RYUK_DISABLED=$${TESTCONTAINERS_RYUK_DISABLED:-true} $(TEST_RUNNER) -tags=integration -coverprofile=$(COVER_PROFILE_INT) -race -cover ./tests/integration/...
$(call show_coverage,$(COVER_PROFILE_INT))
@echo "[ok] Integration tests passed"
test-e2e:
$(call print_title,Running e2e tests against local stack)
@echo "Requires: $(DOCKER_CMD) up -d (ensure full stack is running)"
@TESTCONTAINERS_RYUK_DISABLED=$${TESTCONTAINERS_RYUK_DISABLED:-true} $(TEST_RUNNER) -tags=e2e -timeout=10m -v -p 1 -coverprofile=$(COVER_PROFILE_E2E) -race -cover ./tests/e2e/...
$(call show_coverage,$(COVER_PROFILE_E2E))
@echo "[ok] E2E tests passed"
test-e2e-fast:
$(call print_title,Running e2e tests - quick mode)
@TESTCONTAINERS_RYUK_DISABLED=$${TESTCONTAINERS_RYUK_DISABLED:-true} $(TEST_RUNNER) -tags=e2e -timeout=5m -short -p 1 -coverprofile=$(COVER_PROFILE_E2E) -race -cover ./tests/e2e/...
$(call show_coverage,$(COVER_PROFILE_E2E))
@echo "[ok] E2E tests passed (quick mode)"
test-e2e-journeys:
$(call print_title,Running e2e journey tests only)
@TESTCONTAINERS_RYUK_DISABLED=$${TESTCONTAINERS_RYUK_DISABLED:-true} $(TEST_RUNNER) -tags=e2e -timeout=10m -v -coverprofile=$(COVER_PROFILE_E2E) -race -cover ./tests/e2e/journeys/...
$(call show_coverage,$(COVER_PROFILE_E2E))
@echo "[ok] Journey tests passed"
test-e2e-discovery:
$(call print_title,Running Discovery E2E tests with mock Fetcher)
@TESTCONTAINERS_RYUK_DISABLED=$${TESTCONTAINERS_RYUK_DISABLED:-true} E2E_REQUIRE_FETCHER_MOCK=1 $(TEST_RUNNER) -tags=e2e -timeout=5m -v -p 1 -coverprofile=$(COVER_PROFILE_E2E) -race -cover -run TestDiscovery ./tests/e2e/journeys/...
@echo "[ok] Discovery E2E tests passed"
test-e2e-dashboard:
$(call print_title,Running dashboard stresser - data will be PRESERVED)
@echo "This test generates ~5k transactions and keeps data for dashboard viewing."
@echo "To clean up later, delete the context 'dashboard-stress-5k' via API."
@echo ""
TESTCONTAINERS_RYUK_DISABLED=$${TESTCONTAINERS_RYUK_DISABLED:-true} E2E_KEEP_DATA=1 $(TEST_RUNNER) -tags=e2e -timeout=30m -v -count=1 -race -run TestDashboardStresser_HighVolume ./tests/e2e/journeys/...
@echo ""
@echo "[ok] Dashboard stresser completed - data preserved in database"
test-chaos:
$(call print_title,Running chaos/resilience tests)
@echo "Requires Docker for testcontainers (PostgreSQL + Redis + RabbitMQ + Toxiproxy)"
@TESTCONTAINERS_RYUK_DISABLED=$${TESTCONTAINERS_RYUK_DISABLED:-true} $(TEST_RUNNER) -tags=chaos -timeout=15m -v -count=1 -p 1 -race ./tests/chaos/...
@echo "[ok] Chaos tests passed"
test-leak:
$(call print_title,Running goroutine-leak tests)
@$(CLEAN_ENV) $(TEST_RUNNER) -tags="unit leak" -timeout=120s -count=1 $(GO_CI_PACKAGES)
@echo "[ok] Goroutine-leak tests passed"
test-all:
$(call print_title,Running all tests - unit + integration + e2e)
@$(MAKE) test-unit
@$(MAKE) test-int
@$(MAKE) test-e2e
$(call merge_coverage,$(COVER_PROFILE_UNIT),$(COVER_PROFILE_INT),$(COVER_PROFILE_E2E),,$(COVER_PROFILE_TOTAL))
$(call show_coverage,$(COVER_PROFILE_TOTAL))
@echo "[ok] All tests passed"
cover:
$(call print_title,Generating test coverage report)
@$(MAKE) test
@go tool cover -html=$(COVER_PROFILE_TOTAL) -o coverage.html
@echo ""
@echo "Coverage Summary:"
@echo "----------------------------------------"
@go tool cover -func=$(COVER_PROFILE_TOTAL) | grep total | awk '{print "Total coverage: " $$3}'
@echo "----------------------------------------"
@echo "Open coverage.html in your browser to view detailed coverage report"
@echo "[ok] Coverage report generated successfully"
#-------------------------------------------------------
# CI Commands
#-------------------------------------------------------
.PHONY: ci
ci:
$(call print_title,Running local CI verification pipeline)
@$(MAKE) generate
@$(MAKE) generate-docs
@$(MAKE) generate-casdoor
@$(MAKE) lint
@$(MAKE) test
@$(MAKE) test-leak
@$(MAKE) test-int
@$(MAKE) check-tests
@$(MAKE) check-test-tags
@$(MAKE) check-migrations
@$(MAKE) check-generated-artifacts
@$(MAKE) check-license
@$(MAKE) sec
@$(MAKE) vet
@$(MAKE) vulncheck
@echo ""
@echo "=========================================="
@echo " [ok] Local CI verification passed"
@echo "=========================================="
#-------------------------------------------------------
# Service Commands
#-------------------------------------------------------
.PHONY: up down start stop restart rebuild-up clean-docker logs
up:
$(call print_title,Starting all services with Docker Compose)
@$(DOCKER_CMD) up -d
@echo "[ok] All services started successfully"
down:
$(call print_title,Stopping all services)
@$(DOCKER_CMD) down
@echo "[ok] All services stopped successfully"
start:
$(call print_title,Starting all containers)
@$(DOCKER_CMD) start
@echo "[ok] All containers started successfully"
stop:
$(call print_title,Stopping all containers)
@$(DOCKER_CMD) stop
@echo "[ok] All containers stopped successfully"
restart:
$(call print_title,Restarting all containers)
@$(MAKE) down && $(MAKE) up
@echo "[ok] All containers restarted successfully"
rebuild-up:
$(call print_title,Rebuilding and restarting all services)
@$(DOCKER_CMD) up -d --build
@echo "[ok] All services rebuilt and restarted successfully"
clean-docker:
$(call print_title,Cleaning all Docker resources)
@$(DOCKER_CMD) down -v --remove-orphans 2>/dev/null || true
@docker system prune -f
@docker volume prune -f
@echo "[ok] All Docker resources cleaned successfully"
logs:
$(call print_title,Showing logs for all services)
@$(DOCKER_CMD) logs --tail=50 -f
#-------------------------------------------------------
# Docker Commands
#-------------------------------------------------------
.PHONY: docker-build
docker-build:
$(call print_title,Building Docker image)
@docker build -t $(BINARY_NAME):local .
@echo "[ok] Docker image built successfully"
#-------------------------------------------------------
# Code Generation Commands
#-------------------------------------------------------
.PHONY: generate generate-casdoor generate-docs
generate:
$(call print_title,Running code generation)
@go generate ./...
@echo "[ok] Code generation completed"
generate-casdoor:
$(call print_title,Generating Casdoor seed data)
@go run ./cmd/generate-casdoor --output config/casdoor/init_data.json
@echo "[ok] Casdoor seed data generated at config/casdoor/init_data.json"
generate-docs:
$(call print_title,Generating Swagger documentation)
$(call check_command,swag,"go install github.com/swaggo/swag/cmd/swag@latest")
@swag init -g cmd/matcher/main.go -o docs/swagger --parseInternal --parseDependency --exclude worktrees
$(call check_command,python3,"Install Python 3 from https://python.org or via your package manager")
@python3 scripts/inject_swagger_tags.py cmd/matcher/main.go docs/swagger/swagger.json docs/swagger/swagger.yaml
@if [ -f "LICENSE.md" ]; then \
cp LICENSE.md docs/swagger/LICENSE.md; \
echo "License copied to docs/swagger/LICENSE.md"; \
else \
echo "Warning: LICENSE.md not found, skipping license copy"; \
fi
@echo "Swagger documentation generated in docs/swagger/"
@echo "Checking for operationIds..."
@grep -c '"operationId"' docs/swagger/swagger.json || true
@if command -v swagger-cli >/dev/null 2>&1; then \
echo "Validating swagger spec..."; \
swagger-cli validate --no-schema docs/swagger/swagger.yaml || \
{ echo "Note: swagger-cli v4+ validates as OpenAPI 3.x; swag generates Swagger 2.0. Spec structure is valid."; exit 1; }; \
else \
echo "Note: swagger-cli not installed, skipping validation"; \
fi
@echo "[ok] Swagger documentation generated successfully"
#-------------------------------------------------------
# Migration Commands
#-------------------------------------------------------
.PHONY: check-db-safety migrate-up migrate-down migrate-to migrate-create migrate-version migrate-force
check-db-safety:
@set -eu; \
host="$(POSTGRES_HOST)"; \
pass="$(POSTGRES_PASSWORD)"; \
sslmode="$(POSTGRES_SSLMODE)"; \
url="$(DATABASE_URL)"; \
is_local=0; \
case "$$host" in localhost|127.0.0.1|::1|0.0.0.0) is_local=1 ;; esac; \
if [ "$$is_local" -eq 0 ]; then \
if [ -z "$$pass" ]; then \
echo "ERROR: refusing to run with empty POSTGRES_PASSWORD against non-local POSTGRES_HOST=$$host." >&2; \
echo "POSTGRES_HOST=$$host POSTGRES_SSLMODE=$$sslmode DATABASE_URL=$$url" >&2; \
exit 1; \
fi; \
if [ "$$sslmode" = "disable" ]; then \
echo "ERROR: refusing to run with POSTGRES_SSLMODE=disable against non-local POSTGRES_HOST=$$host." >&2; \
echo "POSTGRES_HOST=$$host POSTGRES_SSLMODE=$$sslmode DATABASE_URL=$$url" >&2; \
exit 1; \
fi; \
else \
if [ -z "$$pass" ] || [ "$$sslmode" = "disable" ]; then \
echo "WARNING: using development-only DB defaults (empty POSTGRES_PASSWORD and/or POSTGRES_SSLMODE=disable) for localhost." >&2; \
echo "POSTGRES_HOST=$$host POSTGRES_SSLMODE=$$sslmode DATABASE_URL=$$url" >&2; \
fi; \
fi
migrate-up: check-db-safety
$(call print_title,Applying database migrations)
$(call check_command,migrate,"https://github.com/golang-migrate/migrate")
$(call check_command,go,"https://go.dev/dl/")
@DATABASE_URL="$(DATABASE_URL)" go run ./cmd/migration-preflight --action up
@migrate -path $(MIGRATE_PATH) -database "$(DATABASE_URL)" up
@echo "[ok] Migrations applied successfully"
migrate-down: check-db-safety
$(call print_title,Rolling back last migration)
$(call check_command,migrate,"https://github.com/golang-migrate/migrate")
$(call check_command,go,"https://go.dev/dl/")
@DATABASE_URL="$(DATABASE_URL)" go run ./cmd/migration-preflight --action down
@migrate -path $(MIGRATE_PATH) -database "$(DATABASE_URL)" down 1
@echo "[ok] Migration rolled back successfully"
migrate-to: check-db-safety
$(call print_title,Migrating to specific version)
@if [ -z "$(VERSION)" ]; then \
echo "Error: VERSION not specified."; \
echo "Usage: make migrate-to VERSION=<version_number>"; \
exit 1; \
fi
$(call check_command,migrate,"https://github.com/golang-migrate/migrate")
$(call check_command,go,"https://go.dev/dl/")
@DATABASE_URL="$(DATABASE_URL)" go run ./cmd/migration-preflight --action goto --target $(VERSION)
@migrate -path $(MIGRATE_PATH) -database "$(DATABASE_URL)" goto $(VERSION)
@echo "[ok] Migrated to version $(VERSION) successfully"
migrate-create:
$(call print_title,Creating new migration)
@if [ -z "$(NAME)" ]; then \
echo "Error: NAME not specified."; \
echo "Usage: make migrate-create NAME=<migration_name>"; \
exit 1; \
fi
$(call check_command,migrate,"https://github.com/golang-migrate/migrate")
@migrate create -ext sql -dir $(MIGRATE_PATH) -seq $(NAME)
@echo "[ok] Migration files created"
@echo ""
@echo "Next steps:"
@echo " 1. Edit the .up.sql file with your changes"
@echo " 2. Edit the .down.sql file with the rollback"
@echo " 3. Run 'make migrate-up' to apply"
# migrate-version prints the current migration version (read-only; no
# check-db-safety dependency so it works against any DATABASE_URL).
migrate-version:
$(call print_title,Current migration version)
$(call check_command,migrate,"https://github.com/golang-migrate/migrate")
@migrate -path $(MIGRATE_PATH) -database "$(DATABASE_URL)" version
# migrate-force hard-sets the migration version without running SQL. Use only
# to rescue a stuck/dirty migration state — this does NOT apply or roll back
# anything. Requires VERSION=<n>.
migrate-force: check-db-safety
$(call print_title,Forcing migration version)
@if [ -z "$(VERSION)" ]; then \
echo "Error: VERSION not specified."; \
echo "Usage: make migrate-force VERSION=<version_number>"; \
echo "WARNING: this only overwrites schema_migrations; no SQL runs."; \
exit 1; \
fi
$(call check_command,migrate,"https://github.com/golang-migrate/migrate")
@echo "WARNING: forcing version $(VERSION) — schema_migrations will be"
@echo " overwritten without running any up/down SQL. Use only to"
@echo " rescue a stuck state (dirty flag) after manual repair."
@migrate -path $(MIGRATE_PATH) -database "$(DATABASE_URL)" force $(VERSION)
@echo "[ok] Migration version forced to $(VERSION)"