|
1 | 1 | # Copyright The Linux Foundation and each contributor to LFX. |
2 | 2 | # SPDX-License-Identifier: MIT |
3 | | -.PHONY: deps apigen build run debug test |
4 | 3 |
|
5 | | -# Install the goa CLI tool. |
| 4 | +# Variables |
| 5 | +BINARY_NAME=project-api |
| 6 | +BINARY_PATH=bin/$(BINARY_NAME) |
| 7 | +GO_MODULE=github.com/linuxfoundation/lfx-v2-project-service |
| 8 | +CMD_PATH=$(GO_MODULE)/cmd/project-api |
| 9 | +DESIGN_MODULE=$(CMD_PATH)/design |
| 10 | +GO_FILES=$(shell find . -name '*.go' -not -path './gen/*' -not -path './vendor/*') |
| 11 | +GOA_VERSION=v3 |
| 12 | + |
| 13 | +# Docker variables |
| 14 | +DOCKER_IMAGE=linuxfoundation/lfx-v2-project-service |
| 15 | +DOCKER_TAG=0.1.0 |
| 16 | + |
| 17 | +# Helm variables |
| 18 | +HELM_CHART_PATH=../../charts/lfx-v2-project-service |
| 19 | +HELM_RELEASE_NAME=lfx-v2-project-service |
| 20 | +HELM_NAMESPACE=lfx |
| 21 | + |
| 22 | +# Build variables |
| 23 | +BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S') |
| 24 | +GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown") |
| 25 | +VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev") |
| 26 | +LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT)" |
| 27 | + |
| 28 | +# Test variables |
| 29 | +TEST_FLAGS=-v -race -cover |
| 30 | +TEST_TIMEOUT=5m |
| 31 | + |
| 32 | +.PHONY: all help deps apigen build run debug test test-verbose test-coverage clean lint fmt check verify docker helm-install helm-uninstall |
| 33 | + |
| 34 | +# Default target |
| 35 | +all: clean deps apigen fmt lint test build |
| 36 | + |
| 37 | +# Help target |
| 38 | +help: |
| 39 | + @echo "Available targets:" |
| 40 | + @echo " all - Run clean, deps, apigen, fmt, lint, test, and build" |
| 41 | + @echo " deps - Install dependencies including goa CLI" |
| 42 | + @echo " apigen - Generate API code from design files" |
| 43 | + @echo " build - Build the binary" |
| 44 | + @echo " run - Run the service" |
| 45 | + @echo " debug - Run the service with debug logging" |
| 46 | + @echo " test - Run unit tests" |
| 47 | + @echo " test-verbose - Run tests with verbose output" |
| 48 | + @echo " test-coverage - Run tests with coverage report" |
| 49 | + @echo " clean - Remove generated files and binaries" |
| 50 | + @echo " lint - Run golangci-lint" |
| 51 | + @echo " fmt - Format Go code" |
| 52 | + @echo " check - Run fmt and lint without modifying files" |
| 53 | + @echo " verify - Verify API generation is up to date" |
| 54 | + @echo " docker - Build Docker image" |
| 55 | + @echo " helm-install - Install Helm chart" |
| 56 | + @echo " helm-uninstall - Uninstall Helm chart" |
| 57 | + |
| 58 | +# Install dependencies |
6 | 59 | deps: |
7 | | - go install goa.design/goa/v3/cmd/goa@latest |
| 60 | + @echo "==> Installing dependencies..." |
| 61 | + go mod download |
| 62 | + go install goa.design/goa/$(GOA_VERSION)/cmd/goa@latest |
| 63 | + @command -v golangci-lint >/dev/null 2>&1 || { \ |
| 64 | + echo "==> Installing golangci-lint..."; \ |
| 65 | + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \ |
| 66 | + } |
8 | 67 |
|
9 | | -# Generate the API code from the design files. |
| 68 | +# Generate API code from design files |
10 | 69 | apigen: deps |
11 | | - goa gen github.com/linuxfoundation/lfx-v2-project-service/cmd/project-api/design |
| 70 | + @echo "==> Generating API code..." |
| 71 | + goa gen $(DESIGN_MODULE) |
| 72 | + @echo "==> API generation complete" |
12 | 73 |
|
13 | | -# Build the go program to ensure there aren't any build errors. |
14 | | -build: |
15 | | - go build -o bin/project-api . |
| 74 | +# Build the binary |
| 75 | +build: clean |
| 76 | + @echo "==> Building $(BINARY_NAME)..." |
| 77 | + @mkdir -p bin |
| 78 | + go build $(LDFLAGS) -o $(BINARY_PATH) . |
| 79 | + @echo "==> Build complete: $(BINARY_PATH)" |
16 | 80 |
|
17 | | -# Run the service. |
18 | | -run: |
19 | | - go run . |
| 81 | +# Run the service |
| 82 | +run: apigen |
| 83 | + @echo "==> Running $(BINARY_NAME)..." |
| 84 | + go run $(LDFLAGS) . |
20 | 85 |
|
21 | | -# Run the service with debug logging. |
22 | | -debug: |
23 | | - go run . -d |
| 86 | +# Run with debug logging |
| 87 | +debug: apigen |
| 88 | + @echo "==> Running $(BINARY_NAME) in debug mode..." |
| 89 | + go run $(LDFLAGS) . -d |
24 | 90 |
|
25 | | -# Run the unit tests. |
| 91 | +# Run tests |
26 | 92 | test: |
27 | | - go test . |
| 93 | + @echo "==> Running tests..." |
| 94 | + go test $(TEST_FLAGS) -timeout $(TEST_TIMEOUT) ./... |
| 95 | + |
| 96 | +# Run tests with verbose output |
| 97 | +test-verbose: |
| 98 | + @echo "==> Running tests (verbose)..." |
| 99 | + go test $(TEST_FLAGS) -v -timeout $(TEST_TIMEOUT) ./... |
| 100 | + |
| 101 | +# Run tests with coverage |
| 102 | +test-coverage: |
| 103 | + @echo "==> Running tests with coverage..." |
| 104 | + @mkdir -p coverage |
| 105 | + go test $(TEST_FLAGS) -timeout $(TEST_TIMEOUT) -coverprofile=coverage/coverage.out ./... |
| 106 | + go tool cover -html=coverage/coverage.out -o coverage/coverage.html |
| 107 | + @echo "==> Coverage report: coverage/coverage.html" |
| 108 | + |
| 109 | +# Clean build artifacts |
| 110 | +clean: |
| 111 | + @echo "==> Cleaning build artifacts..." |
| 112 | + @rm -rf bin/ coverage/ |
| 113 | + @go clean -cache |
| 114 | + @echo "==> Clean complete" |
| 115 | + |
| 116 | +# Run linter |
| 117 | +lint: |
| 118 | + @echo "==> Running linter..." |
| 119 | + @if command -v golangci-lint >/dev/null 2>&1; then \ |
| 120 | + golangci-lint run ./...; \ |
| 121 | + else \ |
| 122 | + echo "golangci-lint not found. Run 'make deps' to install it."; \ |
| 123 | + exit 1; \ |
| 124 | + fi |
| 125 | + |
| 126 | +# Format code |
| 127 | +fmt: |
| 128 | + @echo "==> Formatting code..." |
| 129 | + @go fmt ./... |
| 130 | + @gofmt -s -w $(GO_FILES) |
| 131 | + |
| 132 | +# Check formatting and linting without modifying files |
| 133 | +check: |
| 134 | + @echo "==> Checking code format..." |
| 135 | + @if [ -n "$$(gofmt -l $(GO_FILES))" ]; then \ |
| 136 | + echo "The following files need formatting:"; \ |
| 137 | + gofmt -l $(GO_FILES); \ |
| 138 | + exit 1; \ |
| 139 | + fi |
| 140 | + @echo "==> Code format check passed" |
| 141 | + @$(MAKE) lint |
| 142 | + |
| 143 | +# Verify that generated code is up to date |
| 144 | +verify: apigen |
| 145 | + @echo "==> Verifying generated code is up to date..." |
| 146 | + @if [ -n "$$(git status --porcelain gen/)" ]; then \ |
| 147 | + echo "Generated code is out of date. Run 'make apigen' and commit the changes."; \ |
| 148 | + git status --porcelain gen/; \ |
| 149 | + exit 1; \ |
| 150 | + fi |
| 151 | + @echo "==> Generated code is up to date" |
| 152 | + |
| 153 | +# Build Docker image |
| 154 | +docker: |
| 155 | + @echo "==> Building Docker image..." |
| 156 | + docker build -t $(DOCKER_IMAGE):$(DOCKER_TAG) -f ../../Dockerfile ../../ |
| 157 | + @echo "==> Docker image built: $(DOCKER_IMAGE):$(DOCKER_TAG)" |
| 158 | + |
| 159 | +# Install Helm chart |
| 160 | +helm-install: |
| 161 | + @echo "==> Installing Helm chart..." |
| 162 | + helm upgrade --install $(HELM_RELEASE_NAME) $(HELM_CHART_PATH) --namespace $(HELM_NAMESPACE) |
| 163 | + @echo "==> Helm chart installed: $(HELM_RELEASE_NAME)" |
| 164 | + |
| 165 | +# Uninstall Helm chart |
| 166 | +helm-uninstall: |
| 167 | + @echo "==> Uninstalling Helm chart..." |
| 168 | + helm uninstall $(HELM_RELEASE_NAME) --namespace $(HELM_NAMESPACE) |
| 169 | + @echo "==> Helm chart uninstalled: $(HELM_RELEASE_NAME)" |
0 commit comments