-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
111 lines (87 loc) · 3.36 KB
/
Makefile
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
# Go parameters
GO = go
BIN = $(CURDIR)/.bin
LINT_CONFIG = $(CURDIR)/.golangci.yaml
MODULE = $(shell $(GO) list -m)
DATE ?= $(shell date +%FT%T%z)
VERSION ?= $(shell git describe --tags --always --dirty --match="[0-9]*.[0-9]*.[0-9]*" 2> /dev/null || \
cat $(CURDIR)/.version 2> /dev/null || echo v0)
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null)
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
LDFLAGS_VERSION := -X main.version=$(VERSION) -X main.gitCommit=$(COMMIT) -X main.gitBranch=$(BRANCH) -X \"main.buildDate=$(DATE)\"
# platforms and architectures for release; default to MacOS (darwin) and arm64 (M1/M2)
TARGETOS := $(or $(TARGETOS), darwin)
TARGETARCH := $(or $(TARGETARCH), arm64)
PLATFORMS = darwin linux windows
ARCHITECTURES = amd64 arm64
TIMEOUT = 15
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell printf "\033[34;1m▶\033[0m")
export CGO_ENABLED=0
export GOPROXY=https://proxy.golang.org
export GOOS=$(TARGETOS)
export GOARCH=$(TARGETARCH)
.PHONY: all
all: fmt lint test build
.PHONY: dependency
dependency: ; $(info $(M) downloading dependencies...) @ ## Build program binary
$Q $(GO) mod download
.PHONY: build
build: dependency | ; $(info $(M) building $(GOOS)/$(GOARCH) binary...) @ ## Build program binary
$Q env GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) $(GO) build \
-tags release \
-ldflags "$(LDFLAGS_VERSION)" \
-o $(BIN)/$(basename $(MODULE)) ./cmd/main.go
.PHONY: release
release: clean ; $(info $(M) building binaries for multiple os/arch...) @ ## Build program binary for paltforms and os
$(foreach GOOS, $(PLATFORMS),\
$(foreach GOARCH, $(ARCHITECTURES), \
$(shell \
GOPROXY=$(GOPROXY) CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) \
$(GO) build \
-tags release \
-ldflags "$(LDFLAGS_VERSION)" \
-o $(BIN)/$(basename $(MODULE))_$(GOOS)_$(GOARCH) ./cmd/main.go || true)))
# Tools
setup-tools: setup-lint setup-gomock
setup-lint:
$(GO) install github.com/golangci/golangci-lint/cmd/[email protected]
setup-gomock:
$(GO) install github.com/golang/mock/[email protected]
GOLINT=golangci-lint
GOMOCK=gomock
# Tests
.PHONY: test
test: ; $(info $(M) running test ...) @ ## run tests with coverage
$Q $(GO) test -v -cover ./... -coverprofile=coverage.out
$Q $(GO) tool cover -func=coverage.out
.PHONY: test-json
test-json: ; $(info $(M) running test output JSON ...) @ ## run tests with JSON report and coverage
$Q $(GO) test -v -cover ./... -coverprofile=coverage.out -json > test-report.out
$Q $(GO) tool cover -func=coverage.out
.PHONY: test-view
test-view: ; $(info $(M) generating coverage report ...) @ ## generate HTML coverage report
$(GO) tool cover -html=coverage.out
.PHONY: fmt
fmt: ; $(info $(M) running gofmt...) @ ## Run gofmt on all source files
$Q $(GO) fmt ./...
.PHONY: lint
lint: setup-lint; $(info $(M) running golangci-lint...) @ ## Run golangci-lint
$Q $(GOLINT) run -v -c $(LINT_CONFIG) ./...
# generate test mock for interfaces
.PHONY: mockgen
mockgen: setup-gomock ; $(info $(M) generating mocks...) @ ## Run mockery
$Q $(GO) generate ./...
# Misc
.PHONY: clean
clean: ; $(info $(M) cleaning...) @ ## Cleanup everything
@rm -rf $(BIN)
@rm -rf test/tests.* test/coverage.*
.PHONY: help
help:
@grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'
.PHONY: version
version:
@echo $(VERSION)