-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
135 lines (103 loc) · 4.93 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
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: init
init:
$(MAKE) up
$(MAKE) build
.PHONY: up
up:
docker-compose up -d \
.PHONY: test
test:
go test -v -mod=readonly -cover ./...
.PHONY: lint
lint:: \
lint-rules \
golangci-lint
.PHONY: lint-fix
lint-fix:: \
lint-rules \
golangci-lint-fix
GOLANGCI_LINT_VERSION=v1.31.0
GOLANGCI_LINT_DIR=$(shell go env GOPATH)/pkg/golangci-lint/$(GOLANGCI_LINT_VERSION)
GOLANGCI_LINT_BIN=$(GOLANGCI_LINT_DIR)/golangci-lint
$(GOLANGCI_LINT_BIN):
curl -vfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOLANGCI_LINT_DIR) $(GOLANGCI_LINT_VERSION)
.PHONY: install-golangci-lint
install-golangci-lint: $(GOLANGCI_LINT_BIN)
.PHONY: golangci-lint
golangci-lint: install-golangci-lint
$(GOLANGCI_LINT_BIN) -v run
.PHONY: golangci-lint-fix
golangci-lint-fix: install-golangci-lint
$(GOLANGCI_LINT_BIN) -v run --fix
.PHONY: lint-rules
lint-rules:: ensure-command-pcregrep
# Disallowed files.
find . -name ".DS_Store" | xargs -n 1 -I {} sh -c 'echo {} && false'
# Don't use upper case letter in file and directory name.
# The convention for separator in name is:
# - file: "_"
# - directory in "/cmd": "-"
# - other directory: shouldn't be separated
! find . -name "*.go" | pcregrep "[[:upper:]]"
# Don't use more than 2 block of packages in an import statement.
! pcregrep -rnM --include=".+\.go$$" "import \(\n([\t \w\"/.-]+\n)+(\n([\t \w\"/.-]+\n)+){2,}\)" .
# Don't use more than 1 import statement.
! pcregrep -rnM --include=".+\.go$$" "((.*\n)+import){2,}" .
# Don't export type/function/variable/constant in main package/test.
! pcregrep -rnM --include=".+\.go$$" --exclude=".+_test\.go$$" "^package main\n(.*\n)*(type|func|var|const) [[:upper:]]" .
! pcregrep -rnM --include=".+\.go$$" --exclude=".+_test\.go$$" "^package main\n(.*\n)*(var|const) \(\n((\t.*)?\n)*\t[[:upper:]]" .
! pcregrep -rnM --include=".+_test\.go$$" "^(type|var|const) [[:upper:]]" .
! pcregrep -rnM --include=".+_test\.go$$" "^(var|const) \(\n((\t.*)?\n)*\t[[:upper:]]" .
# ! pcregrep -rnM --include=".+_test\.go$$" "^func [[:upper:]]" . | pcregrep -v ":func (Test|Benchmark).*\((t|b) \*testing\.(T|B)\) {"
# Write meaningful comments instead of "...".
! pcregrep -rn --include=".+\.go$$" "\/\/.*\.\.\.$$" .
# Don't add a space after "//nolint:"
! pcregrep -rnF --include=".+\.go$$" "//nolint: " .
# Don't declare a var block inside a function.
! pcregrep -rn --include=".+\.go$$" "^\t+var \($$" .
# Call golang-libraries/panichandler.Recover() with defer.
! pcregrep -rnF --include=".+\.go$$" "panichandler.Recover()" . | pcregrep -vF "defer"
# Don't use context.TODO().
! pcregrep -rnF --include=".+\.go$$" "context.TODO()" .
# For nil error do `t.Fatal("no error")` instead of using testutils.
! pcregrep -rnM --include=".+_test\.go$$" "if err == nil \{\n.+testutils\." .
# Use httptest.NewRequest() instead of http.NewRequest() in tests.
! pcregrep -rnF --include=".+_test\.go$$" --exclude-dir="^httpclientrequest$$" "http.NewRequest(" .
# Don't use http.DefaultServeMux directly or indirectly.
! pcregrep -rn --include=".+\.go$$" --exclude-dir="^debugutils$$" "http\.(Handle\(|HandleFunc\(|DefaultServeMux)" .
# Use httpclientrequest instead of http.DefaultClient|Get()|Head()|Post()|PostForm().
! pcregrep -rn --include=".+\.go$$" --exclude-dir="^httpclientrequest$$" "http\.(DefaultClient|Get\(|Head\(|Post\(|PostForm\()" . | grep -vF "TODO"
# Don't use JSON "omitempty" with a `time.Time` because it has no zero value. Convert it to a pointer or remove omitempty.
! pcregrep -rn --include=".+\.go$$" "\stime.Time\s+\`.*json:\".*omitempty.*\`" .
# Use mgobsonutils.ParseObjectIdHex() instead of bson.ObjectIdHex().
! pcregrep -rnF --include=".+\.go$$" --exclude-dir="^mgobsonutils$$" "bson.ObjectIdHex" .
# Don't mention "mgo" in projects migrated to the official MongoDB driver.
(pcregrep -rn --include=".+\.go$$" "(github\.com\/globalsign\/mgo|github\.com\/DTSL\/golang-libraries\/mgo)" . > /dev/null) || (! pcregrep -rni --include=".+\.go$$" "mgo" .)
# Use MongoDB options constructors and setters instead of the struct.
! pcregrep -rn --include=".+\.go$$" "&options\.\w+\{" .
# Use constructors from golang-libraries/redisutils.
! pcregrep -rn --include=".+\.go$$" --exclude-dir="^redis(test|utils)$$" "redis\.New(Client|FailoverClient|ClusterClient|FailoverClusterClient|Ring|SentinelClient|UniversalClient)\(" .
# Use jsonlog.NewErrorOptionalFile().
! pcregrep -rn --include=".+\.go$$" "jsonlog\.(New|NewFile|NewOptionalFile)\(" .
.PHONY: mod-update
mod-update:
go get -v -u -d all
$(MAKE) mod-tidy
.PHONY: mod-tidy
mod-tidy:
rm -f go.sum
go mod tidy -v
.PHONY: clean
clean::
go clean -cache -testcache
.PHONY: ensure-command-pcregrep
ensure-command-pcregrep:
$(call ENSURE_COMMAND,pcregrep)
.PHONY: mysql-shell
mysql-shell:
docker-compose exec mysql mysql -uroot -proot
GO_BUILD_DIR=build
.PHONY: build
build::
mkdir -p $(GO_BUILD_DIR)
go build -v -mod=readonly -ldflags="-s -w -X main.version=$(VERSION)" -o $(GO_BUILD_DIR) ./cmd/...