Skip to content

Commit

Permalink
Check that go version is 1.10+ in kubebuilder init
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Apr 9, 2018
1 parent 0ddf457 commit eda2265
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd/kubebuilder/initproject/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

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

func runInitRepo(cmd *cobra.Command, args []string) {
version := runtime.Version()
if versionCmp(version, "go1.10") < 0 {
log.Fatalf("The go version is %v, must be 1.10+", version)
}

if len(domain) == 0 {
log.Fatal("Must specify --domain")
}
Expand Down Expand Up @@ -97,3 +105,29 @@ type templateArgs struct {
BoilerPlate string
Repo string
}

func versionCmp(v1 string, v2 string) int {
v1s := strings.Split(strings.Replace(v1, "go", "", 1), ".")
v2s := strings.Split(strings.Replace(v2, "go", "", 1), ".")
for i := 0; i < len(v1s) && i < len(v2s); i++ {
mv1, err1 := strconv.Atoi(v1s[i])
mv2, err2 := strconv.Atoi(v2s[i])
if err1 == nil && err2 == nil {
cmp := mv1 - mv2
if cmp > 0 {
return 1
} else if cmp < 0 {
return -1
}
} else {
log.Fatalf("Unexpected error comparing %v with %v", v1, v2)
}
}
if len(v1s) == len(v2s) {
return 0
} else if len(v1s) > len(v2s) {
return 1
} else {
return -1
}
}

0 comments on commit eda2265

Please sign in to comment.