Skip to content

Commit 83e9ed7

Browse files
authored
Merge pull request #4986 from afritzler/enh/go-install-tool
🌱 Add versioned tool installation via `go-install-tool` helper in Kubebuilder Makefile such as we provide for end users
2 parents 98bc8f4 + cadc3a2 commit 83e9ed7

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

Makefile

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ else
2626
GOBIN=$(shell go env GOBIN)
2727
endif
2828

29+
## Location to install dependencies to
30+
LOCALBIN ?= $(shell pwd)/bin
31+
$(LOCALBIN):
32+
mkdir -p $(LOCALBIN)
33+
2934
##@ General
3035

3136
# The help target prints out all targets with their descriptions organized
@@ -124,19 +129,17 @@ yamllint:
124129
@files=$$(find testdata -name '*.yaml' ! -path 'testdata/*/dist/*'); \
125130
docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/yamllint:latest $$files -d "{extends: relaxed, rules: {line-length: {max: 120}}}" --no-warnings
126131

127-
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
132+
.PHONY: golangci-lint
128133
golangci-lint:
129-
@[ -f $(GOLANGCI_LINT) ] || { \
130-
GOBIN=$(shell pwd)/bin go install github.com/golangci/golangci-lint/v2/cmd/[email protected] ;\
131-
}
134+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
132135

133136
.PHONY: apidiff
134137
apidiff: go-apidiff ## Run the go-apidiff to verify any API differences compared with origin/master
135-
$(GOBIN)/go-apidiff master --compare-imports --print-compatible --repo-path=.
138+
$(GO_APIDIFF) master --compare-imports --print-compatible --repo-path=.
136139

137140
.PHONY: go-apidiff
138141
go-apidiff:
139-
go install github.com/joelanford/go-apidiff@v0.6.1
142+
$(call go-install-tool,$(GO_APIDIFF),github.com/joelanford/go-apidiff,$(GO_APIDIFF_VERSION))
140143

141144
##@ Tests
142145

@@ -219,3 +222,27 @@ update-k8s-version: ## Update Kubernetes API version in version.go and .goreleas
219222
@sed -i.bak 's/KUBERNETES_VERSION=.*/KUBERNETES_VERSION=$(K8S_VERSION)/' build/.goreleaser.yml
220223
@# Clean up backup files
221224
@find . -name "*.bak" -type f -delete
225+
226+
## Tool Binaries
227+
GO_APIDIFF ?= $(LOCALBIN)/go-apidiff
228+
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
229+
230+
## Tool Versions
231+
GO_APIDIFF_VERSION ?= v0.6.1
232+
GOLANGCI_LINT_VERSION ?= v2.3.0
233+
234+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
235+
# $1 - target path with name of binary
236+
# $2 - package url which can be installed
237+
# $3 - specific version of package
238+
define go-install-tool
239+
@[ -f "$(1)-$(3)" ] || { \
240+
set -e; \
241+
package=$(2)@$(3) ;\
242+
echo "Downloading $${package}" ;\
243+
rm -f $(1) || true ;\
244+
GOBIN=$(LOCALBIN) go install $${package} ;\
245+
mv $(1) $(1)-$(3) ;\
246+
} ;\
247+
ln -sf $(1)-$(3) $(1)
248+
endef

0 commit comments

Comments
 (0)