diff --git a/utils/meta.go b/utils/meta.go index 41c827781db14d716745b1163943f5f05c425d80..e21fcecb3082dddd8c3271e46c90e98ee1faac0f 100644 --- a/utils/meta.go +++ b/utils/meta.go @@ -2,6 +2,7 @@ package utils import ( "fmt" + "regexp" "strconv" "strings" @@ -20,11 +21,26 @@ type ServerVersion struct { // ParseVersion parses version.Info into a ServerVersion struct func ParseVersion(v *version.Info) (ret ServerVersion, err error) { - ret.Major, err = strconv.Atoi(v.Major) + re := regexp.MustCompile("[0-9]+") + + major := re.FindAllString(v.Major, 1) + if len(major) < 1 { + err = fmt.Errorf("Parse major version failed for %s", v.Major) + return + } + + ret.Major, err = strconv.Atoi(major[0]) if err != nil { return } - ret.Minor, err = strconv.Atoi(v.Minor) + + minor := re.FindAllString(v.Minor, 1) + if len(minor) < 1 { + err = fmt.Errorf("Parse minor version failed for %s", v.Minor) + return + } + + ret.Minor, err = strconv.Atoi(minor[0]) if err != nil { return } diff --git a/utils/meta_test.go b/utils/meta_test.go index 8b1f47b2d37cf36a94f348a9cf89f899abbb17b4..ea9f30bbf396d46315d3817d0f51da8506645a24 100644 --- a/utils/meta_test.go +++ b/utils/meta_test.go @@ -26,7 +26,19 @@ func TestParseVersion(t *testing.T) { expected: ServerVersion{Major: 1, Minor: 70}, }, { - input: version.Info{Major: "1", Minor: "6x"}, + input: version.Info{Major: "1", Minor: "6x"}, + expected: ServerVersion{Major: 1, Minor: 6}, + }, + { + input: version.Info{Major: "1", Minor: "6+custom7"}, + expected: ServerVersion{Major: 1, Minor: 6}, + }, + { + input: version.Info{Major: "", Minor: "6"}, + error: true, + }, + { + input: version.Info{Major: "1", Minor: ""}, error: true, }, }