Skip to content

Commit 555f93e

Browse files
authored
feat: toolchain (#236)
1 parent d1b02db commit 555f93e

File tree

5 files changed

+105
-14
lines changed

5 files changed

+105
-14
lines changed

cmd/makefile.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ const (
1313
)
1414

1515
var (
16-
toolsGo string
17-
renovate bool
16+
toolsGo string
17+
renovate bool
18+
toolchain bool
1819
// makefileCmd represents the makefile command.
1920
makefileCmd = &cobra.Command{
2021
Use: "makefile [tools]",
@@ -25,7 +26,7 @@ var (
2526
if err != nil {
2627
return err
2728
}
28-
return makefile.Generate(client, mf, renovate, toolsGo, args...)
29+
return makefile.Generate(client, mf, renovate, toolchain, toolsGo, args...)
2930
},
3031
}
3132
)
@@ -37,6 +38,9 @@ func init() {
3738
makefileCmd.Flags().
3839
StringVar(&toolsGo, flagToolsGo, "tools.go", "The tools.go file to check for tools dependencies")
3940
makefileCmd.Flags().
40-
BoolVar(&renovate, "renovate", false, "If enables, renovate config is added to the Makefile "+
41+
BoolVar(&renovate, "renovate", false, "If enabled, renovate config is added to the Makefile "+
4142
"(renovate.json file, if existing)")
43+
makefileCmd.Flags().
44+
BoolVar(&toolchain, "toolchain", false, "If enabled, the Makefile evaluates the go version from "+
45+
"go.mod and installs the tool with this go version")
4246
}

pkg/makefile/.toolbox.mk.tpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ TB_LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || p
66
TB_LOCALBIN ?= $(TB_LOCALDIR)/bin
77
$(TB_LOCALBIN):
88
if [ ! -e $(TB_LOCALBIN) ]; then mkdir -p $(TB_LOCALBIN); fi
9+
{{- if $.Toolchain }}
10+
11+
## Go Version
12+
TB_GO_VERSION ?= $(shell grep -E '^go [0-9]+\.[0-9]+' go.mod | awk '{print $$2}')
13+
{{- end }}
914

1015
## Tool Binaries
1116
{{- range .Tools }}
@@ -29,7 +34,7 @@ TB_{{.UpperName}}_VERSION ?= {{.Version}}
2934
.PHONY: tb.{{.Name}}
3035
tb.{{.Name}}: $(TB_{{.UpperName}}) ## Download {{.Name}} locally if necessary.
3136
$(TB_{{.UpperName}}): $(TB_LOCALBIN)
32-
test -s $(TB_LOCALBIN)/{{.Name}} || GOBIN=$(TB_LOCALBIN) go install {{.ToolName}}{{- if .Version }}@$(TB_{{.UpperName}}_VERSION){{- end }}
37+
test -s $(TB_LOCALBIN)/{{.Name}} || GOBIN=$(TB_LOCALBIN) {{ if $.Toolchain }}GOTOOLCHAIN=go$(TB_GO_VERSION) {{ end }}go install {{.ToolName}}{{- if .Version }}@$(TB_{{.UpperName}}_VERSION){{- end }}
3338
{{- end }}
3439

3540
## Reset Tools

pkg/makefile/make.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ var (
2323
getRelease = github.LatestRelease
2424
)
2525

26-
func Generate(client *resty.Client, makefile string, renovate bool, toolsFile string, tools ...string) error {
26+
func Generate(client *resty.Client, makefile string, renovate, toolchain bool, toolsFile string, tools ...string) error {
2727
argTools, toolData := mergeWithToolsGo(toolsFile, unique(tools))
28-
return generateForTools(client, makefile, renovate, argTools, toolData)
28+
return generateForTools(client, makefile, renovate, toolchain, argTools, toolData)
2929
}
3030

31-
func generateForTools(client *resty.Client, makefile string, renovate bool, argTools []string, toolData []toolData) error {
31+
func generateForTools(
32+
client *resty.Client,
33+
makefile string,
34+
renovate, toolchain bool,
35+
argTools []string,
36+
toolData []toolData,
37+
) error {
3238
for _, t := range argTools {
3339
td, err := dataForArg(client, t)
3440
if err != nil {
@@ -54,6 +60,7 @@ func generateForTools(client *resty.Client, makefile string, renovate bool, argT
5460
"Tools": toolData,
5561
"WithVersions": withVersions,
5662
"Renovate": renovate,
63+
"Toolchain": toolchain,
5764
}); err != nil {
5865
return err
5966
}

pkg/makefile/make_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var _ = Describe("Make", func() {
4040
})
4141
Context("Generate", func() {
4242
It("should generateForTools a correct output", func() {
43-
err := Generate(resty.New(), makeFilePath, false, "",
43+
err := Generate(resty.New(), makeFilePath, false, false, "",
4444
"sigs.k8s.io/controller-tools/cmd/[email protected]/kubernetes-sigs/controller-tools",
4545
"github.com/golangci/golangci-lint/v2/cmd/golangci-lint",
4646
"github.com/bakito/semver",
@@ -53,7 +53,7 @@ var _ = Describe("Make", func() {
5353
})
5454
It("should migrate to include correct output", func() {
5555
makeFilePath = copyFile("Makefile.content.migrate", tempDir)
56-
err := Generate(resty.New(), makeFilePath, false, "",
56+
err := Generate(resty.New(), makeFilePath, false, false, "",
5757
"sigs.k8s.io/controller-tools/cmd/[email protected]/kubernetes-sigs/controller-tools",
5858
"github.com/golangci/golangci-lint/v2/cmd/golangci-lint",
5959
"github.com/bakito/semver",
@@ -64,8 +64,8 @@ var _ = Describe("Make", func() {
6464
Ω(readFile(makeFilePath)).Should(Equal(readFile(testDataDir, "Makefile.content.expected")))
6565
Ω(readFile(includeFilePath) + "\n").Should(Equal(readFile(testDataDir, ".toolbox.mk.content.expected")))
6666
})
67-
It("should generateForTools a correct output wit hybrid tools", func() {
68-
err := Generate(resty.New(), makeFilePath, false,
67+
It("should generateForTools a correct output with hybrid tools", func() {
68+
err := Generate(resty.New(), makeFilePath, false, false,
6969
filepath.Join(testDataDir, "tools.go.tst"),
7070
"sigs.k8s.io/controller-tools/cmd/[email protected]/kubernetes-sigs/controller-tools",
7171
"github.com/bakito/toolbox",
@@ -75,7 +75,7 @@ var _ = Describe("Make", func() {
7575
Ω(readFile(includeFilePath) + "\n").Should(Equal(readFile(testDataDir, ".toolbox.mk.hybrid.expected")))
7676
})
7777
It("should generateForTools a correct output with renovate enabled", func() {
78-
err := Generate(resty.New(), makeFilePath, true, "",
78+
err := Generate(resty.New(), makeFilePath, true, false, "",
7979
"sigs.k8s.io/controller-tools/cmd/[email protected]/kubernetes-sigs/controller-tools",
8080
"github.com/golangci/golangci-lint/v2/cmd/golangci-lint",
8181
"github.com/bakito/semver",
@@ -85,15 +85,29 @@ var _ = Describe("Make", func() {
8585
Ω(readFile(makeFilePath)).Should(Equal(readFile(testDataDir, "Makefile.content.expected")))
8686
Ω(readFile(includeFilePath) + "\n").Should(Equal(readFile(testDataDir, ".toolbox.mk.renovate.expected")))
8787
})
88+
89+
It("should generateForTools a correct output with toolchain enabled", func() {
90+
err := Generate(resty.New(), makeFilePath, false, true, "",
91+
"sigs.k8s.io/controller-tools/cmd/[email protected]/kubernetes-sigs/controller-tools",
92+
"github.com/golangci/golangci-lint/v2/cmd/golangci-lint",
93+
"github.com/bakito/semver",
94+
"github.com/bakito/toolbox",
95+
)
96+
Ω(err).ShouldNot(HaveOccurred())
97+
98+
Ω(readFile(makeFilePath)).Should(Equal(readFile(testDataDir, "Makefile.content.expected")))
99+
Ω(readFile(includeFilePath) + "\n").Should(Equal(readFile(testDataDir, ".toolbox.mk.toolchain.expected")))
100+
})
88101
})
102+
89103
Context("generateForTools", func() {
90104
It("should generateForTools a correct output", func() {
91105
td := []toolData{
92106
dataForTool(true, "sigs.k8s.io/controller-tools/cmd/controller-gen"),
93107
dataForTool(true, "github.com/bakito/semver"),
94108
dataForTool(true, "github.com/bakito/toolbox"),
95109
}
96-
err := generateForTools(resty.New(), makeFilePath, false, nil, td)
110+
err := generateForTools(resty.New(), makeFilePath, false, false, nil, td)
97111
Ω(err).ShouldNot(HaveOccurred())
98112
Ω(readFile(makeFilePath)).Should(Equal(readFile(testDataDir, "Makefile.content.expected")))
99113
Ω(readFile(includeFilePath) + "\n").Should(Equal(readFile(testDataDir, ".toolbox.mk.tools.go.expected")))
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## toolbox - start
2+
## Generated with https://github.com/bakito/toolbox
3+
4+
## Current working directory
5+
TB_LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || pwd)
6+
## Location to install dependencies to
7+
TB_LOCALBIN ?= $(TB_LOCALDIR)/bin
8+
$(TB_LOCALBIN):
9+
if [ ! -e $(TB_LOCALBIN) ]; then mkdir -p $(TB_LOCALBIN); fi
10+
11+
## Go Version
12+
TB_GO_VERSION ?= $(shell grep -E '^go [0-9]+\.[0-9]+' go.mod | awk '{print $$2}')
13+
14+
## Tool Binaries
15+
TB_CONTROLLER_GEN ?= $(TB_LOCALBIN)/controller-gen
16+
TB_GOLANGCI_LINT ?= $(TB_LOCALBIN)/golangci-lint
17+
TB_SEMVER ?= $(TB_LOCALBIN)/semver
18+
TB_TOOLBOX ?= $(TB_LOCALBIN)/toolbox
19+
20+
## Tool Versions
21+
TB_CONTROLLER_GEN_VERSION ?= v0.2.1
22+
TB_GOLANGCI_LINT_VERSION ?= v0.2.1
23+
TB_SEMVER_VERSION ?= v0.2.1
24+
TB_TOOLBOX_VERSION ?= v0.2.1
25+
26+
## Tool Installer
27+
.PHONY: tb.controller-gen
28+
tb.controller-gen: $(TB_CONTROLLER_GEN) ## Download controller-gen locally if necessary.
29+
$(TB_CONTROLLER_GEN): $(TB_LOCALBIN)
30+
test -s $(TB_LOCALBIN)/controller-gen || GOBIN=$(TB_LOCALBIN) GOTOOLCHAIN=go$(TB_GO_VERSION) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(TB_CONTROLLER_GEN_VERSION)
31+
.PHONY: tb.golangci-lint
32+
tb.golangci-lint: $(TB_GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
33+
$(TB_GOLANGCI_LINT): $(TB_LOCALBIN)
34+
test -s $(TB_LOCALBIN)/golangci-lint || GOBIN=$(TB_LOCALBIN) GOTOOLCHAIN=go$(TB_GO_VERSION) go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(TB_GOLANGCI_LINT_VERSION)
35+
.PHONY: tb.semver
36+
tb.semver: $(TB_SEMVER) ## Download semver locally if necessary.
37+
$(TB_SEMVER): $(TB_LOCALBIN)
38+
test -s $(TB_LOCALBIN)/semver || GOBIN=$(TB_LOCALBIN) GOTOOLCHAIN=go$(TB_GO_VERSION) go install github.com/bakito/semver@$(TB_SEMVER_VERSION)
39+
.PHONY: tb.toolbox
40+
tb.toolbox: $(TB_TOOLBOX) ## Download toolbox locally if necessary.
41+
$(TB_TOOLBOX): $(TB_LOCALBIN)
42+
test -s $(TB_LOCALBIN)/toolbox || GOBIN=$(TB_LOCALBIN) GOTOOLCHAIN=go$(TB_GO_VERSION) go install github.com/bakito/toolbox@$(TB_TOOLBOX_VERSION)
43+
44+
## Reset Tools
45+
.PHONY: tb.reset
46+
tb.reset:
47+
@rm -f \
48+
$(TB_LOCALBIN)/controller-gen \
49+
$(TB_LOCALBIN)/golangci-lint \
50+
$(TB_LOCALBIN)/semver \
51+
$(TB_LOCALBIN)/toolbox
52+
53+
## Update Tools
54+
.PHONY: tb.update
55+
tb.update: tb.reset
56+
toolbox makefile -f $(TB_LOCALDIR)/Makefile \
57+
sigs.k8s.io/controller-tools/cmd/[email protected]/kubernetes-sigs/controller-tools \
58+
github.com/golangci/golangci-lint/v2/cmd/golangci-lint \
59+
github.com/bakito/semver \
60+
github.com/bakito/toolbox
61+
## toolbox - end

0 commit comments

Comments
 (0)