-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
292 lines (248 loc) · 9.72 KB
/
Makefile
File metadata and controls
292 lines (248 loc) · 9.72 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
# Midaz Go SDK Makefile
# Color definitions - empty to disable colors
YELLOW :=
GREEN :=
CYAN :=
RED :=
NC :=
BOLD :=
# Component-specific variables
SERVICE_NAME := Midaz Go SDK
BIN_DIR := ./bin
ARTIFACTS_DIR := ./artifacts
DOCS_DIR := ./docs/godoc
VERSION := 1.0.0
# Ensure directories exist
$(shell mkdir -p $(ARTIFACTS_DIR))
$(shell mkdir -p $(DOCS_DIR))
# Define a simple function for section headers
define print_header
@echo ""
@echo "==== $(1) ===="
@echo ""
endef
# Go commands
GO := go
GOFMT := gofmt
GOLINT := golangci-lint
GOSEC_VERSION := v2.24.7
GOSEC := $(GO) run github.com/securego/gosec/v2/cmd/gosec@$(GOSEC_VERSION)
GOMOD := $(GO) mod
GOBUILD := $(GO) build
GOTEST := $(GO) test
GOTOOL := $(GO) tool
GOCLEAN := $(GO) clean
# Project variables
PROJECT_ROOT := $(shell pwd)
PROJECT_NAME := midaz-go-sdk
LDFLAGS := -ldflags "-X main.Version=$(VERSION)"
MODULE := $(shell $(GO) list -m)
# Environment variables
ENV_FILE := $(PROJECT_ROOT)/.env
ENV_EXAMPLE_FILE := $(PROJECT_ROOT)/.env.example
# Load environment variables if .env exists
ifneq (,$(wildcard .env))
include .env
endif
#-------------------------------------------------------
# Core Commands
#-------------------------------------------------------
.PHONY: help
help:
@echo ""
@echo "$(SERVICE_NAME) Commands"
@echo ""
@echo "Core Commands:"
@echo " make help - Display this help message"
@echo " make set-env - Create .env file from .env.example if it doesn't exist"
@echo " make test - Run all tests"
@echo " make test-fast - Run tests with -short flag"
@echo " make clean - Clean build artifacts"
@echo " make coverage - Generate test coverage report"
@echo ""
@echo "Code Quality Commands:"
@echo " make lint - Run linting tools"
@echo " make fmt - Format code"
@echo " make tidy - Clean dependencies"
@echo " make verify-sdk - Run SDK quality checks"
@echo " make hooks - Install git hooks"
@echo " make gosec - Run security checks with gosec"
@echo ""
@echo "Example Commands:"
@echo " make example - Run complete workflow example"
@echo " make demo-data - Run mass demo data generator (interactive)"
@echo ""
@echo "Documentation Commands:"
@echo " make godoc - Start a godoc server for interactive documentation"
@echo " make godoc-static - Generate static documentation files"
@echo " make docs - Generate comprehensive documentation (includes godoc-static)"
@echo ""
#-------------------------------------------------------
# Environment Setup
#-------------------------------------------------------
.PHONY: set-env
set-env:
$(call print_header,"Setting up environment")
@if [ ! -f "$(ENV_FILE)" ] && [ -f "$(ENV_EXAMPLE_FILE)" ]; then \
echo "$(YELLOW)No .env file found. Creating from .env.example...$(NC)"; \
cp $(ENV_EXAMPLE_FILE) $(ENV_FILE); \
echo "$(GREEN)[ok]$(NC) Created .env file from .env.example$(GREEN) ✔️$(NC)"; \
elif [ ! -f "$(ENV_FILE)" ] && [ ! -f "$(ENV_EXAMPLE_FILE)" ]; then \
echo "$(RED)[error]$(NC) Neither .env nor .env.example files found$(RED) ❌$(NC)"; \
exit 1; \
elif [ -f "$(ENV_FILE)" ]; then \
read -t 10 -p "$(YELLOW).env file already exists. Overwrite with .env.example? [Y/n] (auto-yes in 10s)$(NC) " answer || answer="Y"; \
answer=$${answer:-Y}; \
if [[ $$answer =~ ^[Yy] ]]; then \
cp $(ENV_EXAMPLE_FILE) $(ENV_FILE); \
echo "$(GREEN)[ok]$(NC) Overwrote .env file with .env.example$(GREEN) ✔️$(NC)"; \
else \
echo "$(YELLOW)[skipped]$(NC) Kept existing .env file$(YELLOW) ⚠️$(NC)"; \
fi; \
fi
#-------------------------------------------------------
# SDK Quality Check Targets
#-------------------------------------------------------
.PHONY: check-references check-api-compatibility verify-sdk hooks
# Check that no lib-commons references appear in public packages
check-references:
@echo "$(YELLOW)Checking for lib-commons references in public API...$(NC)"
@! grep -r "lib-commons" --include="*.go" ./models ./entities | grep -v "//.*lib-commons" || (echo "$(RED)❌ Found lib-commons references in public API!$(NC)" && exit 1)
@echo "$(GREEN)✅ No lib-commons references found in public API$(NC)"
# Verify that our refactoring doesn't break API compatibility
check-api-compatibility:
@echo "$(YELLOW)Checking API compatibility...$(NC)"
@go build ./models ./entities ./pkg/...
@echo "$(GREEN)✅ API builds successfully$(NC)"
# Verify our implementation
verify-sdk: check-references check-api-compatibility
@echo "$(GREEN)✅ All SDK quality checks passed!$(NC)"
# Install git hooks
hooks:
$(call print_header,"Installing Git Hooks")
@chmod +x scripts/install-hooks.sh
@./scripts/install-hooks.sh
#-------------------------------------------------------
# Test Commands
#-------------------------------------------------------
.PHONY: test test-fast coverage
test:
$(call print_header,"Running tests")
@./scripts/run_tests.sh
test-fast:
$(call print_header,"Running fast tests")
@GOTEST_SHORT=1 ./scripts/run_tests.sh
coverage:
$(call print_header,"Generating test coverage")
@$(GOTEST) -coverprofile=$(ARTIFACTS_DIR)/coverage.out $$(go list ./... | grep -v -E '(examples|mocks|/version$$)')
@$(GOTOOL) cover -html=$(ARTIFACTS_DIR)/coverage.out -o $(ARTIFACTS_DIR)/coverage.html
@echo "Coverage report generated at $(ARTIFACTS_DIR)/coverage.html"
@echo "$(GREEN)[ok]$(NC) Coverage report generated successfully"
#-------------------------------------------------------
# Code Quality Commands
#-------------------------------------------------------
.PHONY: lint fmt tidy gosec
lint:
$(call print_header,"Running linters")
@if find . -name "*.go" -type f | grep -q .; then \
if ! command -v $(GOLINT) > /dev/null; then \
echo "$(YELLOW)Installing golangci-lint...$(NC)"; \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \
fi; \
$(GOLINT) run; \
echo "$(GREEN)[ok]$(NC) Linting completed successfully$(GREEN) ✔️$(NC)"; \
else \
echo "$(YELLOW)No Go files found, skipping linting$(NC)"; \
fi
fmt:
$(call print_header,"Formatting code")
@$(GOFMT) -s -w .
@echo "$(GREEN)[ok]$(NC) Formatting completed successfully$(GREEN) ✔️$(NC)"
tidy:
$(call print_header,"Cleaning dependencies")
@$(GOMOD) tidy
@echo "$(GREEN)[ok]$(NC) Dependencies cleaned successfully$(GREEN) ✔️$(NC)"
gosec:
$(call print_header,"Running security checks")
@echo "$(CYAN)Running gosec security scanner ($(GOSEC_VERSION))...$(NC)"
@$(GOSEC) -quiet ./...
@echo "$(GREEN)[ok]$(NC) Security checks completed successfully$(GREEN) ✔️$(NC)"
#-------------------------------------------------------
# Clean Commands
#-------------------------------------------------------
.PHONY: clean
clean:
$(call print_header,"Cleaning build artifacts")
@echo "$(CYAN)Cleaning build artifacts...$(NC)"
@$(GOCLEAN)
@rm -rf $(BIN_DIR)/ $(ARTIFACTS_DIR)/coverage.out $(ARTIFACTS_DIR)/coverage.html
@echo "$(GREEN)[ok]$(NC) Artifacts cleaned successfully$(GREEN) ✔️$(NC)"
#-------------------------------------------------------
# Example Commands
#-------------------------------------------------------
.PHONY: example
example:
$(call print_header,"Running Complete Workflow Example")
$(call print_header,"Make sure the Midaz Stack is running --default is localhost")
@cp $(ENV_FILE) examples/workflow-with-entities/.env
@cd examples/workflow-with-entities && go run main.go
.PHONY: demo-data
demo-data:
$(call print_header,Running Mass Demo Data Generator)
$(call print_header,Ensure Midaz services are running on localhost:3000 (onboarding) and :3001 (transaction))
@if [ -f "$(ENV_FILE)" ]; then \
cp $(ENV_FILE) examples/mass-demo-generator/.env; \
else \
echo "⚠️ Warning: $(ENV_FILE) not found. Run 'make set-env' first or create .env manually."; \
exit 1; \
fi
@cd examples/mass-demo-generator && DEMO_NON_INTERACTIVE=0 go run .
#-------------------------------------------------------
# Documentation Commands
#-------------------------------------------------------
.PHONY: godoc godoc-static docs
godoc:
$(call print_header,"Starting godoc server")
@echo "$(CYAN)Starting godoc server at http://localhost:6060/pkg/$(MODULE)/$(NC)"
@if ! command -v godoc > /dev/null; then \
echo "$(YELLOW)Installing godoc...$(NC)"; \
go install golang.org/x/tools/cmd/godoc@latest; \
fi
@godoc -http=:6060
# List of packages to generate documentation for
PACKAGES := \
$(MODULE) \
$(MODULE)/entities \
$(MODULE)/models \
$(MODULE)/pkg/config \
$(MODULE)/pkg/concurrent \
$(MODULE)/pkg/observability \
$(MODULE)/pkg/pagination \
$(MODULE)/pkg/validation \
$(MODULE)/pkg/validation/core \
$(MODULE)/pkg/errors \
$(MODULE)/pkg/format \
$(MODULE)/pkg/retry \
$(MODULE)/pkg/performance
godoc-static:
$(call print_header,"Generating static documentation")
@echo "$(CYAN)Generating static documentation...$(NC)"
@rm -rf $(DOCS_DIR)
@mkdir -p $(DOCS_DIR)
@# Process each package
@for pkg in $(PACKAGES) ; do \
echo "$(CYAN)Generating documentation for $${pkg}...$(NC)" ; \
if [ "$$pkg" = "$(MODULE)" ]; then \
pkg_path="." ; \
else \
pkg_path=$${pkg#$(MODULE)/}; \
fi ; \
pkg_dir=$(DOCS_DIR)/$${pkg_path} ; \
mkdir -p $${pkg_dir} ; \
go doc $${pkg} > $${pkg_dir}/index.txt ; \
done
@echo "$(GREEN)[ok]$(NC) Static documentation generated successfully in $(DOCS_DIR)$(GREEN) ✔️$(NC)"
# Just run godoc-static for now, as we have manually edited README.md
docs: godoc-static
$(call print_header,"Documentation generation complete")
@echo "$(GREEN)[ok]$(NC) Documentation generated successfully$(GREEN) ✔️$(NC)"