-
Notifications
You must be signed in to change notification settings - Fork 10
/
Makefile
271 lines (226 loc) · 9.48 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
.DEFAULT_GOAL := all
DOCKER_REGISTRY ?= registry.digitalocean.com/ice-io
DOCKER_TAG ?= latest-locally
GO_VERSION_MANIFEST := https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json
REQUIRED_COVERAGE_PERCENT := 0
COVERAGE_FILE := cover.out
REPOSITORY := $(shell basename `pwd`)
CGO_ENABLED ?= 1
GOOS ?=
GOARCH ?=
SERVICE_NAME ?=
SERVICES := $(wildcard ./cmd/*)
export CGO_ENABLED GOOS GOARCH SERVICE_NAME
define getLatestGoPatchVersion
$(shell curl -s $(GO_VERSION_MANIFEST) | jq -r '.[0].version')
endef
define getLatestGoMinorVersion
$(shell echo $(call getLatestGoPatchVersion) | cut -f1,2 -d'.')
endef
latestGoVersion:
@echo $(call getLatestGoPatchVersion)
latestGoMinorVersion:
@echo $(call getLatestGoMinorVersion)
updateGoModVersion:
go mod edit -go $(call getLatestGoMinorVersion)
checkModVersion: updateGoModVersion
@if git status --porcelain | grep -q go.mod; then \
echo "Outdated go version in go.mod. Please update it using 'make updateGoModVersion' and make sure everything works correctly and tests pass then commit the changes."; \
exit 1; \
fi; \
true;
updateAllDependencies:
go get -t -u ./...
go mod tidy
checkIfAllDependenciesAreUpToDate: updateAllDependencies
@if git status --porcelain | grep -q go.sum; then \
echo "Some dependencies are outdated. Please update all dependencies using 'make updateAllDependencies' and make sure everything works correctly and tests pass then commit the changes."; \
exit 1; \
fi; \
true;
generate-swagger:
swag init --parseDependency --parseInternal -d ${SERVICE} -g $(shell echo "$${SERVICE##*/}" | sed 's/-/_/g').go -o ${SERVICE}/api;
generate-swaggers:
go install github.com/swaggo/swag/cmd/swag@latest
set -xe; \
[ -d cmd ] && find ./cmd -mindepth 1 -maxdepth 1 -type d -print | grep -v 'fixture' | grep -v 'scripts' | sed 's/\.\///g' | while read service; do \
env SERVICE=$${service} $(MAKE) generate-swagger; \
done;
format-swagger:
swag fmt -d ${SERVICE} -g $(shell echo "$${SERVICE##*/}" | sed 's/-/_/g').go
format-swaggers:
set -xe; \
[ -d cmd ] && find ./cmd -mindepth 1 -maxdepth 1 -type d -print | grep -v 'fixture' | grep -v 'scripts' | sed 's/\.\///g' | while read service; do \
env SERVICE=$${service} $(MAKE) format-swagger; \
done;
generate-mocks:
# go install github.com/golang/mock/mockgen@latest
# mockgen -source=CHANGE_ME.go -destination=CHANGE_ME.go -package=CHANGE_ME
generate:
$(MAKE) generate-swaggers
$(MAKE) format-swaggers
$(MAKE) generate-mocks
$(MAKE) addLicense
$(MAKE) format-imports
checkGenerated: generate
@if git status --porcelain | grep -e [.]go -e [.]json -e [.]yaml; then \
echo "Please commit generated files, using 'make generate'."; \
git --no-pager diff; \
exit 1; \
fi; \
true;
build-all@ci/cd:
go build -tags=go_json -a -v -race ./...
build: build-all@ci/cd
binary-specific-service:
set -xe; \
echo "$@: $(SERVICE_NAME) / $(GOOS) / $(GOARCH)" ; \
go build -tags=go_json -a -v -o ./cmd/$${SERVICE_NAME}/bin ./cmd/$${SERVICE_NAME}; \
cp ./cmd/$${SERVICE_NAME}/bin ./$${SERVICE_NAME}.$${GOOS}.$${GOARCH}.bin; \
test:
set -xe; \
mf="$$(pwd)/Makefile"; \
find . -mindepth 1 -maxdepth 4 -type d -print | grep -v '\./\.' | grep -v '/\.' | sed 's/\.\///g' | while read service; do \
cd $${service} ; \
if [[ $$(find . -mindepth 1 -maxdepth 1 -type f -print | grep -E '_test.go' | wc -l | sed "s/ //g") -gt 0 ]]; then \
make -f $$mf test@ci/cd; \
fi ; \
for ((i=0;i<$$(echo "$${service}" | grep -o "/" | wc -l | sed "s/ //g");i++)); do \
cd .. ; \
done; \
cd .. ; \
done;
# TODO should be improved to a per file check and maybe against a previous value
#(maybe we should use something like SonarQube for this?)
coverage: $(COVERAGE_FILE)
@t=`go tool cover -func=$(COVERAGE_FILE) | grep total | grep -Eo '[0-9]+\.[0-9]+'`;\
echo "Total coverage: $${t}%"; \
if [ "$${t%.*}" -lt $(REQUIRED_COVERAGE_PERCENT) ]; then \
echo "ERROR: It has to be at least $(REQUIRED_COVERAGE_PERCENT)%"; \
exit 1; \
fi;
test@ci/cd:
# TODO make -race work
go test -timeout 20m -tags=go_json,test -v -cover -coverprofile=$(COVERAGE_FILE) -covermode atomic
benchmark@ci/cd:
# TODO make -race work
go test -timeout 20m -tags=go_json,test -run=^$ -v -bench=. -benchmem -benchtime 10s
benchmark:
set -xe; \
mf="$$(pwd)/Makefile"; \
find . -mindepth 1 -maxdepth 4 -type d -print | grep -v '\./\.' | grep -v '/\.' | sed 's/\.\///g' | while read service; do \
cd $${service} ; \
if [[ $$(find . -mindepth 1 -maxdepth 1 -type f -print | grep -E '_test.go' | wc -l | sed "s/ //g") -gt 0 ]]; then \
make -f $$mf benchmark@ci/cd; \
fi ; \
for ((i=0;i<$$(echo "$${service}" | grep -o "/" | wc -l | sed "s/ //g");i++)); do \
cd .. ; \
done; \
cd .. ; \
done;
print-all-packages-with-tests:
set -xe; \
find . -mindepth 1 -maxdepth 4 -type d -print | grep -v '\./\.' | grep -v '/\.' | sed 's/\.\///g' | while read service; do \
cd $${service} ; \
if [[ $$(find . -mindepth 1 -maxdepth 1 -type f -print | grep -E '_test.go' | wc -l | sed "s/ //g") -gt 0 ]]; then \
echo "$${service}"; \
fi ; \
for ((i=0;i<$$(echo "$${service}" | grep -o "/" | wc -l | sed "s/ //g");i++)); do \
cd .. ; \
done; \
cd .. ; \
done;
clean:
@go clean
@rm -f tmp$(COVERAGE_FILE) $(COVERAGE_FILE) 2>/dev/null || true
@test -d cmd && find ./cmd -mindepth 2 -maxdepth 2 -type f -name bin -exec rm -f {} \; || true;
@test -d cmd && find ./cmd -mindepth 2 -maxdepth 2 -type d -name bins -exec rm -Rf {} \; || true;
@find . -name ".tmp-*" -exec rm -Rf {} \; || true;
@find . -maxdepth 1 -name "*.bin" -exec rm -Rf {} \; || true;
@find . -mindepth 1 -maxdepth 3 -type f -name $(COVERAGE_FILE) -exec rm -Rf {} \; || true;
@find . -mindepth 1 -maxdepth 3 -type f -name tmp$(COVERAGE_FILE) -exec rm -Rf {} \; || true;
lint:
golangci-lint run
# run specific service by its name
run-%:
go run -tags=go_json -v ./cmd/$*
run:
ifeq ($(words $(SERVICES)),1)
$(MAKE) $(subst ./cmd/,run-,$(SERVICES))
else
@echo "Do not know what to run"
@echo "Targets:"
@for target in $(subst ./cmd/,run-,$(SERVICES)); do \
echo " $${target}"; \
done; false;
endif
# run specific service by its name
binary-run-%:
./cmd/$*/bin
binary-run:
ifeq ($(words $(SERVICES)),1)
$(MAKE) $(subst ./cmd/,binary-run-,$(SERVICES))
else
@echo "Do not know what to run"
@echo "Targets:"
@for target in $(subst ./cmd/,binary-run-,$(SERVICES)); do \
echo " $${target}"; \
done; false;
endif
buildAllBinaries:
set -xe; \
find ./cmd -mindepth 1 -maxdepth 1 -type d -print | grep -v 'fixture' | grep -v 'scripts' | while read service; do \
env SERVICE_NAME=$${service##*/} env GOOS=$(GOOS) env GOARCH=$(GOARCH) $(MAKE) dockerfile; \
done;
# note: it requires make-4.3+ to run that
buildMultiPlatformDockerImage:
set -xe; \
find ./cmd -mindepth 1 -maxdepth 1 -type d -print | grep -v 'fixture' | grep -v 'scripts' | while read service; do \
for arch in amd64 arm64 s390x ppc64le; do \
docker buildx build \
--platform linux/$${arch} \
-f $${service}/Dockerfile \
--label os=linux \
--label arch=$${arch} \
--force-rm \
--pull -t $(DOCKER_REGISTRY)/$(REPOSITORY)/$${service##*/}:$(DOCKER_TAG) \
--build-arg SERVICE_NAME=$${service##*/} \
--build-arg TARGETARCH=$${arch} \
--build-arg TARGETOS=linux \
.; \
done; \
done;
start-test-environment:
#go run -race -v local.go
go run -v local.go
getAddLicense:
go install -v github.com/google/addlicense@latest
addLicense: getAddLicense
`go env GOPATH`/bin/addlicense -f LICENSE.header -ignore '**/.testdata/*.html' * .github/*
checkLicense: getAddLicense
`go env GOPATH`/bin/addlicense -f LICENSE.header -check -ignore '**/.testdata/*.html' * .github/*
fix-field-alignment:
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
fieldalignment -fix ./...
download-ip2location-sample:
rm -f -R tmp
rm -f sample.bin.db24.zip
rm -f ./users/internal/device/metadata/.testdata/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN
wget https://cdn.ip2location.com/downloads/sample.bin.db24.zip
unzip sample.bin.db24.zip -d tmp
mkdir -p ./users/internal/device/metadata/.testdata/; mv ./tmp/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN ./users/internal/device/metadata/.testdata/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN
rm -f -R tmp
rm -f sample.bin.db24.zip
format-imports:
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/daixiang0/gci@latest
gci write -s standard -s default -s "prefix(github.com/ice-blockchain)" ./..
goimports -w -local github.com/ice-blockchain ./..
# examples: print-firebase-token-author, print-ice-token-admin
print-%:
go run -v local.go $(shell echo --generate$(shell echo $(firstword $(subst -, ,$*)) | awk '{print toupper(substr($$0,1,1))tolower(substr($$0,2))}')Auth) $(lastword $(subst -, ,$*))
start-seeding:
go run -v local.go --startSeeding true
all: checkLicense checkModVersion checkIfAllDependenciesAreUpToDate checkGenerated build download-ip2location-sample test coverage benchmark clean
local: addLicense updateGoModVersion updateAllDependencies generate build buildMultiPlatformDockerImage download-ip2location-sample test coverage benchmark lint clean
dockerfile: binary-specific-service