1- GO =$(shell which go)
2- GIT =$(shell which git)
3- GOLANGCI_LINT =$(shell which golangci-lint)
4-
5- .PHONY : go-check
6- go-check :
7- $(call error-if-empty,$(GO ) ,go)
8-
9- .PHONY : git-check
10- git-check :
11- $(call error-if-empty,$(GIT ) ,git)
12-
13- .PHONY : golangci-lint-check
14- golangci-lint-check :
15- $(call error-if-empty,$(GIT ) ,git)
161
172.PHONY : go-module-version
183go-module-version : go-check git-check
194 @echo " go get $( shell $( GO) list ./pkg/app) @$( shell $( GIT) rev-parse HEAD) "
205
21- .PHONY : test
22- test : go-check
23- @$(GO ) test --race --cover ./...
24-
25- .PHONY : lint
26- lint : golangci-lint-check
27- @$(GOLANGCI_LINT ) run ./... --fix
28-
296.PHONY : examples
307examples : go-check examples-mod examples-test examples-lint
318 @echo " Running examples tests and linting"
@@ -46,12 +23,106 @@ examples-test: go-check
4623 done
4724
4825.PHONY : examples-lint
49- examples-lint : golangci-lint-check
26+ examples-lint : golangci-lint
5027 @for dir in $$(find . -mindepth 2 -name go.mod | sed -r 's/(.* ) (go.mod)/\1 /g' ); do \
5128 echo " Running linter in $$ {dir}" ; \
5229 cd $(CURDIR ) /$$ {dir} && $(GOLANGCI_LINT ) run ./... --fix && cd $(CURDIR ) ; \
5330 done
5431
32+ .PHONY : lint
33+ lint : golangci-lint # # Run linter.
34+ @$(GOLANGCI_LINT ) run --fix
35+
36+ .PHONY : test
37+ test : go-check
38+ @$(GO ) test --race --cover ./...
39+
40+ # # Run all generate-* jobs in bulk.
41+ .PHONY : generate
42+ generate : update-workflows-go-version update-workflows-golangci-lint-version
43+
44+
45+ # #@ Dependencies
46+
47+ WHOAMI ?= $(shell whoami)
48+
49+ # # Location to install dependencies to
50+ LOCALBIN ?= $(shell pwd) /bin
51+ $(LOCALBIN ) :
52+ mkdir -p $(LOCALBIN )
53+
54+ # # Tool Binaries
55+ GO =$(shell which go)
56+ GIT =$(shell which git)
57+ GOLANGCI_LINT = $(LOCALBIN ) /golangci-lint
58+ YQ = $(LOCALBIN ) /yq
59+
60+ # # TODO: remap in yaml file (version.yaml or smthng)
61+ # # Tool Versions
62+ # GO_BUILDER_VERSION must be without 'v' prefix
63+ GO_BUILDER_VERSION = 1.26.1
64+ GOLANGCI_LINT_VERSION = v2.11.4
65+ YQ_VERSION ?= v4.50.1
66+
67+
68+ .PHONY : update-workflows-go-version
69+ update-workflows-go-version : yq
70+ for file in $$ (find .github/workflows -name " *.yaml" -o -name " *.yml" ); do \
71+ if grep -q " actions/setup-go" $$ file; then \
72+ $(YQ ) -i ' (.env.GO_VERSION) = "$(GO_BUILDER_VERSION)"' $$ file; \
73+ fi ; \
74+ done
75+ echo " Updated go-version in workflow files to $( GO_BUILDER_VERSION) "
76+
77+ .PHONY : update-workflows-golangci-lint-version
78+ update-workflows-golangci-lint-version : yq
79+ for file in $$ (find .github/workflows -name " *.yaml" -o -name " *.yml" ); do \
80+ if grep -q " golangci/golangci-lint-action" $$ file; then \
81+ $(YQ ) -i ' (.env.GOLANGCI_LINT_VERSION) = "$(GOLANGCI_LINT_VERSION)"' $$ file; \
82+ fi ; \
83+ done
84+ echo " Updated golangci-lint version in workflow files to $( GOLANGCI_LINT_VERSION) "
85+
86+ # # Installed tools check
87+
88+ .PHONY : go-check
89+ go-check :
90+ $(call error-if-empty,$(GO ) ,go)
91+
92+ .PHONY : git-check
93+ git-check :
94+ $(call error-if-empty,$(GIT ) ,git)
95+
96+ # # Tool installations
97+
98+ .PHONY : golangci-lint
99+ golangci-lint : $(GOLANGCI_LINT ) # # Download golangci-lint locally if necessary.
100+ $(GOLANGCI_LINT ) : $(LOCALBIN )
101+ $(call go-install-tool,$(GOLANGCI_LINT ) ,github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION ) )
102+
103+ .PHONY : yq
104+ yq : $(YQ ) # # Download yq locally if necessary.
105+ $(YQ ) : $(LOCALBIN )
106+ $(call go-install-tool,$(YQ ) ,github.com/mikefarah/yq/v4,$(YQ_VERSION ) )
107+
108+
109+ # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
110+ # $1 - target path with name of binary
111+ # $2 - package url which can be installed
112+ # $3 - specific version of package
113+ define go-install-tool
114+ @[ -f "$(1 ) -$(3 ) " ] || { \
115+ set -e; \
116+ package=$(2 ) @$(3 ) ;\
117+ echo "Downloading $${package}" ;\
118+ rm -f $(1 ) || true ;\
119+ GOBIN=$(LOCALBIN ) GOTOOLCHAIN=$(GO_TOOLCHAIN_AUTOINSTALL_VERSION ) go install $${package} ;\
120+ mv $(1 ) $(1 ) -$(3 ) ;\
121+ } ;\
122+ ln -sf $(1 ) -$(3 ) $(1 )
123+ endef
124+
125+
55126define error-if-empty
56127@if [[ -z $(1 ) ]]; then echo "$(2 ) not installed"; false; fi
57- endef
128+ endef
0 commit comments