-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathMakefile
More file actions
215 lines (175 loc) · 6.69 KB
/
Copy pathMakefile
File metadata and controls
215 lines (175 loc) · 6.69 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
HOMEDIR := $(shell pwd)
OUTDIR := $(HOMEDIR)/output
GOPKGS := $$(go list ./...| grep -vE "vendor|/cmd")
COVERPKGS := $$(go list ./... | tr '\n' ',' | sed 's/,$$//')
BINARY := bitflip
BINARY6 := bitflip6
BINARYBA := baize
BINARYMP := mping
BINARYMP6 := mping6
BINARYKN := kuiniu
BINARYEVR := evr
BINARYTR := traceroute
COVERAGE_FILE := coverage.out
COVERAGE_HTML := coverage.html
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
DATE := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
LDFLAGS := -s -w -X github.com/baidu/nettools/version.Version=$(VERSION) -X github.com/baidu/nettools/version.Commit=$(COMMIT) -X github.com/baidu/nettools/version.Date=$(DATE)
# Colors for terminal output
COLOR_RESET := \033[0m
COLOR_BOLD := \033[1m
COLOR_GREEN := \033[32m
COLOR_YELLOW := \033[33m
COLOR_BLUE := \033[34m
# make, make all
all: prepare compile package
prepare:
go mod download
# make compile
compile: build build6 build-baize build-mping build-mping6 build-kuiniu build-evr build-traceroute
build:
go build -ldflags "$(LDFLAGS)" -o $(HOMEDIR)/$(BINARY) ./cmd/bitflip/
build6:
go build -ldflags "$(LDFLAGS)" -o $(HOMEDIR)/$(BINARY6) ./cmd/bitflip6/
build-baize:
go build -ldflags "$(LDFLAGS)" -o $(HOMEDIR)/$(BINARYBA) ./cmd/baize/
build-mping:
go build -ldflags "$(LDFLAGS)" -o $(HOMEDIR)/$(BINARYMP) ./cmd/mping/
build-mping6:
go build -ldflags "$(LDFLAGS)" -o $(HOMEDIR)/$(BINARYMP6) ./cmd/mping6/
build-kuiniu:
mkdir -p $(OUTDIR)
go build -ldflags "$(LDFLAGS)" -o $(OUTDIR)/$(BINARYKN) ./cmd/kuiniu/
build-evr:
mkdir -p $(OUTDIR)
go build -ldflags "$(LDFLAGS)" -o $(OUTDIR)/$(BINARYEVR) ./cmd/evr/
build-traceroute:
go build -ldflags "$(LDFLAGS)" -o $(HOMEDIR)/$(BINARYTR) ./cmd/traceroute/
## test: Run all tests
test: prepare
go test -race -timeout=120s -v -cover $(GOPKGS) -coverprofile=$(COVERAGE_FILE)
## test-short: Run tests in short mode
test-short:
@echo "$(COLOR_BLUE)Running tests (short mode)...$(COLOR_RESET)"
go test -short ./...
## test-race: Run tests with race detector
test-race:
@echo "$(COLOR_BLUE)Running tests with race detector...$(COLOR_RESET)"
go test -race ./...
## test-coverage: Run tests with coverage and generate HTML report
test-coverage:
@echo "$(COLOR_BLUE)Running tests with coverage...$(COLOR_RESET)"
go test -coverprofile=$(COVERAGE_FILE) -covermode=atomic -coverpkg=$(COVERPKGS) $(GOPKGS)
@echo "$(COLOR_GREEN)Coverage report generated: $(COVERAGE_FILE)$(COLOR_RESET)"
go tool cover -html=$(COVERAGE_FILE) -o $(COVERAGE_HTML)
@echo "$(COLOR_GREEN)HTML coverage report: $(COVERAGE_HTML)$(COLOR_RESET)"
## treemap: Generate SVG treemap of test coverage
treemap: test-coverage
go-cover-treemap -coverprofile $(COVERAGE_FILE) > coverage.svg
@echo "$(COLOR_GREEN)SVG coverage treemap: coverage.svg$(COLOR_RESET)"
## test-verbose: Run tests with verbose output
test-verbose:
@echo "$(COLOR_BLUE)Running tests (verbose)...$(COLOR_RESET)"
go test -v -count=1 ./...
## benchmark: Run benchmarks
benchmark:
@echo "$(COLOR_BLUE)Running benchmarks...$(COLOR_RESET)"
go test -bench=. -benchmem ./...
## test-codec: Run codec package tests only
test-codec:
@echo "$(COLOR_BLUE)Running codec tests...$(COLOR_RESET)"
go test -v ./sonar/codec/...
## test-stat: Run stat package tests only
test-stat:
@echo "$(COLOR_BLUE)Running stat tests...$(COLOR_RESET)"
go test -v ./stat/...
## test-client: Run client package tests only
test-client:
@echo "$(COLOR_BLUE)Running client tests...$(COLOR_RESET)"
go test -v ./sonar/client/...
## test-server: Run server package tests only
test-server:
@echo "$(COLOR_BLUE)Running server tests...$(COLOR_RESET)"
go test -v ./sonar/server/...
## test-config: Run config package tests only
test-config:
@echo "$(COLOR_BLUE)Running config tests...$(COLOR_RESET)"
go test -v ./sonar/config/...
# make package
package:
rm -rf $(OUTDIR)
mkdir -p $(OUTDIR)
mv $(BINARY) $(OUTDIR)/
mv $(BINARY6) $(OUTDIR)/
mv $(BINARYBA) $(OUTDIR)/
mv $(BINARYMP) $(OUTDIR)/
mv $(BINARYMP6) $(OUTDIR)/
mv $(BINARYTR) $(OUTDIR)/
mv $(BINARYKN) $(OUTDIR)/
mv $(BINARYEVR) $(OUTDIR)/
# make lint
lint:
golangci-lint run ./...
## fmt: Format all Go files
fmt:
@echo "$(COLOR_BLUE)Formatting code...$(COLOR_RESET)"
gofmt -s -w .
@echo "$(COLOR_GREEN)Code formatted successfully$(COLOR_RESET)"
## fmt-check: Check if code is formatted
fmt-check:
@echo "$(COLOR_BLUE)Checking code formatting...$(COLOR_RESET)"
@test -z "$$(gofmt -l .)" || (echo "$(COLOR_YELLOW)The following files need formatting:$(COLOR_RESET)" && gofmt -l . && exit 1)
@echo "$(COLOR_GREEN)All files are properly formatted$(COLOR_RESET)"
## vet: Run go vet
vet:
@echo "$(COLOR_BLUE)Running go vet...$(COLOR_RESET)"
go vet ./...
## check: Run fmt-check, vet, and lint
check: fmt-check vet lint
@echo "$(COLOR_GREEN)All checks passed!$(COLOR_RESET)"
# make clean
clean:
go clean
rm -rf $(OUTDIR)
rm -f $(COVERAGE_FILE) $(COVERAGE_HTML) coverage.svg
## tidy: Tidy and verify dependencies
tidy:
@echo "$(COLOR_BLUE)Tidying dependencies...$(COLOR_RESET)"
go mod tidy
go mod verify
@echo "$(COLOR_GREEN)Dependencies tidied$(COLOR_RESET)"
## install-tools: Install development tools
install-tools:
@echo "$(COLOR_BLUE)Installing development tools...$(COLOR_RESET)"
@which golangci-lint > /dev/null || (echo "Installing golangci-lint..." && \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest)
@which go-cover-treemap > /dev/null || (echo "Installing go-cover-treemap..." && \
go install github.com/nikolaydubina/go-cover-treemap@latest)
@echo "$(COLOR_GREEN)Tools installed$(COLOR_RESET)"
## ci: Run continuous integration checks
ci: prepare check test-race test-coverage
@echo "$(COLOR_GREEN)CI checks passed!$(COLOR_RESET)"
## snapshot: Build local snapshot with goreleaser (no publish)
snapshot:
goreleaser build --snapshot --clean
## deploy: Create a release with goreleaser
deploy:
goreleaser release --clean
## help: Display this help message
help:
@echo "$(COLOR_BOLD)nettools - Makefile Commands$(COLOR_RESET)"
@echo ""
@echo "$(COLOR_BOLD)Usage:$(COLOR_RESET)"
@echo " make $(COLOR_GREEN)<target>$(COLOR_RESET)"
@echo ""
@echo "$(COLOR_BOLD)Available targets:$(COLOR_RESET)"
@awk '/^## / { \
sub(/^## /, ""); \
split($$0, a, ":"); \
printf " $(COLOR_GREEN)%-18s$(COLOR_RESET)- %s\n", a[1], a[2]; \
}' Makefile
.PHONY: all prepare compile test test-short test-race test-coverage treemap test-verbose benchmark \
test-codec test-stat test-client test-server test-config \
lint fmt fmt-check vet check clean tidy install-tools ci help build build6 build-baize build-mping build-mping6 build-kuiniu build-evr build-traceroute package \
snapshot deploy