-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile.common
121 lines (104 loc) · 3.25 KB
/
Makefile.common
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
# Version for go mod tidy -compat flag.
GO_MOD_COMPAT_VERSION := 1.20
# Configure golang module proxy URI.
GOPROXY ?= proxy.golang.org
# Version of linter.
# - https://github.com/golangci/golangci-lint/releases/tag/v1.53.3
# - https://github.com/mgechev/revive/tree/v1.3.2
GOLANGCI_LINT_VERSION := $(or $(GOLANGCI_LINT_VERSION),v1.53.3)
GOLANGCI_LINT_VERSION_FULL := $(subst v,golangci-lint has version ,$(GOLANGCI_LINT_VERSION))
# Set common utilities environs.
ENV_BIN := $(or $(ENV_BIN),env)
GIT_BIN := $(or $(GIT_BIN),git)
GO_BIN := $(or $(GO_BIN),go)
GREP_BIN := $(or $(GREP_BIN),grep)
ID_BIN := $(or $(ID_BIN),id)
MKDIR_BIN := $(or $(MKDIR_BIN),mkdir)
PWD_BIN := $(or $(PWD_BIN),pwd)
SH_BIN := $(or $(SH_BIN),sh)
TEST_BIN := $(or $(TEST_BIN),test)
WGET_BIN := $(or $(WGET_BIN),wget)
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set).
ifeq (,$(shell $(GO_BIN) env GOBIN))
GOBIN = $(shell $(GO_BIN) env GOPATH)/bin
else
GOBIN = $(shell $(GO_BIN) env GOBIN)
endif
# Build flags.
GO_BUILD_ENV = CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=$(GOPROXY)
GO_BUILD_FLAGS ?= --tags "netgo osusergo static_build"
LDFLAGS = -s -w -extldflags "-static"
# Set golangci-lint binary path.
GOLANGCI_LINT_BIN=$(GOBIN)/golangci-lint
# Download golangci-lint if needed.
.PHONY: golangci-lint
golangci-lint:
ifeq ("$(wildcard $(GOLANGCI_LINT_BIN))","")
$(WGET_BIN) -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
$(SH_BIN) -s -- -b $(GOBIN) $(GOLANGCI_LINT_VERSION)
else
$(GOLANGCI_LINT_BIN) --version | \
$(GREP_BIN) -qE '^$(GOLANGCI_LINT_VERSION_FULL)' || \
$(WGET_BIN) -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
$(SH_BIN) -s -- -b $(GOBIN) $(GOLANGCI_LINT_VERSION)
endif
# Print environment configuration.
.PHONY: env-info
env-info:
@echo
@echo \# Current user information:
@$(ID_BIN)
@echo
@echo \# Current working directory:
@$(PWD_BIN)
@echo
@echo \# Golang related environment variables:
@$(GO_BIN) env
@echo
@echo \# Compiler version:
@$(GO_BIN) version
@echo
@echo \# Git status:
@$(GIT_BIN) status -s
@echo
# Run linter.
.PHONY: lint
lint: golangci-lint
$(GOLANGCI_LINT_BIN) -v run --sort-results ./...
# Run linter with fix flag.
.PHONY: lint-n-fix
lint-n-fix: golangci-lint
$(GOLANGCI_LINT_BIN) -v run --fix --sort-results ./...
# Run tests.
.PHONY: test
test:
$(GO_BIN) test -v -failfast ./...
# Tidy golang dependencies.
.PHONY: tidy
tidy:
$(GO_BIN) mod tidy -v -compat=$(GO_MOD_COMPAT_VERSION)
# Create artifacts directory.
.PHONY: artifacts-dir
artifacts-dir:
$(MKDIR_BIN) -vp "$(ARTIFACTS_DIR)"
# Clean cache.
.PHONY: clean
clean:
$(GO_BIN) clean -cache -testcache -x
# Build application.
.PHONY: build
build: artifacts-dir env-info
$(ENV_BIN) $(GO_BUILD_ENV) $(GO_BIN) build $(GO_BUILD_FLAGS) -ldflags='$(LDFLAGS)' -a -o '$(ARTIFACTS_DIR)/$(BIN_NAME)'
@echo
# Fail when directory tree is dirty.
.PHONY: check-git-clean
check-git-clean:
@status=$$($(GIT_BIN) status --porcelain=v1); \
if [ ! -z "$${status}" ]; then \
echo "Error: working directory tree is dirty."; \
exit 1; \
fi
# Show git diff helper (with excludes).
.PHONY: diff
diff:
$(GIT_BIN) diff --diff-algorithm=minimal --ignore-all-space -- ":(exclude)vendor/*" ":(exclude)go.sum"