Skip to content

Commit eda2265

Browse files
committed
Check that go version is 1.10+ in kubebuilder init
1 parent 0ddf457 commit eda2265

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

cmd/kubebuilder/initproject/init.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"log"
2222
"os"
2323
"path/filepath"
24+
"runtime"
25+
"strconv"
26+
"strings"
2427

2528
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
2629
"github.com/spf13/cobra"
@@ -48,6 +51,11 @@ func AddInit(cmd *cobra.Command) {
4851
}
4952

5053
func runInitRepo(cmd *cobra.Command, args []string) {
54+
version := runtime.Version()
55+
if versionCmp(version, "go1.10") < 0 {
56+
log.Fatalf("The go version is %v, must be 1.10+", version)
57+
}
58+
5159
if len(domain) == 0 {
5260
log.Fatal("Must specify --domain")
5361
}
@@ -97,3 +105,29 @@ type templateArgs struct {
97105
BoilerPlate string
98106
Repo string
99107
}
108+
109+
func versionCmp(v1 string, v2 string) int {
110+
v1s := strings.Split(strings.Replace(v1, "go", "", 1), ".")
111+
v2s := strings.Split(strings.Replace(v2, "go", "", 1), ".")
112+
for i := 0; i < len(v1s) && i < len(v2s); i++ {
113+
mv1, err1 := strconv.Atoi(v1s[i])
114+
mv2, err2 := strconv.Atoi(v2s[i])
115+
if err1 == nil && err2 == nil {
116+
cmp := mv1 - mv2
117+
if cmp > 0 {
118+
return 1
119+
} else if cmp < 0 {
120+
return -1
121+
}
122+
} else {
123+
log.Fatalf("Unexpected error comparing %v with %v", v1, v2)
124+
}
125+
}
126+
if len(v1s) == len(v2s) {
127+
return 0
128+
} else if len(v1s) > len(v2s) {
129+
return 1
130+
} else {
131+
return -1
132+
}
133+
}

0 commit comments

Comments
 (0)