-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
135 lines (105 loc) · 4.36 KB
/
Makefile
File metadata and controls
135 lines (105 loc) · 4.36 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
.PHONY: all build test lint vet fmt fmt-check deps import-crds clean coverage cloc help
REPO_PATH := github.com/cocoonstack/cocoon-operator
BINARY_NAME := cocoon-operator
GOIMPORTS_LOCAL_PREFIXES := github.com/cocoonstack/
## Target OSes for vet / lint
GOOSES ?= linux darwin
REVISION := $(shell git rev-parse HEAD || echo unknown)
BUILTAT := $(shell date +%Y-%m-%dT%H:%M:%S)
VERSION := $(shell git describe --tags $(shell git rev-list --tags --max-count=1) 2>/dev/null || echo dev)
GO_LDFLAGS ?= -X $(REPO_PATH)/version.REVISION=$(REVISION) \
-X $(REPO_PATH)/version.BUILTAT=$(BUILTAT) \
-X $(REPO_PATH)/version.VERSION=$(VERSION)
ifneq ($(KEEP_SYMBOL), 1)
GO_LDFLAGS += -s
endif
BUILD_OUT ?= $(BINARY_NAME)
ifneq ($(GOOS),)
ifneq ($(GOARCH),)
BUILD_OUT := $(BINARY_NAME)-$(GOOS)-$(GOARCH)
endif
endif
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
## Tool versions
GOLANGCILINT_VERSION ?= v2.9.0
GOLANGCILINT_ROOT := $(LOCALBIN)/golangci-lint-$(GOLANGCILINT_VERSION)
GOLANGCILINT := $(GOLANGCILINT_ROOT)/golangci-lint
GOFMT := $(LOCALBIN)/gofumpt
GOIMPORTS := $(LOCALBIN)/goimports
## Tool download targets
.PHONY: golangci-lint
golangci-lint: $(GOLANGCILINT)
$(GOLANGCILINT):
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOLANGCILINT_ROOT) $(GOLANGCILINT_VERSION)
.PHONY: gofumpt
gofumpt: $(GOFMT)
$(GOFMT): | $(LOCALBIN)
GOBIN=$(LOCALBIN) go install mvdan.cc/gofumpt@latest
.PHONY: goimports
goimports: $(GOIMPORTS)
$(GOIMPORTS): | $(LOCALBIN)
GOBIN=$(LOCALBIN) go install golang.org/x/tools/cmd/goimports@latest
# --- Primary targets ---
all: deps fmt lint test build ## Full pipeline: deps, fmt, lint, test, build
# --- Dependencies ---
deps: ## Tidy Go modules (no-op when running inside a Go workspace)
@if [ -z "$$(go env GOWORK)" ] || [ "$$(go env GOWORK)" = "off" ]; then \
go mod tidy; \
else \
echo "==> workspace mode active ($$(go env GOWORK)); skipping go mod tidy"; \
fi
import-crds: ## Copy CRD YAML from cocoon-common into config/crd/bases
@COCOON_COMMON_DIR="$$(go list -m -f '{{.Dir}}' github.com/cocoonstack/cocoon-common)"; \
if [ -z "$$COCOON_COMMON_DIR" ]; then \
echo "could not resolve github.com/cocoonstack/cocoon-common module path" >&2; \
exit 1; \
fi; \
rm -rf config/crd/bases; \
mkdir -p config/crd/bases; \
cp $$COCOON_COMMON_DIR/apis/v1/crds/*.yaml config/crd/bases/; \
echo "imported $$(ls config/crd/bases | wc -l | tr -d ' ') CRDs from $$COCOON_COMMON_DIR"
# --- Build ---
build: ## Build cocoon-operator binary
CGO_ENABLED=0 go build -ldflags "$(GO_LDFLAGS)" -o $(BUILD_OUT) .
# --- Testing ---
test: vet ## Run tests with race detection and coverage
go test -race -timeout 120s -count=1 -cover -coverprofile=coverage.out ./...
coverage: test ## Generate and display coverage report
go tool cover -func=coverage.out
@echo ""
@echo "To view HTML coverage report: go tool cover -html=coverage.out"
# --- Code quality ---
vet: ## Run go vet on every target OS
@for goos in $(GOOSES); do \
echo "==> go vet GOOS=$$goos"; \
GOOS=$$goos go vet ./... || exit 1; \
done
lint: golangci-lint ## Run golangci-lint on every target OS
@for goos in $(GOOSES); do \
echo "==> golangci-lint GOOS=$$goos"; \
GOOS=$$goos $(GOLANGCILINT) run ./... || exit 1; \
done
fmt: gofumpt goimports ## Format code with gofumpt and goimports
$(GOFMT) -l -w .
$(GOIMPORTS) -l -w --local '$(GOIMPORTS_LOCAL_PREFIXES)' .
fmt-check: gofumpt goimports ## Check formatting (fails if files need formatting)
@test -z "$$($(GOFMT) -l .)" || { echo "Files need formatting (gofumpt):"; $(GOFMT) -l .; exit 1; }
@test -z "$$($(GOIMPORTS) -l .)" || { echo "Files need formatting (goimports):"; $(GOIMPORTS) -l .; exit 1; }
# --- Maintenance ---
clean: ## Remove build artifacts, coverage files, and test cache
rm -f $(BINARY_NAME) $(BINARY_NAME)-linux-* $(BINARY_NAME)-darwin-*
rm -rf bin/ dist/
rm -f coverage.out coverage.html coverage.txt
go clean -testcache
cloc: ## Count lines of code excluding tests (requires cloc)
cloc --exclude-dir=vendor,dist --exclude-ext=json --not-match-f='_test\.go$$' .
# --- Help ---
help: ## Show this help message
@echo "cocoon-operator Makefile targets:"
@echo ""
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
@echo ""