-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
138 lines (116 loc) · 3.91 KB
/
Makefile
File metadata and controls
138 lines (116 loc) · 3.91 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
# Build configuration
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Directories and files
BUILD_DIR := ./build
BINARY_NAME := memenow-resource-manager
CMD_DIR := ./cmd
# Docker configuration
DOCKER_IMAGE_NAME := ghcr.io/memenow/memenow-resource-manager
# Go build configuration
GO := go
GOFLAGS := -v
CGO_ENABLED := 0
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
# Linker flags to inject version information
LDFLAGS := -w -s \
-X main.Version=$(VERSION) \
-X main.BuildTime=$(BUILD_TIME) \
-X main.GitCommit=$(GIT_COMMIT)
# Build command
GO_BUILD_CMD := CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) \
$(GO) build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME) $(CMD_DIR)/main.go
.PHONY: all build clean test lint fmt vet container container-push help
# Default target
all: clean fmt vet test build
## help: Display this help message
help:
@echo "Available targets:"
@echo " build - Build the binary"
@echo " clean - Remove build artifacts"
@echo " test - Run tests"
@echo " lint - Run golangci-lint"
@echo " fmt - Format code with gofmt"
@echo " vet - Run go vet"
@echo " container - Build Docker container"
@echo " container-push - Build and push Docker container"
@echo " all - Run clean, fmt, vet, test, and build"
## build: Build the binary
build:
@echo "Building $(BINARY_NAME) version $(VERSION)..."
@mkdir -p $(BUILD_DIR)
$(GO_BUILD_CMD)
@echo "Build complete: $(BUILD_DIR)/$(BINARY_NAME)"
## clean: Remove build artifacts
clean:
@echo "Cleaning build artifacts..."
@rm -rf $(BUILD_DIR)
@rm -f coverage.out coverage.html
@echo "Clean complete"
## test: Run all tests
test:
@echo "Running tests..."
@$(GO) test -v -race -coverprofile=coverage.out ./...
@echo "Tests complete"
## test-coverage: Run tests with coverage report
test-coverage: test
@echo "Generating coverage report..."
@$(GO) tool cover -html=coverage.out -o coverage.html
@echo "Coverage report generated: coverage.html"
## lint: Run golangci-lint (requires golangci-lint to be installed)
lint:
@echo "Running linter..."
@if command -v golangci-lint >/dev/null 2>&1; then \
golangci-lint run ./...; \
else \
echo "golangci-lint not found. Install from https://golangci-lint.run/"; \
exit 1; \
fi
## fmt: Format all Go code
fmt:
@echo "Formatting code..."
@$(GO) fmt ./...
@echo "Formatting complete"
## vet: Run go vet
vet:
@echo "Running go vet..."
@$(GO) vet ./...
@echo "Vet complete"
## mod-download: Download dependencies
mod-download:
@echo "Downloading dependencies..."
@$(GO) mod download
@echo "Dependencies downloaded"
## mod-tidy: Tidy dependencies
mod-tidy:
@echo "Tidying dependencies..."
@$(GO) mod tidy
@echo "Dependencies tidied"
## mod-verify: Verify dependencies
mod-verify:
@echo "Verifying dependencies..."
@$(GO) mod verify
@echo "Dependencies verified"
## container: Build Docker container
container: build
@echo "Building Docker image $(DOCKER_IMAGE_NAME):$(VERSION)..."
@docker build -t $(DOCKER_IMAGE_NAME):$(VERSION) $(BUILD_DIR)
@docker tag $(DOCKER_IMAGE_NAME):$(VERSION) $(DOCKER_IMAGE_NAME):latest
@echo "Docker image built: $(DOCKER_IMAGE_NAME):$(VERSION)"
## container-push: Build and push Docker container
container-push: container
@echo "Pushing Docker image $(DOCKER_IMAGE_NAME):$(VERSION)..."
@docker push $(DOCKER_IMAGE_NAME):$(VERSION)
@docker push $(DOCKER_IMAGE_NAME):latest
@echo "Docker image pushed"
## run: Build and run the binary
run: build
@echo "Running $(BINARY_NAME)..."
@$(BUILD_DIR)/$(BINARY_NAME)
## install-tools: Install development tools
install-tools:
@echo "Installing development tools..."
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@echo "Tools installed"