-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile.base
1666 lines (1519 loc) · 67.8 KB
/
Makefile.base
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Manual: http://github.com/tkrop/go-make/MANUAL.md
SHELL := /bin/bash
# Setup variables for special purposes.
NULL :=
COMMA := ,
SPACE := $(NULL) $(NULL)
TAB := $(NULL) $(NULL)
# Setup colored message templates.
COLOR-info := 1;96
COLOR-debug := 1;94
COLOR-error := 1;91
COLOR-warning := 1;93
COLOR-failure := 1;91
COLOR-success := 1;92
COLOR-topic := 1;97
COLOR-cmd := 1;95
COLOR-target := 1;96
# Finding: \$\(call *[ce]*msg
msg = $(strip \033[$(COLOR-$(1))m$(2)\033[0m $(3))
cmsg = $(call msg,$(1),$(1):,$(2))
emsg = echo -e "$(call msg,$(1),$(1):,$(2))" > /dev/stderr
cerror = $(error $(shell $(call emsg,error,"$(1)")))
cdebug = $(shell if [[ "$${MAKEFLAGS}" =~ --(trace|debug=.*) ]]; then \
$(call emsg,debug,$(1)); fi)
# Log execution environment for debugging.
$(call cdebug,$(shell uname --all))
# Check whether bash version is higher or equal version 3.2 to be compatible.
ifneq ($(shell echo $${BASH_VERSION} | grep -E -v "^([4-9]+|3\.2)"),)
$(call cerror,bash version incompatible [$${BASH_VERSION}])
endif
# Check whether bash is running in minmal compatibility mode for testing.
ifdef BASH_COMPAT
$(shell $(call emsg,info,bash compatibility mode set [$(BASH_COMPAT)]))
endif
# Setup default makeflags create consistent behavior.
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --no-print-directory
MAKEFLAGS += --not-keep-going
MAKEFLAGS := $(sort $(MAKEFLAGS))
# Setup default directories of projects.
DIR_RUN := $(CURDIR)/run
DIR_CRED := $(DIR_RUN)/creds
DIR_BUILD := $(CURDIR)/build
ifneq ("$(wildcard Makefile.vars)","")
include Makefile.vars
endif
# Setup default go compiler environment.
export GO ?= go
export GOPATH ?= $(shell $(GO) env GOPATH)
export GOBIN ?= $(GOPATH)/bin
# Export private repositories that do not need to be downloaded.
export GOPRIVATE ?= github.bus.zalan.do
# Setup go to use desired and consistent go versions.
GOVERSION := $(shell $(GO) version $$(which $(GO)) | sed -E "s/.*go([0-9.]+).*/\1/")
# Function for checking and wanring about go and package version mismatches.
go-check-version = \
if [ -f "$(GOBIN)/$(1)" ]; then \
GOVERSION="$$($(GO) version -v "$(GOBIN)/$(1)" | sed "s/.*: go//")"; \
if ! [[ "$${GOVERSION}" =~ ^$(GOVERSION)* ]]; then \
CHECK="$${GOVERSION} => $(GOVERSION)"; \
$(call emsg,warning,$(1) build [$${CHECK}]: \
run 'make update-$(1)' or install matching compiler version); \
fi; \
MODULE="$(call go-pkg,$(TOOLS_GO),origin,^$(1)$$,,$(2))"; \
if [ "$${MODULE\#\#*@}" != "latest" ]; then \
MVERSION="$$($(GO) version -m "$(GOBIN)/$(1)" | \
grep -E "^\s*(mod|dep)\s*$${MODULE%%@*}" | cut -f 4)"; \
if [ "$${MVERSION}" != "$${MODULE\#\#*@}" ]; then \
CHECK="$${MVERSION} => $${MODULE\#\#*@}"; \
$(call emsg,warning,$(1) version [$${CHECK}]: \
run 'make update-$(1)' to sync with project version); \
fi; \
fi; \
fi
# Function for determining the actual version of a go package from downloading.
VERSION_REGEX := 's/.*"Path":\s*"([^"]*)".*"Version":\s*"([^"]*)".*/\1@\2/g'
go-download-version = \
$(GO) mod download --json $(1) 2>/dev/null | \
sed --regexp-extended --null-data $(VERSION_REGEX)
# Function to ensure compatibility of gawk and mawk.
ifneq ($(shell echo "false" | \
awk '{ print gensub("false","true","g") }' 2>/dev/null),true)
awk-gensub := function gensub(r, s, h, t) { \
if (t == "") { t = $$0 } \
if (h ~ "^[gG]") { gsub(r, s, t) } \
else if (int(h) != 0) { \
for (i = 1; i <= int(h); i++) { \
if (match(t, r)) { \
t = substr(t, 1, RSTART-1) s substr(t, RSTART+RLENGTH) \
} \
} \
} \
return t \
}
else
awk-gensub :=
endif
# Function for go-package handling (TODO: check spliting and simplifying).
go-pkg = $(shell awk -v mode="$(2)" -v filter="$(3)" \
-v not="$(4)" -v version="$(5)" ' $(awk-gensub) \
BEGIN { FS = "[/@]"; RS = "[ \n\r]" } \
function package(vindex) { \
if (vindex) { \
return substr($$0, 1, vindex - 1) \
} else { return $$0 } \
} { \
field = NF; \
if (vindex = index($$0, "@")) { field-- } \
if ($$(field) ~ "v[0-9]+") { field-- } \
if (!filter || \
(not && ($$(field) !~ filter)) || \
(!not && ($$(field) ~ filter))) { \
if (mode == "command") { \
if (!($$(field) in map)) { \
print $$(field); map[$$(field)]++ \
} \
} else if (mode == "module") { \
if (!((pkg = package(vindex)) in map)) { \
if (version ~ "^[0-9.]+(-[0-9]{14}-[0-9a-f]{12})?$$") { \
print pkg ((version == "latest") ? "@latest" : "@v" version) \
} else { \
print ((vindex) ? $$0 : $$0 "@latest") \
} map[pkg]++ \
} \
} else if (mode == "origin") { \
if (!((pkg = package(vindex)) in map)) { \
print $$1 "/" $$2 "/" $$3 "@" \
((vindex) ? substr($$0, vindex + 1) : "latest"); \
map[pkg]++ \
} \
} else if (mode == "package") { \
if (!((pkg = package(vindex)) in map)) { \
print pkg; map[pkg]++ \
} \
} \
} \
}' <<<"$(1)")
# Setup go-make to use desired build and config scripts.
GOMAKE_DEP := github.com/tkrop/[email protected]
GOMAKE_MAKEFILE := $(realpath $(firstword $(MAKEFILE_LIST)))
GOMAKE_CONFIG := $(patsubst %/,%,$(dir $(GOMAKE_MAKEFILE)))
# Setup default tool command versions to be installed.
TOOLS_GO += \
github.com/golangci/golangci-lint/cmd/[email protected] \
github.com/zalando/zally/cli/zally \
golang.org/x/vuln/cmd/govulncheck \
github.com/uudashr/gocognit/cmd/gocognit \
github.com/fzipp/gocyclo/cmd/gocyclo \
github.com/mgechev/[email protected] \
github.com/securego/gosec/v2/cmd/gosec \
github.com/tsenart/deadcode \
honnef.co/go/tools/cmd/staticcheck \
github.com/zricethezav/gitleaks/v8 \
github.com/icholy/gomajor \
github.com/golang/mock/mockgen \
github.com/tkrop/go-testing/cmd/mock \
$(GOMAKE_DEP)
TOOLS_SH += \
github.com/anchore/syft \
github.com/anchore/grype
TOOLS_NPM += \
markdownlint-cli
NVM_BIN ?=
# Check for the actually used go-make version.
$(shell $(call go-check-version,go-make,))
# Setup default browser command to open coverage reports.
BROWSER ?= xdg-open
# Setup default verions used for building, releasing, and publishing.
VERSION ?= snapshot
ifeq ($(wildcard VERSION),VERSION)
BUILD_VERSION := v$(shell cat VERSION)
else
BUILD_VERSION ?= $(VERSION)
endif
IMAGE_VERSION ?= $(VERSION)
# Setup container image command (supports: docker or podman).
IMAGE_CMD ?= $(shell command -v docker || command -v podman || docker)
# Setup default container image name and host.
ifdef IMAGE_NAME
ifneq ($(words $(subst /, ,$(IMAGE_NAME))),3)
$(call cerror,invalid image name [$(IMAGE_NAME)] \
- image name format is repo/team/name!)
endif
IMAGE_HOST ?= $(word 1,$(subst /, ,$(IMAGE_NAME)))
IMAGE_TEAM ?= $(word 2,$(subst /, ,$(IMAGE_NAME)))
IMAGE_ARTIFACT ?= $(word 3,$(subst /, ,$(IMAGE_NAME)))
else
IMAGE_HOST ?= pierone.stups.zalan.do
IMAGE_TEAM ?= $(TEAM)
IMAGE_ARTIFACT ?= $(GITREPONAME)
endif
IMAGE ?= $(IMAGE_HOST)/$(IMAGE_TEAM)/$(IMAGE_ARTIFACT):$(IMAGE_VERSION)
# Setup sensible defaults for workflow configuration variables.
IMAGE_PUSH ?= pulls
CODE_QUALITY ?= base
TEST_TIMEOUT ?= 10s
# Ensure that we can find default config files.
ifeq ($(wildcard $(GOPATH)/pkg/mod/$(GOMAKE_DEP)/config/),)
$(shell $(GO) install $(GOMAKE_DEP) >/dev/null 2>&1)
endif
# Function to search for default config files.
search-default = $(firstword $(wildcard $(1)) $(wildcard $(GOMAKE_CONFIG)/$(1) \
$(wildcard $(GOPATH)/pkg/mod/$(GOMAKE_DEP)/config/$(1))))
# Setup default config files to be used.
FILE_MAKE ?= $(call search-default,Makefile)
FILE_VARS ?= $(call search-default,Makefile.vars)
FILE_GOLANGCI ?= $(call search-default,.golangci.yaml)
FILE_MARKDOWN ?= $(call search-default,.markdownlint.yaml)
FILE_GITLEAKS ?= $(call search-default,.gitleaks.toml)
FILE_REVIVE ?= $(call search-default,revive.toml)
FILE_CODACY ?= $(call search-default,.codacy.yaml)
FILE_CONTAINER ?= Dockerfile
# Setup custom delivery files to be scanned for updating go versions.
DELIVERY := $(strip $(wildcard delivery*.yaml) $(wildcard .github/workflows/*.yaml))
DELIVERY_REGEX ?= (cdp-runtime\/go-|go-version: \^?)[0-9.]*
GOMOD_REGEX ?= ^(go )[0-9.]*$$
# Setup default git command and repository information.
GIT ?= git
GITHOOKS ?= pre-commit # commit-msg
GITAUTHOR ?= $$(echo -n "$$(git config --get user.name) <$$(git config --get user.email)>")
GITREPO ?= $(shell $(GIT) remote get-url origin 2>/dev/null | \
sed "s/^https:\/\///; s/^git@//; s/.git$$//; s/:/\//")
GITHOSTNAME ?= $(word 1,$(subst /, ,$(GITREPO)))
GITORGNAME ?= $(word 2,$(subst /, ,$(GITREPO)))
GITREPONAME ?= $(word 3,$(subst /, ,$(GITREPO)))
TEAM ?= $(shell cat .zappr.yaml | grep "X-Zalando-Team" | \
sed "s/.*:[[:space:]]*\([a-z-]*\).*/\1/")
# Setup default database connection.
DB_HOST ?= 127.0.0.1
DB_PORT ?= 5432
DB_NAME ?= db
DB_USER ?= user
DB_PASSWORD ?= pass
DB_VERSION ?= latest
DB_IMAGE ?= postgres:$(DB_VERSION)
# Setup default aws startup settings.
AWS_SERVICES ?= sqs s3
AWS_VERSION ?= latest
AWS_IMAGE ?= localstack/localstack:$(AWS_VERSION)
# Setup codacy integration.
CODACY ?= enabled
ifeq ($(IMAGE_VERSION),snapshot)
CODACY_CONTINUE ?= false
else
CODACY_CONTINUE ?= true
endif
CODACY_PROVIDER ?= ghe
CODACY_USER ?= $(GITORGNAME)
CODACY_PROJECT ?= $(GITREPONAME)
CODACY_API_BASE_URL ?= https://codacy.bus.zalan.do
CODACY_CLIENTS ?= aligncheck deadcode
CODACY_BINARIES ?= gosec staticcheck
ifeq (CODACY_API_BASE_URL,$(CODACY_API_BASE_URL))
CODACY_REPORTER_VERSION ?= 13.12.3
endif
CODACY_GOSEC_VERSION ?= 0.4.5
CODACY_STATICCHECK_VERSION ?= 3.0.12
CODACY_PROJECT_TOKEN ?=
CODACY_API_TOKEN ?=
# Function for conversion of variables name.
upper = $(shell echo "$(1)" | tr '[:lower:]' '[:upper:]')
# Default target list for all and pipeline builds.
TARGETS_ALL ?= init test lint build image
TARGETS_INIT ?= init-hooks init-go $(if $(filter $(CODACY),enabled),init-codacy,)
TARGETS_CLEAN ?= clean-build
TARGETS_UPDATE ?= update-go update-deps update-make
TARGETS_COMMIT ?= test-go test-unit lint-leaks? lint-$(CODE_QUALITY) lint-markdown
TARGETS_TEST ?= test-all $(if $(filter $(CODACY),enabled),test-upload,)
TARGETS_LINT ?= lint-leaks? lint-$(CODE_QUALITY) lint-markdown lint-apis \
$(if $(filter $(CODACY),enabled),lint-codacy,)
INIT_MAKE ?= $(notdir $(FILE_MAKE) $(FILE_VARS) $(FILE_MARKDOWN) \
$(FILE_CODACY) $(FILE_REVIVE))
UPDATE_MAKE ?= $(notdir $(FILE_MAKE) $(FILE_GOLANGCI) $(FILE_MARKDOWN) \
$(FILE_GITLEAKS) $(FILE_CODACY) $(FILE_REVIVE))
# Stub function for general setup in run-targets (default: true).
run-setup ?= true
# Stub function for common variables in run-targets (called with one arg).
run-vars ?=
# Stub function for local variables in run-targets (called with one arg).
run-vars-local ?=
# Stub function for container specific variables in run-targets (called with one arg).
run-vars-image ?=
# Stub function to set up aws localstack run-target (default: true).
run-setup-aws ?= true
# General setup of tokens for run-targets.
run-token-create = \
ztoken > $(DIR_CRED)/token; echo "Bearer" > $(DIR_CRED)/type
run-token-link = \
test -n "$(1)" && test -n "$(2)" && test -n "$(3)" && ( \
test -h "$(DIR_CRED)/$(1)-$(2)" || ln -s type "$(DIR_CRED)/$(1)-$(2)" && \
test -h "$(DIR_CRED)/$(1)-$(3)" || ln -s token "$(DIR_CRED)/$(1)-$(3)" \
) || test -n "$(1)" && test -n "$(2)" && test -z "$(3)" && ( \
test -h "$(DIR_CRED)/$(1)" || ln -s type "$(DIR_CRED)/$(1)" && \
test -h "$(DIR_CRED)/$(2)" || ln -s token "$(DIR_CRED)/$(2)" \
) || test -n "$(1)" && test -z "$(2)" && test -z "$(3)" && ( \
test -h "$(DIR_CRED)/$(1)" || ln -s token "$(DIR_CRED)/$(1)" \
) || true
# Setup default environment variables.
FIND := find . ! -path "./run/*" ! -path "./build/*"
FILTER := sed "s|^./||" | grep -E "^[a-z0-9/_-]*\.go$$"
SOURCES := $(shell $(FIND) -name "*.go" ! -name "mock_*_test.go" | $(FILTER))
MODULES := $(shell echo $(SOURCES) | xargs -r dirname | sort --unique)
COMMANDS := $(shell $(FIND) -name "main.go" | xargs -r readlink -f | \
sed -E "s/^.*\/([^/]*)\/main.go$$/\1/" | sort --unique)
COMMANDS_PKG := $(shell $(FIND) -name "main.go" | xargs -r readlink -f | \
sed -E "s/^(.*\/([^/]*))\/main.go$$/\2=\1/; s|$(CURDIR)|.|")
COMMANDS_REGEX := $(shell echo "^$(COMMANDS)$$" | tr ' ' '|' )
COMMANDS_GO := $(sort $(call go-pkg,$(TOOLS_GO),command,$(COMMANDS_REGEX),not,))
COMMANDS_SH := $(sort $(call go-pkg,$(TOOLS_SH),command,$(COMMANDS_REGEX),not,))
# Setup of runtime arguments for commands that support arguments.
CMDWORDS ?= call show git- test- lint run- version- update
CMDARGS ?= $(shell awk '{ split("$(CMDWORDS)",seps); \
for (i = 1; i <= NF; i++) { if (found) { \
line = ((line) ? line " " $$i : $$i); continue \
} \
for (p in seps) { if ($$i ~ "^"seps[p] ) { found = 1; } } } \
} END { print line }' <<<"$(MAKECMDGOALS)")
ifdef CMDARGS
ARGS := $(strip $(CMDARGS) $(ARGS))
$(shell if [ -n "$(ARGS)" ]; then \
$(call emsg,info,captured arguments [$(ARGS)]); \
fi)
$(eval $(subst :,\:,$(CMDARGS))::;@:)
endif
# Setup optimized golang generate mocks execution.
MOCKS := $(shell if [ -n "$(SOURCES)" ]; then \
grep "//go:generate[[:space:]]*mockgen" $(SOURCES) | \
awk '{ \
source = "_"; target = "_"; package = "_"; \
origin = substr($$1, 0, index($$1, "://go:generate") - 1); \
path = substr(origin, 0, match(origin, "/[^/]*$$")); \
for (i = 1; i <= NF; i++) { \
if ($$i ~ "^-source=") { \
cmd = "readlink -f \"" path "/" substr($$i, 9) "\""; \
cmd | getline source; close(cmd) \
} else if ($$i ~ "^-destination=") { \
cmd = "readlink -f \"" path "/" substr($$i, 14) "\""; \
cmd | getline target; close(cmd) \
} \
} \
if (source == "_") { package = $$(NF-1) } \
print target "|" source "|" package "|" origin \
}' | sort --unique; fi)
MOCKS_TARGETS := $(shell echo "$(MOCKS)" | \
awk 'BEGIN {FS="|"; RS=" "} ($$1 != "_") { print $$1 }' | sort --unique)
MOCKS_SOURCES := $(shell echo "$(MOCKS)" | \
awk 'BEGIN {FS="|"; RS=" "} ($$2 != "_") { print $$2 }' | sort --unique)
MOCKS_ORIGINS := $(shell echo "$(MOCKS)" | \
awk 'BEGIN {FS="|"; RS=" "} ($$4 != "_") { print $$4 }' | sort --unique)
# Prepare target lists used to define rules and preconditions.
TARGETS_PUBLISH := $(addprefix version-publish-, all $(COMMANDS))
TARGETS_TEST_INIT := test-clean init-sources init-hooks
TARGETS_INIT_MAKE := $(addprefix init/,$(INIT_MAKE))
TARGETS_INIT_ALL := $(addprefix init/,$(sort $(INIT_MAKE) $(UPDATE_MAKE)))
TARGETS_INIT_CODACY := $(addprefix init-, $(CODACY_BINARIES))
TARGETS_LINT_CODACY_CLIENTS := $(addprefix lint-, $(CODACY_CLIENTS))
TARGETS_LINT_CODACY_BINARIES := $(addprefix lint-, $(CODACY_BINARIES))
TARGETS_IMAGE := $(shell $(FIND) -type f -name "$(FILE_CONTAINER)*" | sed 's|^./||')
TARGETS_IMAGE_BUILD := $(addprefix image-build/, $(TARGETS_IMAGE))
TARGETS_IMAGE_PUSH := $(addprefix image-push/, $(TARGETS_IMAGE))
TARGETS_BUILD := $(addprefix build-, $(COMMANDS))
TARGETS_BUILD_LINUX := $(addprefix $(DIR_BUILD)/linux/, $(COMMANDS))
TARGETS_INSTALL := $(addprefix install-, $(COMMANDS))
TARGETS_INSTALL_GO := $(addprefix install-, $(COMMANDS_GO))
TARGETS_INSTALL_SH := $(addprefix install-, $(COMMANDS_SH))
TARGETS_INSTALL_NPM := $(addprefix install-, $(TOOLS_NPM:-cli=))
TARGETS_INSTALL_ALL := $(TARGETS_INSTALL) $(TARGETS_INSTALL_GO) \
$(TARGETS_INSTALL_SH) $(TARGETS_INSTALL_NPM)
TARGETS_UNINSTALL := $(addprefix uninstall-, $(COMMANDS))
TARGETS_UNINSTALL_GO := $(addprefix uninstall-, $(COMMANDS_GO))
TARGETS_UNINSTALL_SH := $(addprefix uninstall-, $(COMMANDS_SH))
TARGETS_UNINSTALL_NPM := $(addprefix uninstall-, $(TOOLS_NPM:-cli=))
TARGETS_UNINSTALL_CODACY := $(addprefix uninstall-codacy-, $(CODACY_BINARIES))
TARGETS_UNINSTALL_ALL := $(TARGETS_UNINSTALL) $(TARGETS_UNINSTALL_GO) \
$(TARGETS_UNINSTALL_SH) $(TARGETS_UNINSTALL_NPM) $(TARGETS_UNINSTALL_CODACY)
TARGETS_RUN := $(addprefix run-, $(COMMANDS))
TARGETS_RUN_GO := $(addprefix run-go-, $(COMMANDS))
TARGETS_RUN_IMAGE := $(addprefix run-image-, $(COMMANDS))
TARGETS_RUN_CLEAN := $(addprefix run-clean-, $(COMMANDS) db aws)
TARGETS_CLEAN_ALL := clean-init $(TARGETS_CLEAN) clean-run $(TARGETS_UNINSTALL_ALL)
TARGETS_CLEAN_RUN := $(addprefix clean-run-, $(COMMANDS) db aws)
TARGETS_UPDATE_GO := $(addprefix update-,$(sort $(COMMANDS_GO) go-make))
TARGETS_UPDATE_MAKE := $(addprefix update/,$(UPDATE_MAKE))
TARGETS_UPDATE_MAKE? := $(addsuffix ?,$(TARGETS_UPDATE_MAKE))
TARGETS_UPDATE_ALL := update-go update-deps update-tools update-make
TARGETS_UPDATE_ALL? := update-go? update-deps? update-make?
TARGETS_UPDATE? := $(filter $(addsuffix ?,$(TARGETS_UPDATE)), \
$(TARGETS_UPDATE_ALL?) $(TARGETS_UPDATE_MAKE?))
## Standard: default targets to test, lint, and build.
#@ executes the default targets.
all:: $(TARGETS_ALL)
#@ executes the default targets after cleaning up.
all-clean:: clean clean-run all
#@ executes the pre-commit check targets.
commit:: $(TARGETS_COMMIT)
#@ show short cut for show-help.
help:: show-help
##@ executes the given arguments as commands (for testing).
call::; @$(ARGS)
$(DIR_BUILD) $(DIR_RUN) $(DIR_CRED) $(GOBIN):
@if [ ! -d "$@" ]; then mkdir -p $@; fi;
## Make-Support: targets to support user understanding.
show-help = awk ' \
BEGIN { \
printf("\n$(call msg,topic,Usage:,) $(call msg,cmd,(go-)make, \
) $(call msg,target,<target>,)\n") \
} \
/^\#\# .*/ { \
if (i = index($$0, ":")) { \
printf("\n$(call msg,topic,%s,)%s\n", \
substr($$0, 4, i - 4), substr($$0, i)) \
} else { \
printf("\n$(call msg,topic,%s,)\n", substr($$0, 4)) \
} next \
} \
/^\#@ / { \
if (i = index($$0, ":")) { \
printf(" $(call msg,target,%-25s,) %s\n", \
substr($$0, 4, i - 4), substr($$0, i + 2)) \
} else { line = substr($$0, 4) } next \
} \
/^[a-zA-Z_0-9?-]+:/ { \
if (line) { \
target = substr($$1, 1, length($$1) - 1); \
if (i = index(line, " \# ")) { \
target = target " " substr(line, 1, i - 1); \
line = substr(line, i + 3) \
} \
printf(" $(call msg,target,%-25s,) %s\n", target, line); \
line = "" \
} \
}'
show-vars = awk ' \
($$0 ~ "^[[:alnum:]_-]* [?+]=") { \
if (comment) { print comment } print $$0; var = 1; next \
} \
(var && $$0 ~ "^([\t]|$$)") { \
print $$0; next \
} \
($$0 ~ "^\#") { \
comment = $$0; var = 0; next \
} { \
comment = ""; var = 0; next \
}'
show-targets = awk -v RS= -F: ' \
/(^|\n)\# Files(\n|$$)/,/(^|\n)\# Finished Make data base/ { \
if ($$1 !~ "^[\#.]") { print $$1 } \
}' | LC_ALL=C sort --unique | \
egrep --invert-match --regexp='^[^[:alnum:]]'
#@ shows this short extracted help about main target families.
show-help::
@cat $(MAKEFILE_LIST) | $(show-help) || true;
#@ shows all supported (customizable) variable (with defaults).
show-vars:
@cat $(firstword $(MAKEFILE_LIST)) | $(show-vars) || true;
#@ shows all supported targets (without prerequesites).
show-targets:
@$(MAKE) --no-builtin-rules --no-builtin-variables --print-data-base \
--question --makefile=$(firstword $(MAKEFILE_LIST)) 2>/dev/null | \
$(show-targets) || true;
#@ shows the raw base makefile as implemented.
show-raw:
@cat $(firstword $(MAKEFILE_LIST)) || true;
#@ shows the effective makefile as evaluated by make.
show-make:
@$(MAKE) --no-builtin-rules --no-builtin-variables --print-data-base \
--question --makefile=$(firstword $(MAKEFILE_LIST)) 2>/dev/null || true;
## Git-Support: targets for standard git commands (experimental).
GITFIX ?= $(GIT) commit --amend --no-edit
GITPUSH ?= $(GIT) push --force-with-lease
GITBRANCH ?= $(GIT) branch --show-current
GITPRUNE ?=$(GIT) fetch --all --prune --verbose
GITLOGFMT ?= $(GIT) log --max-count=1 --pretty=format
# https://github.com/pvdlg/conventional-changelog-metahub#commit-types
COMMIT_CONVENTION ?= feat deprecate remove docs fix style refactor \
perf test build ci chore
git-message = \
if [ -z "$${MESSAGE}" ]; then MESSAGE="chore: update deps"; fi; \
if ! [[ "$${MESSAGE}" =~ ^[^:]+:.*$$ ]]; then MESSAGE="$(1): $${MESSAGE}"; fi; \
if ! [[ "$${MESSAGE}" =~ ^.*\([^()]*\#[0-9]+\)$$ ]]; then \
COMMIT="$$($(GITLOGFMT):"%s" | \
sed -E 's/^.*\(?([^() ]*\#[0-9]+)\)?.*$$/\1/')"; \
NUMBER="$${COMMIT\#\#*\#}"; CONTEXT="$${COMMIT%%\#*}"; \
MESSAGE="$${MESSAGE} ($${CONTEXT}\#$$((NUMBER+$(2))))"; \
fi
git-main = \
$$($(GIT) remote show origin 2>/dev/null | grep "HEAD branch:" | cut -d ' ' -f 5)
git-clean = \
$(GITPRUNE); HISTORY=($$($(GIT) log --pretty=format:"%T" "$${MAIN}")); \
for BRANCH in $$($(GIT) branch | cut -c 3- | grep -vw "$(1)"); do \
TIP="$$($(GITLOGFMT):"%T" "$${BRANCH}")"; \
if [[ "$${HISTORY[@]}" =~ $${TIP} ]]; then \
$(GIT) branch --delete --force "$${BRANCH}"; \
elif [ "$(ARGS)" == "all" ]; then \
$(GIT) branch --delete "$${BRANCH}"; \
else $(call emsg,info,keeping branch [$${BRANCH}]); \
fi; \
done; $(GITPRUNE)
git-verify = awk -v mode="$(1)" -v author="$(GITAUTHOR)" ' $(awk-gensub) \
BEGIN { \
re_type_suffix = "(\\[[a-z]+\\])?!?: "; \
re_type_missing = "^([a-z]+)" re_type_suffix; \
re_type_invalid = "^(" gensub(" ", "|", "g", \
"$(COMMIT_CONVENTION)") ")" re_type_suffix; \
re_issue = "([a-z]+/[a-z]+)?\#[0-9]+"; \
re_issue_list = "\\(" re_issue "((,|\\) *\\()" re_issue ")*\\)"; \
re_issue_missing = re_issue_list "$$"; \
re_issue_invalid = "^[^(]*" re_issue_list "$$"; \
signed_by = "Signed-off-by:"; \
re_signed_missing = signed_by " +[^\\n]*$$"; \
re_signed_author = "^" signed_by " +" author "$$"; \
} \
function verify() { \
msg = gensub("((:?\\n)\#[^\\n]*|\\n)*$$", "", "g", msg); \
title = (i = index(msg,"\n")) ? substr(msg, 0, i - 1) : msg; \
if (title !~ re_type_missing) { \
printf("$(call cmsg,error,commit type missing \
[title=%s])\n", title); errors++ \
} else if (title !~ re_type_invalid) { \
printf("$(call cmsg,error,commit type invalid \
[title=%s])\n", title); errors++ \
} \
if (title !~ re_issue_missing) { \
printf("$(call cmsg,error,issue missing \
[titel=%s])\n", title); errors++ \
} else if (title !~ re_issue_invalid) { \
printf("$(call cmsg,error,issue invalid \
[titel=%s])\n", title); errors++ \
} \
sign = (i = index(msg, signed_by)) ? substr(msg, i) : ""; \
if (sign !~ re_signed_missing) { \
printf("$(call cmsg,error,signed-off-by missing \
[msg=%s])\n", msg); errors++ \
} else if (sign !~ "^" re_signed_missing) { \
printf("$(call cmsg,error,signed-off-by by to many \
[sign=%s])\n", sign); errors++ \
} else if (mode == "message" && sign !~ re_signed_author) { \
printf("$(call cmsg,error,signed-off-by not the author \
[sign=%s; author=%s])\n", sign, author); errors++ \
} msg = "" \
} \
(mode == "message") { \
if (msg != "") { \
msg = msg "\n" $$0 \
} else { msg = $$0 } next \
} \
(mode != "message" && $$0 ~ "^ ") { \
if (msg != "") { \
msg = msg "\n" substr($$0, 5) \
} else { msg = substr($$0, 5) } next \
} \
(mode != "message") { if (msg) { verify(); } } \
END { \
if (mode == "message" || msg) { verify(); } if (errors) { \
printf("$(call cmsg,failure,git-verify $(ARGS) \
[errors=%d])\n", errors); exit 1 \
} \
printf("$(call cmsg,success,git-verify $(ARGS) \
[errors=%d])\n", errors) \
}'
GITFORMAT ?= "%Cred%h %t %Cgreen%ad%Creset | %s %C(bold blue)[%an]%Creset%C(yellow)%d%Creset"
#@ prints the git log as in pretty short format.
git-log::
@$(GIT) log --graph --date=iso --pretty=format:$(GITFORMAT);
#@ prints the git log as graph in pretty short format.
git-graph::
@$(GIT) log --graph --date=iso --pretty=format:$(GITFORMAT) --all;
#@ [all] # cleans up the git history removing merged branches except the current.
git-clean::
@BRANCH="$$($(GITBRANCH))"; MAIN="$(git-main)"; \
$(call git-clean,$${MAIN}\|$${BRANCH});
#@ [all] # checks out default branch and cleans up the git history removing merged branches.
git-reset::
@BRANCH="$$($(GITBRANCH))"; MAIN="$(git-main)"; \
if [ "$${BRANCH}" != "$${MAIN}" ]; then \
$(GIT) checkout "$${MAIN}" && $(GIT) pull || ( \
$(GIT) stash && $(GIT) checkout "$${MAIN}" && \
$(GIT) pull && $(GIT) stash apply \
) \
fi || exit 1; $(call git-clean,$${MAIN});
#@ <mode> [msg|log-file] # checks whether git log follows the commit conventions.
git-verify::
@MODE="$(firstword $(ARGS))"; case "$${MODE}" in \
( "log" ) cat "$(wordlist 2,$(words $(ARGS)),$(ARGS))";; \
( "message" ) cat "$(wordlist 2,$(words $(ARGS)),$(ARGS))";; \
( "branch" ) $(GIT) log --no-merges --format=raw "$$($(GITBRANCH))";; \
( "pull" | * ) MAIN="$(git-main)" && $(GIT) fetch origin "$${MAIN}" --verbose && \
$(GIT) log --no-merges --format=raw "$$($(GITBRANCH))" "^origin/$${MAIN}";; \
esac | $(call git-verify,$${MODE}) > /dev/stderr
#@ <branch> <message> # creates a branch with the current change set using next issue.
git-create:: git-create-feat
# TODO: check whether I'm on the default branch and on remote HEAD.
$(addprefix git-create-,$(COMMIT_CONVENTION)):: git-create-%:
@BRANCH="$(firstword $(ARGS))"; \
MESSAGE="$(wordlist 2,$(words $(ARGS)),$(ARGS))"; \
if [ -z "$${BRANCH}" ]; then BRANCH="update-deps"; fi; \
$(call git-message,$*,1); \
$(GIT) checkout -B "$${BRANCH}" && \
ARGS="" $(GIT) commit --signoff --message "$${MESSAGE}" && \
$(GIT) push --set-upstream origin "$${BRANCH}" || \
$(GIT) checkout $${BRANCH}; exit;
#@ <message> # commits the current change set using the given message with current issue.
git-commit:: git-commit-feat
$(addprefix git-commit-,$(COMMIT_CONVENTION)):: git-commit-%:
@MESSAGE="$(ARGS)"; $(call git-message,$*,0); \
ARGS="" $(GIT) commit --signoff --message "$${MESSAGE}"; exit;
#@ [(no-)edit|(no-)verify] # pushes the latest changes to the previous commit of the origin.
git-fix::
@GITFIX=($(patsubst %,"%",$(GITFIX))); GITPUSH=($(patsubst %,"%",$(GITPUSH))); \
if [[ "$(ARGS)" =~ verify ]]; then GITFIX=("$${GITFIX[@]/--no-verify}"); fi; \
if [[ "$(ARGS)" =~ no-verify ]]; then GITFIX+=("--no-verify"); fi; \
if [[ "$(ARGS)" =~ edit ]]; then GITFIX=("$${GITFIX[@]/--no-edit}"); fi; \
if [[ "$(ARGS)" =~ no-edit ]]; then GITFIX+=("--no-edit"); fi; \
for KEY in "$${!GITFIX[@]}"; do \
if [ "$${GITFIX[$${KEY}]}" == "" ]; then unset GITFIX[$${KEY}]; fi; \
done; \
echo "$${GITFIX[@]} && $${GITPUSH[@]}"; \
ARGS="" "$${GITFIX[@]}" && $${GITPUSH[@]}; exit;
#@ pushes the current branch to the same branch of the origin.
git-push::
@$(GIT) push --set-upstream origin \
$$($(GIT) branch 2> /dev/null | sed -e '/^[^*]/d; s/* \(.*\)/\1/');
## Init: targets to initialize tools and (re-)sources.
#@ initialize the project to prepare it for building.
init:: $(TARGETS_INIT)
#@ initialize the go module and package dependencies.
init-go:: go.sum
go.sum: go.mod
$(GO) mod tidy && touch go.sum && $(GO) mod download;
go.mod:
$(GO) mod init "$(GITREPO)";
#@ initialize git pre-commit and commit-message hooks.
init-hooks:: $(GITHOOKS:%=.git/hooks/%)
git-hooks-commit-msg = echo -ne '\#!/bin/sh\n\n\
echo "make git-verify message $${1}" > /dev/stderr;\n\
command -v $(GOBIN)/go-make >/dev/null || \\\n\
GOBIN=$(GOBIN) $(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) && \\\n\
$(GOBIN)/go-make git-verify message $${1};\n' | sed 's/^ *//g'
git-hooks-pre-commit = echo -ne '\#!/bin/sh\n\n\
echo "make commit" > /dev/stderr;\n\
command -v $(GOBIN)/go-make >/dev/null || \\\n\
GOBIN=$(GOBIN) $(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) && \\\n\
$(GOBIN)/go-make commit;\n' | sed 's/^ *//g'
.git/hooks/commit-msg: $(MAKEFILE_LIST)
@if ! diff <(cat $@) <($(git-hooks-commit-msg)) >/dev/null; then \
$(git-hooks-commit-msg) >$@; chmod 755 $@; \
fi;
.git/hooks/pre-commit: $(MAKEFILE_LIST)
@if ! diff <(cat $@) <($(git-hooks-pre-commit)) >/dev/null; then \
$(git-hooks-pre-commit) >$@; chmod 755 $@; \
fi;
#@ initialize the generated sources.
init-sources:: $(MOCKS_TARGETS)
$(MOCKS_TARGETS):: go.sum $(MOCKS_SOURCES) install-mockgen install-mock
@MOCK=($(subst |,$(SPACE),$(firstword $(filter $@|%,$(MOCKS))))); \
TARGET="$${MOCK[0]}"; SOURCE="$${MOCK[1]}"; \
PACKAGE="$${MOCK[2]}"; ORIGIN="$${MOCK[3]}"; \
if [ ! -f "$${TARGET}" ]; then \
echo "$(GO) generate \"$${ORIGIN}\""; $(GO) generate "$${ORIGIN}"; \
elif [ "$${SOURCE}" != "_" -a "$${TARGET}" -ot "$${SOURCE}" ]; then \
echo "$(GO) generate \"$${ORIGIN}\""; $(GO) generate "$${ORIGIN}"; \
elif [ "$${PACKAGE}" != "_" -a "$${TARGET}" -ot "go.sum" ]; then \
echo "$(GO) generate \"$${ORIGIN}\""; $(GO) generate "$${ORIGIN}"; \
fi; \
#@ initialize the codacy support - if enabled.
init-codacy:: $(TARGETS_INIT_CODACY)
$(TARGETS_INIT_CODACY):: init-%:
@VERSION="$(CODACY_$(call upper,$*)_VERSION)"; \
FILE="$(GOBIN)/codacy-$*-$${VERSION}"; \
if [ ! -f $${FILE} ]; then \
BASE="https://github.com/codacy/codacy-$*/releases/download"; \
echo curl --silent --location --output $${FILE} \
$${BASE}/$${VERSION}/codacy-$*-$${VERSION}; \
curl --silent --location --output $${FILE} \
$${BASE}/$${VERSION}/codacy-$*-$${VERSION}; \
chmod 700 $${FILE}; \
fi; \
#@ <version> # initialize project by copying config files from template.
init-make:: $(TARGETS_INIT_MAKE)
$(TARGETS_INIT_ALL):: init/%:
@DIR="$${PWD}"; FILE="$*"; #FILE="$${FILE##$${DIR}/}"; \
if [ ! -f "$${DIR}/$${FILE}" -o "$(ARGS)" == "current" ]; then \
echo "install: $${FILE} (installed)"; \
cp "$(GOMAKE_CONFIG)/$${FILE}" "$${DIR}/$${FILE}"; \
else \
$(call emsg,warning,install: $${FILE} (blocked)); \
fi; \
#@ <version> # initialize project by copying Makefile.base from template.
init-make!::
cp "$(GOMAKE_CONFIG)/Makefile.base" "Makefile"; \
## Test: targets to test the source code (and compiler environment).
TEST_ALL := $(DIR_BUILD)/test-all.cover
TEST_UNIT := $(DIR_BUILD)/test-unit.cover
TEST_BENCH := $(DIR_BUILD)/test-bench.cover
#@ execute default test set.
test:: $(TARGETS_TEST)
#@ <pkg>[/<test>] # execute all tests.
test-all:: $(TARGETS_TEST_INIT) $(TEST_ALL)
#@ <pkg>[/<test>] # execute only unit tests.
test-unit:: $(TARGETS_TEST_INIT) $(TEST_UNIT)
#@ <pkg>[/<test>] # execute benchmarks.
test-bench:: $(TARGETS_TEST_INIT) $(TEST_BENCH)
# remove all coverage files of test and benchmarks.
test-clean::
@if [ -f "$(TEST_ALL)" ]; then rm -vf $(TEST_ALL); fi; \
if [ -f "$(TEST_UNIT)" ]; then rm -vf $(TEST_UNIT); fi; \
if [ -f "$(TEST_BENCH)" ]; then rm -vf $(TEST_BENCH); fi; \
#@ start the test coverage report.
test-cover::
@FILE=$$(ls -Art "$(TEST_ALL)" "$(TEST_UNIT)" \
"$(TEST_BENCH)" 2>/dev/null); \
$(GO) tool cover -html="$${FILE}"; \
#@ upload the test coverage report to codacy.
test-upload::
@FILE=$$(ls -Art "$(TEST_ALL)" "$(TEST_UNIT)" \
"$(TEST_BENCH)" 2>/dev/null); \
COMMIT="$$($(GITLOGFMT):"%H")"; \
SCRIPT="https://coverage.codacy.com/get.sh"; \
if [ -n "$(CODACY_PROJECT_TOKEN)" ]; then \
bash <(curl --silent --location $${SCRIPT}) report \
--project-token $(CODACY_PROJECT_TOKEN) --commit-uuid $${COMMIT} \
--codacy-api-base-url $(CODACY_API_BASE_URL) --language go \
--force-coverage-parser go --coverage-reports "$${FILE}"; \
elif [ -n "$(CODACY_API_TOKEN)" ]; then \
bash <(curl --silent --location $${SCRIPT}) report \
--organization-provider $(CODACY_PROVIDER) \
--username $(CODACY_USER) --project-name $(CODACY_PROJECT) \
--api-token $(CODACY_API_TOKEN) --commit-uuid $${COMMIT} \
--codacy-api-base-url $(CODACY_API_BASE_URL) --language go \
--force-coverage-parser go --coverage-reports "$${FILE}"; \
fi; \
# Function to test versions.
test-go = \
for VERSION in $(2); do \
if ! [[ "$(GOVERSION)" =~ ^$${VERSION}[0-9.]* ]]; then \
if [[ "$(GOVERSION)" < "$${VERSION}" ]]; then \
GOCHANGE="upgrade"; CHANGE="downgrade"; \
else \
GOCHANGE="downgrade"; CHANGE="upgrade"; \
fi; \
$(call emsg,error,go version [$(1): $${VERSION} => $(GOVERSION)]: \
run 'make update-go $${VERSION}' to adjust the project or \
$${GOCHANGE} your local go compiler version to $${VERSION}!); \
fi; \
done
#@ test whether project is using the latest go-version.
test-go::
@if [ ! -f go.mod ]; then \
$(call test-go,go.mod,$$(grep "^go [0-9.]*$$" go.mod | cut -f2 -d' ')); \
fi; \
for FILE in $(DELIVERY); do \
if [ -f "$${FILE}" ]; then \
$(call test-go,$${FILE},$$(grep -Eo "$(DELIVERY_REGEX)" "$${FILE}" | \
grep -Eo "[0-9.]*" | sort --unique)); \
fi; \
done; if [ -n "$${GOCHANGE}" ]; then exit 1; fi
# process test arguments.
test-args = \
if [ -n "$(1)" ]; then ARGS=($(1)); \
if [[ "$${ARGS[0]}" =~ ^scope:([^\\s]*)$$ ]]; then \
if [ "$${BASH_REMATCH[1]}" != "all" ]; then \
echo "-coverpgk=./$${ARGS[0]\#*:}"; \
else echo "-coverpkg=./..."; fi; \
ARGS=($${ARGS[@]:1}); \
fi; \
if [[ -f "$${ARGS[0]}" && ! -d "$${ARGS[0]}" && \
"$${ARGS[0]}" == *_test.go ]]; then \
find $$(dirname "$${ARGS[@]}" | sort --unique) \
-maxdepth 1 -a -name "*.go" -a ! -name "*_test.go" \
-o -name "common_test.go" -o -name "mock_*_test.go" | \
sed "s|^|./|"; \
echo $${ARGS[@]/\#/./}; \
elif [[ -d "$${ARGS[0]}" && ! -f "$${ARGS[0]}" ]]; then \
echo $${ARGS[@]/\#/./}; \
elif [[ ! -f "$${ARGS[0]}" && ! -d "$${ARGS[0]}" ]]; then \
for ARG in $${ARGS[0]}; do \
if [ -z "$${PACKAGES}" ]; then PACKAGES="$${ARG%/*}"; \
else PACKAGES="$${PACKAGES}\n$${ARG%/*}"; fi; \
if [ -z "$${TEST_CASE}" ]; then TEST_CASES="-run $${ARG\#\#*/}"; \
else TEST_CASES="$${TEST_CASES} -run $${ARG\#\#*/}"; fi; \
done; \
echo -en "$${PACKAGES}" | sort --unique | sed "s|^|./|"; \
echo "$${TEST_CASES}"; \
else \
$(call emsg,warning,invalid test parameters [$${ARGS[@]}]); \
fi; \
else echo "./..."; fi
TESTFLAGS ?=
TEST_FLAGS ?= $(strip -mod=readonly -count=1 -race $(TESTFLAGS))
TEST_ARGS ?= $(shell $(call test-args,$(ARGS)))
TEST_DEPS ?=
# actuall targets for testing.
$(TEST_ALL):: $(SOURCES) init-sources $(TEST_DEPS) $(DIR_BUILD)
$(GO) test $(TEST_FLAGS) -timeout $(TEST_TIMEOUT) \
-cover -coverprofile $@ $(TEST_ARGS);
$(TEST_UNIT):: $(SOURCES) init-sources $(DIR_BUILD)
$(GO) test $(TEST_FLAGS) -timeout $(TEST_TIMEOUT) \
-cover -coverprofile $@ -short $(TEST_ARGS);
$(TEST_BENCH):: $(SOURCES) init-sources $(DIR_BUILD)
$(GO) test $(TEST_FLAGS) -benchtime=$(TEST_TIMEOUT) \
-cover -coverprofile $@ -short -bench=. $(TEST_ARGS);
#@ execute a kind of self-test running the main targets.
test-self:: update-all? update-all clean-all all-clean install-all
@$(call emsg,success,test-self finished successfully!);
# disabled (deprecated): deadcode golint interfacer ifshort maligned musttag
# nosnakecase rowserrcheck scopelint structcheck varcheck wastedassign
# diabled (distructive): nlreturn ireturn nonamedreturns varnamelen exhaustruct
# exhaustivestruct gochecknoglobals gochecknoinits tagliatelle
# disabled (conflicting): godox gci paralleltest
# not listed (unnecessary): forcetypeassert wsl
# requires unknown setup: depguard
LINTERS_CUSTOM ?=
LINTERS_DISABLED ?=
LINTERS_DISCOURAGED ?= \
deadcode exhaustivestruct exhaustruct gci gochecknoglobals gochecknoinits \
godox golint ifshort interfacer ireturn maligned musttag nlreturn \
nonamedreturns nosnakecase paralleltest rowserrcheck scopelint structcheck \
tagliatelle varcheck varnamelen wastedassign
LINTERS_MINIMUM ?= \
asasalint asciicheck bidichk bodyclose dogsled dupl dupword durationcheck \
errchkjson execinquery exportloopref funlen gocognit goconst gocyclo \
godot gofmt gofumpt goimports gosimple govet importas ineffassign maintidx \
makezero misspell nestif nilerr prealloc predeclared promlinter reassign \
sqlclosecheck staticcheck typecheck unconvert unparam unused \
usestdlibvars whitespace
LINTERS_BASELINE ?= \
containedctx contextcheck cyclop decorder errname forbidigo ginkgolinter \
gocheckcompilerdirectives goheader gomodguard goprintffuncname gosec \
grouper interfacebloat lll loggercheck nakedret nilnil noctx nolintlint \
nosprintfhostport
LINTERS_EXPERT ?= \
errcheck errorlint exhaustive gocritic goerr113 gomnd gomoddirectives \
revive stylecheck tenv testableexamples testpackage thelper tparallel \
wrapcheck
LINT_FILTER := $(LINTERS_CUSTOM) $(LINTERS_DISABLED)
LINT_DISABLED ?= $(subst $(SPACE),$(COMMA),$(strip \
$(filter-out $(LINT_FILTER),$(LINTERS_DISCOURAGED)) $(LINTERS_DISABLED)))
LINT_MINIMUM ?= $(subst $(SPACE),$(COMMA),$(strip \
$(filter-out $(LINT_FILTER),$(LINTERS_MINIMUM)) $(LINTERS_CUSTOM)))
LINT_BASELINE ?= $(subst $(SPACE),$(COMMA),$(strip $(LINT_MINIMUM) \
$(filter-out $(LINT_FILTER),$(LINTERS_BASELINE))))
LINT_EXPERT ?= $(subst $(SPACE),$(COMMA),$(strip $(LINT_BASELINE) \
$(filter-out $(LINT_FILTER),$(LINTERS_EXPERT))))
ifeq ($(shell ls $(FILE_GOLANGCI) 2>/dev/null), $(FILE_GOLANGCI))
LINT_CONFIG := --config $(FILE_GOLANGCI)
endif
LINT_MIN := --enable $(LINT_MINIMUM) --disable $(LINT_DISABLED)
LINT_BASE := --enable $(LINT_BASELINE) --disable $(LINT_DISABLED)
LINT_PLUS := --enable $(LINT_EXPERT) --disable $(LINT_DISABLED)
LINT_MAX := --enable-all --disable $(LINT_DISABLED)
LINT_ALL := --enable $(LINT_EXPERT),$(LINT_DISABLED) --disable-all
LINT_FLAGS ?= --allow-serial-runners --sort-results --color always
LINT_CMD ?= $(GOBIN)/golangci-lint run $(LINT_CONFIG) $(LINT_FLAGS)
LINT_CMD_CONFIG := $(MAKE) lint-config
ifeq ($(ARGS),linters)
LINT_CMD := $(GOBIN)/golangci-lint linters $(LINT_CONFIG) $(LINT_FLAGS)
else ifeq ($(ARGS),config)
LINT_CMD := @
LINT_MIN := LINT_ENABLED=$(LINT_MINIMUM) \
LINT_DISABLED=$(LINT_DISABLED) $(LINT_CMD_CONFIG)
LINT_BASE := LINT_ENABLED=$(LINT_BASELINE) \
LINT_DISABLED=$(LINT_DISABLED) $(LINT_CMD_CONFIG)
LINT_PLUS := LINT_ENABLED=$(LINT_EXPERT) \
LINT_DISABLED=$(LINT_DISABLED) $(LINT_CMD_CONFIG)
LINT_MAX := LINT_DISABLED=$(LINT_DISABLED) $(LINT_CMD_CONFIG)
LINT_ALL := LINT_ENABLED=$(LINT_EXPERT),$(LINT_DISABLED) $(LINT_CMD_CONFIG)
else ifeq ($(ARGS),fix)
LINT_CMD := $(GOBIN)/golangci-lint run $(LINT_CONFIG) $(LINT_FLAGS) --fix
else ifneq ($(ARGS),)
LINT_CMD := $(GOBIN)/golangci-lint run $(LINT_CONFIG) $(LINT_FLAGS)
LINT_MIN := --disable-all --enable $(ARGS)
LINT_BASE := --disable-all --enable $(ARGS)
LINT_PLUS := --disable-all --enable $(ARGS)
LINT_MAX := --disable-all --enable $(ARGS)
LINT_ALL := --disable-all --enable $(ARGS)
endif
## Lint: targets to lint source code.
#@ <fix|linter> # execute all linters for the custom code quality level.
lint:: $(TARGETS_LINT)
#@ <fix|linter> # execute golangci linters for minimal code quality level (go-files).
lint-min:: init-sources install-golangci-lint; $(LINT_CMD) $(LINT_MIN)
#@ <fix|linter> # execute golangci linters for base code quality level (go-files).
lint-base:: init-sources install-golangci-lint; $(LINT_CMD) $(LINT_BASE)
#@ <fix|linter> # execute golangci linters for plus code quality level (go-files).
lint-plus:: init-sources install-golangci-lint; $(LINT_CMD) $(LINT_PLUS)
#@ <fix|linter> # execute golangci linters for maximal code quality level (go-files).
lint-max:: init-sources install-golangci-lint; $(LINT_CMD) $(LINT_MAX)
#@ <fix|linter> # execute all golangci linters for insane code quality level (go-files).
lint-all:: init-sources install-golangci-lint; $(LINT_CMD) $(LINT_ALL)
#@ create a golangci linter config using the custom code quality level.
lint-config::
@LINT_START=$$(awk '($$0 ~ "^linters:.*"){print NR-1}' $(FILE_GOLANGCI)); \
LINT_STOP=$$(awk '(start && $$0 ~ "^[^ ]+:$$"){print NR; start=0} \
($$0 ~ "^linters:.*"){start=1}' $(FILE_GOLANGCI)); \
(sed -ne '1,'$${LINT_START}'p' $(FILE_GOLANGCI); \
echo -e "linters:\n enable:"; \
for LINTER in "# List of min-set linters (min)" $(LINTERS_MINIMUM) \
"# List of base-set linters (base)" $(LINTERS_BASELINE) \
"# List of plus-set linters (plus)" $(LINTERS_EXPERT); do \
if [ "$${LINTER:0:1}" == "#" ]; then X=""; else X="- "; fi; \
echo -e " $${X}$${LINTER}"; \
done; echo -e "\n disable:"; \
for LINTER in "# List of to-avoid linters (avoid)" $(LINTERS_DISABLED); do \
if [ "$${LINTER:0:1}" == "#" ]; then X=""; else X="- "; fi; \
echo -e " $${X}$${LINTER}"; \
done; \
echo; sed -ne $${LINT_STOP}',$$p' $(FILE_GOLANGCI)) | \
awk -v enabled=$${LINT_ENABLED} -v disabled=$${LINT_DISABLED} ' $(awk-gensub) \
((start == 2) && ($$1 == "-")) { \
enabled = enable[$$2]; disabled = disable[$$2]; \
if (enabled && disabled) { \
print $$0 " # (conflicting)" \
} else if (!enabled && disabled) { \
print gensub("-","# -", 1) " # (disabled)" \
} else if (!enabled && !disabled && enone) { \
print gensub("-","# -", 1) " # (missing)" \