Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
287 lines (242 loc) · 11.2 KB
/
Copy pathMakefile
File metadata and controls
287 lines (242 loc) · 11.2 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
.PHONY: help build test lint clean docker-build docker-up docker-down migrate migrate-rollback migrate-status migrate-force seed dev install setup version sync-version check-version
# ============================================================================
# VERSION — single source of truth is the root VERSION file (see docs/VERSIONING.md)
# ============================================================================
VERSION := $(shell cat VERSION 2>/dev/null | tr -d '[:space:]')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
LDFLAGS := -X main.Version=$(VERSION) -X main.Commit=$(GIT_COMMIT)
COMPOSE ?= docker compose
help:
@echo "╔════════════════════════════════════════════════════════════════╗"
@echo "║ OpenRisk Development Commands ║"
@echo "╚════════════════════════════════════════════════════════════════╝"
@echo ""
@echo " 🚀 QUICK START"
@echo " make setup - Initial setup (install dependencies)"
@echo " make dev - Start full development environment"
@echo " make docker-up - Start all Docker services"
@echo ""
@echo " 🔨 BUILD & COMPILE"
@echo " make build - Build backend binary"
@echo " make frontend-build - Build frontend for production"
@echo ""
@echo " 🧪 TESTING"
@echo " make test - Run all backend tests with coverage"
@echo " make test-unit - Run unit tests only (fast)"
@echo " make test-integration - Run integration tests (slow)"
@echo " make frontend-test - Run frontend tests"
@echo ""
@echo " 📝 CODE QUALITY"
@echo " make lint - Run all linters"
@echo " make lint-backend - Run backend linter (golangci-lint)"
@echo " make lint-frontend - Run frontend linter (ESLint)"
@echo " make format - Format code (gofmt + prettier)"
@echo ""
@echo " 🐳 DOCKER"
@echo " make docker-up - Start all containers"
@echo " make docker-down - Stop all containers"
@echo " make docker-build - Build Docker image"
@echo " make docker-logs - View live container logs"
@echo " make docker-status - Show container status"
@echo " make docker-clean - Remove containers and volumes"
@echo ""
@echo " 💾 DATABASE"
@echo " make migrate - Run database migrations"
@echo " make migrate-rollback - Rollback last migration"
@echo " make migrate-status - Show current migration version + dirty flag"
@echo " make migrate-force VERSION=<n> - Recover a DIRTY database (set version, clear dirty)"
@echo " make seed - Seed database with sample data"
@echo " make db-shell - Open database shell (psql)"
@echo " make db-test-shell - Open test database shell"
@echo ""
@echo " 🧹 CLEANUP"
@echo " make clean - Clean build artifacts"
@echo " make clean-all - Clean everything (DESTRUCTIVE)"
@echo ""
# ============================================================================
# SETUP & INSTALLATION
# ============================================================================
setup: install frontend-install
@echo "✅ Setup complete! Run 'make dev' to start developing."
install:
@echo "📦 Installing backend dependencies..."
cd backend && go mod download
@echo "✅ Backend dependencies installed"
frontend-install:
@echo "📦 Installing frontend dependencies..."
cd frontend && npm ci
@echo "✅ Frontend dependencies installed"
# ============================================================================
# BUILD & COMPILE
# ============================================================================
build:
@echo "🔨 Building backend binary (v$(VERSION), commit $(GIT_COMMIT))..."
cd backend && CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o openrisk ./cmd/server
@echo "✅ Backend binary built: backend/openrisk"
# ----------------------------------------------------------------------------
# VERSION propagation & verification
# ----------------------------------------------------------------------------
version: ## Print the single-source version
@echo "$(VERSION)"
sync-version: ## Write VERSION into the Helm chart and frontend package.json
@echo "🔁 Propagating VERSION=$(VERSION) to Helm chart + frontend..."
@sed -i.bak -E 's/^version: .*/version: $(VERSION)/' helm/openrisk/Chart.yaml && rm -f helm/openrisk/Chart.yaml.bak
@sed -i.bak -E 's/^appVersion: .*/appVersion: "$(VERSION)"/' helm/openrisk/Chart.yaml && rm -f helm/openrisk/Chart.yaml.bak
@sed -i.bak -E '0,/"version":/ s/("version": *")[^"]*"/\1$(VERSION)"/' frontend/package.json && rm -f frontend/package.json.bak
@echo "✅ Synced. Review the diff, then commit."
check-version: ## Fail if Helm chart / frontend version drift from VERSION (used by release CI)
@chart="$$(grep -E '^version:' helm/openrisk/Chart.yaml | head -1 | awk '{print $$2}')"; \
app="$$(grep -E '^appVersion:' helm/openrisk/Chart.yaml | head -1 | sed -E 's/appVersion: *"?([^"]*)"?/\1/')"; \
fe="$$(grep -E '"version"' frontend/package.json | head -1 | sed -E 's/.*: *"([^"]*)".*/\1/')"; \
fail=0; \
[ "$$chart" = "$(VERSION)" ] || { echo "::error::Helm chart version '$$chart' != VERSION '$(VERSION)'"; fail=1; }; \
[ "$$app" = "$(VERSION)" ] || { echo "::error::Helm appVersion '$$app' != VERSION '$(VERSION)'"; fail=1; }; \
[ "$$fe" = "$(VERSION)" ] || { echo "::error::frontend version '$$fe' != VERSION '$(VERSION)'"; fail=1; }; \
if [ "$$fail" -ne 0 ]; then exit 1; fi; \
echo "✅ Versions aligned on $(VERSION) (chart/appVersion/frontend)"
frontend-build:
@echo "🔨 Building frontend for production..."
cd frontend && npm run build
@echo "✅ Frontend built: frontend/dist/"
# ============================================================================
# TESTING
# ============================================================================
test:
@echo "🧪 Running all backend tests with coverage..."
cd backend && go test -v -coverprofile=coverage.out ./...
@echo ""
@echo "📊 Coverage report:"
cd backend && go tool cover -func=coverage.out | tail -1
test-unit:
@echo "🧪 Running unit tests (fast)..."
cd backend && go test -v -short ./...
test-integration:
@echo "🧪 Running integration tests..."
$(COMPOSE) up -d test_db
@sleep 2
cd backend && DATABASE_URL="postgres://test:test@localhost:5435/openrisk_test" go test -v -tags=integration ./...
$(COMPOSE) stop test_db
frontend-test:
@echo "🧪 Running frontend tests..."
cd frontend && npm run test
# ============================================================================
# CODE QUALITY & FORMATTING
# ============================================================================
lint-backend:
@echo "📝 Running backend linter..."
cd backend && golangci-lint run ./...
lint-frontend:
@echo "📝 Running frontend linter..."
cd frontend && npm run lint
lint: lint-backend lint-frontend
@echo "✅ All linting passed!"
format:
@echo "🎨 Formatting code..."
cd backend && gofmt -s -w .
cd frontend && npm run format
@echo "✅ Code formatted!"
# ============================================================================
# DOCKER MANAGEMENT
# ============================================================================
docker-up:
@echo "🐳 Starting all containers..."
$(COMPOSE) up -d --build
@echo "✅ Containers started"
@echo ""
@echo " Frontend: http://localhost:5173"
@echo " Backend: http://localhost:8080"
@echo " Postgres: localhost:5434 (openrisk)"
@echo " Redis: localhost:6380"
docker-down:
@echo "🐳 Stopping all containers..."
$(COMPOSE) down
docker-build:
@echo "🐳 Building Docker image..."
docker build -t openrisk:latest .
@echo "✅ Docker image built"
docker-logs:
@echo "📋 Showing live container logs..."
$(COMPOSE) logs -f
docker-status:
@echo "📊 Container status:"
$(COMPOSE) ps
docker-clean:
@echo "🗑️ Removing containers and volumes (DESTRUCTIVE)..."
$(COMPOSE) down -v
@echo "✅ Cleanup complete"
# ============================================================================
# DATABASE MANAGEMENT
# ============================================================================
migrate:
@echo "📊 Running database migrations..."
$(COMPOSE) up -d db
@sleep 2
cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate up
@echo "✅ Migrations complete"
migrate-rollback:
@echo "⏮️ Rolling back last migration..."
$(COMPOSE) up -d db
@sleep 2
cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate down
migrate-status:
@echo "🔎 Current migration version..."
cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate version
# Recovery for a DIRTY database (a migration that failed mid-apply). Set VERSION
# to the last known-good migration, then re-run `make migrate`.
# Example: make migrate-force VERSION=47
migrate-force:
@if [ -z "$(VERSION)" ]; then echo "Usage: make migrate-force VERSION=<known_good_version>"; exit 1; fi
@echo "🔧 Forcing migration version to $(VERSION) (clears dirty flag)..."
cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate force $(VERSION)
@echo "✅ Rollback complete"
seed:
@echo "🌱 Seeding database with sample data..."
cd backend && go run ./cmd/server seed
@echo "✅ Database seeded"
db-shell:
@echo "🔌 Connecting to production database..."
$(COMPOSE) exec db psql -U openrisk -d openrisk
db-test-shell:
@echo "🔌 Connecting to test database..."
$(COMPOSE) exec test_db psql -U test -d openrisk_test
# ============================================================================
# DEVELOPMENT
# ============================================================================
dev:
@echo "🚀 Starting development environment..."
@echo ""
@echo " Starting Docker services (db, redis)..."
$(COMPOSE) up -d db redis test_db
@sleep 2
@echo ""
@echo " Frontend will be available at: http://localhost:5173"
@echo " Backend API at: http://localhost:8080/api/v1"
@echo ""
@echo " Press Ctrl+C to stop. Use 'make docker-down' to stop Docker services."
@echo ""
@sleep 1
@echo "Starting backend (in background)..."
cd backend && go run ./cmd/server &
@sleep 2
@echo ""
@echo "Starting frontend..."
cd frontend && npm run dev
dev-docker:
@echo "🐳 Starting full development environment in Docker..."
$(COMPOSE) up
dev-logs:
@echo "📋 Following all container logs..."
$(COMPOSE) logs -f
# ============================================================================
# CLEANUP
# ============================================================================
clean:
@echo "🧹 Cleaning build artifacts..."
cd backend && go clean
rm -f backend/openrisk
rm -f backend/coverage.out
rm -rf frontend/dist
@echo "✅ Cleanup complete"
clean-all: clean docker-clean
@echo "🗑️ Full cleanup complete (DESTRUCTIVE)"
.DEFAULT_GOAL := help