-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMakefile
More file actions
162 lines (134 loc) · 7.26 KB
/
Copy pathMakefile
File metadata and controls
162 lines (134 loc) · 7.26 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
# ==================================================================================== #
# VARIABLES
# ==================================================================================== #
BINARY_NAME=freighter-backend
# Assuming main.go is in the root. Adjust if it's in e.g. ./cmd/freighter-backend/
CMD_PATH=.
# Versioning - Reuses LABEL and BUILD_DATE from original file
VERSION ?= $(shell git rev-parse --short HEAD)$(and $(shell git status -s),-dirty-$(shell id -u -n))
BUILD_TIME := $(shell date -u +%FT%TZ)
TAG ?= stellar/freighter-backend-v2:$(VERSION)
# Go build flags
# Inject version info: requires 'var version string', 'var buildTime string' in main package
LDFLAGS = -ldflags="-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)"
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
.DEFAULT_GOAL := help
.PHONY: help tidy fmt vet lint generate check test build run clean all docker-build-local docker-build-tag docker-up docker-push docker-build-up
help: ## Display this help screen
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\nTargets:\n"} /^[a-zA-Z_\-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)
# ==================================================================================== #
# QUALITY & PREPARATION
# ==================================================================================== #
tidy: ## Tidy modfiles and format source files
@echo "==> Tidying module files..."
go mod tidy -v
@echo "==> Formatting code..."
go fmt ./...
$(shell go env GOPATH)/bin/gofumpt -l -w .
@echo "==> Fixing imports..."
@command -v $(shell go env GOPATH)/bin/goimports >/dev/null 2>&1 || { go install golang.org/x/tools/cmd/goimports@v0.31.0; }
@find . -type f -name "*.go" ! -path "*mock*" | xargs $(shell go env GOPATH)/bin/goimports -local "github.com/stellar/freighter-backend-v2" -w
fmt: ## Check if code is formatted with gofmt
@echo "==> Checking formatting..."
@test -z $(shell gofmt -l .) || (echo "ERROR: Unformatted files found. Run 'make tidy' or 'gofmt -w .'"; exit 1)
@echo "Format check passed."
vet: ## Run go vet checks
@echo "==> Running go vet..."
go vet ./...
lint: ## Run golangci-lint linter (requires: brew install golangci-lint or equivalent)
@echo "==> Running golangci-lint..."
@command -v golangci-lint >/dev/null 2>&1 || { echo >&2 "ERROR: golangci-lint not found. Install it: https://golangci-lint.run/usage/install/"; exit 1; }
golangci-lint run ./...
generate: ## Run go generate
@echo "==> Running go generate..."
go generate ./...
shadow: ## Run shadow analysis to find shadowed variables
@echo "==> Running shadow analyzer..."
@if ! command -v shadow >/dev/null 2>&1; then \
echo "Installing shadow..."; \
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@v0.31.0; \
fi
$(shell go env GOPATH)/bin/shadow ./...
exhaustive: ## Check exhaustiveness of switch statements
@echo "==> Running exhaustive..."
@command -v exhaustive >/dev/null 2>&1 || { go install github.com/nishanths/exhaustive/cmd/exhaustive@v0.12.0; }
$(shell go env GOPATH)/bin/exhaustive -default-signifies-exhaustive ./...
deadcode: ## Find unused code
@echo "==> Checking for deadcode..."
@if ! command -v deadcode >/dev/null 2>&1; then \
echo "Installing deadcode..."; \
go install golang.org/x/tools/cmd/deadcode@v0.31.0; \
fi
@output=$$($(shell go env GOPATH)/bin/deadcode -test ./...); \
if [ -n "$$output" ]; then \
echo "🚨 Deadcode found:"; \
echo "$$output"; \
exit 1; \
else \
echo "✅ No deadcode found"; \
fi
# exhaustive is intentionally omitted from `check`: golangci-lint already runs the
# exhaustive analyzer with the same settings (see .golangci.yml), so the standalone
# target is redundant. It is also kept out of the chain because exhaustive@v0.12.0
# (its latest release) pins x/tools@v0.15.0, which fails to compile under Go 1.26.
# Run `make exhaustive` directly on Go <=1.25 if you want the standalone tool.
check: tidy fmt vet lint generate shadow deadcode ## Run all checks
@echo "✅ All checks completed successfully"
# ==================================================================================== #
# TESTING
# ==================================================================================== #
unit-test: ## Run unit tests
@echo "==> Running unit tests..."
ENABLE_INTEGRATION_TESTS=false go test -v -race ./...
unit-test-coverage: ## Run unit tests with coverage
@echo "==> Running unit tests with coverage..."
ENABLE_INTEGRATION_TESTS=false go test -v -race -cover -coverprofile=c.out ./...
integration-test: ## Run integration tests
@echo "==> Running integration tests..."
ENABLE_INTEGRATION_TESTS=true go test -v ./internal/integrationtests/... ./internal/db/...
test-all: unit-test-coverage integration-test ## Run all tests
@echo "✅ All tests completed successfully"
# ==================================================================================== #
# BUILD & RUN
# ==================================================================================== #
build: ## Build the application binary with version info
@echo "==> Building binary..."
go build $(LDFLAGS) -o $(BINARY_NAME) $(CMD_PATH)
@echo "Binary created: ./$(BINARY_NAME)"
run: build ## Build and run the application
@echo "==> Running application..."
./$(BINARY_NAME)
# ==================================================================================== #
# CLEANUP
# ==================================================================================== #
clean: ## Remove build artifacts
@echo "==> Cleaning..."
rm -f $(BINARY_NAME)
go clean
# ==================================================================================== #
# ALL
# ==================================================================================== #
all: tidy check test build ## Run tidy, checks, tests, and build
# ==================================================================================== #
# DOCKER OPERATIONS
# ==================================================================================== #
# Check if we need to prepend docker commands with sudo
SUDO := $(shell docker version >/dev/null 2>&1 || echo "sudo")
docker-build-local: ## Build docker image locally using compose
docker compose -f deployments/docker-compose.yml -p freighter-backend build
docker-build-tag: ## Build docker image and tag it
$(SUDO) docker build --pull --label org.opencontainers.image.created="$(BUILD_TIME)" -t $(TAG) -f deployments/Dockerfile .
docker-up: ## Start docker containers using compose
docker compose -f deployments/docker-compose.yml -p freighter-backend up
docker-push: ## Push tagged docker image
$(SUDO) docker push $(TAG)
docker-build-up: docker-build-local docker-up ## Build locally and start containers
# ==================================================================================== #
# DATABASE
# ==================================================================================== #
# Switch between local and a hosted CNPG env by changing only DATABASE_URL.
# Local dev uses docker-compose; to run against a hosted database, export
# DATABASE_URL yourself (same as horizon/RPC/wallet-backend) — see
# internal/db/README.md. Concrete per-env values live in wallet-eng-runbooks.