Skip to content

Commit c2e3c81

Browse files
authored
Add go-tools-installer to Makefile (#334)
1 parent f1503c0 commit c2e3c81

File tree

2 files changed

+162
-7
lines changed

2 files changed

+162
-7
lines changed

Makefile

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
2-
ENVTEST_K8S_VERSION = 1.33.0
3-
41
# 'sample' folder, where you mentioned the `go mod download` should take place
52
SAMPLE_DIR = sample
63

@@ -85,23 +82,43 @@ GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
8582
ADDLICENSE_VERSION ?= v1.1.1
8683
GOIMPORTS_VERSION ?= v0.36.0
8784
GOLANGCI_LINT_VERSION ?= v2.4
85+
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
86+
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
87+
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
88+
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d.%d",$$3, $$2}')
8889

8990
.PHONY: addlicense
9091
addlicense: $(ADDLICENSE) ## Download addlicense locally if necessary.
9192
$(ADDLICENSE): $(LOCALBIN)
92-
test -s $(LOCALBIN)/addlicense || GOBIN=$(LOCALBIN) go install github.com/google/addlicense@$(ADDLICENSE_VERSION)
93+
$(call go-install-tool,$(ADDLICENSE),github.com/google/addlicense,$(ADDLICENSE_VERSION))
9394

9495
.PHONY: goimports
9596
goimports: $(GOIMPORTS) ## Download goimports locally if necessary.
9697
$(GOIMPORTS): $(LOCALBIN)
97-
test -s $(LOCALBIN)/goimports || GOBIN=$(LOCALBIN) go install golang.org/x/tools/cmd/goimports@$(GOIMPORTS_VERSION)
98+
$(call go-install-tool,$(GOIMPORTS),golang.org/x/tools/cmd/goimports,$(GOIMPORTS_VERSION))
9899

99100
.PHONY: envtest
100101
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
101102
$(ENVTEST): $(LOCALBIN)
102-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
103+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
103104

104105
.PHONY: goimports
105106
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
106107
$(GOLANGCI_LINT): $(LOCALBIN)
107-
test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
108+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
109+
110+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
111+
# $1 - target path with name of binary
112+
# $2 - package url which can be installed
113+
# $3 - specific version of package
114+
define go-install-tool
115+
@[ -f "$(1)-$(3)" ] && [ "$$(readlink -- "$(1)" 2>/dev/null)" = "$(1)-$(3)" ] || { \
116+
set -e; \
117+
package=$(2)@$(3) ;\
118+
echo "Downloading $${package}" ;\
119+
rm -f $(1) ;\
120+
GOBIN=$(LOCALBIN) go install $${package} ;\
121+
mv $(1) $(1)-$(3) ;\
122+
} ;\
123+
ln -sf $$(realpath $(1)-$(3)) $(1)
124+
endef

0 commit comments

Comments
 (0)