diff --git a/version/parsed.go b/version/parsed.go index d38528b861..d571d1f71a 100644 --- a/version/parsed.go +++ b/version/parsed.go @@ -7,14 +7,16 @@ import ( ) var versionRE = regexp.MustCompile(`^(\d+)\.(\d+)\.(\d+)(.*)$`) +var fetchRest = regexp.MustCompile(`^-(\d+)-?(.*)$`) // Parsed represents a SemVer-like version information. type Parsed struct { - Major int - Minor int - Patch int - Rest string - Num int // MMmmpp + Major int + Minor int + Patch int + Rest string + Num int // MMmmpp + NumRest int } // Parse parses version information from given string. @@ -36,6 +38,13 @@ func Parse(s string) (*Parsed, error) { return nil, err } + r := fetchRest.FindStringSubmatch(res.Rest) + if len(r) != 0 { + if res.NumRest, err = strconv.Atoi(r[1]); err != nil { + return nil, err + } + } + res.Num = res.Major*10000 + res.Minor*100 + res.Patch return res, nil } diff --git a/version/parsed_test.go b/version/parsed_test.go index 5c371425ac..4af4711fc4 100644 --- a/version/parsed_test.go +++ b/version/parsed_test.go @@ -31,6 +31,9 @@ func TestParsed(t *testing.T) { }, { s: "3.0.0", p: &Parsed{Major: 3, Minor: 0, Patch: 0, Num: 30000}, + }, { + s: "4.0.0-12", + p: &Parsed{Major: 4, Minor: 0, Patch: 0, Rest: "-12", Num: 40000, NumRest: 12}, }, } for i, expected := range data { @@ -57,19 +60,19 @@ func TestParsed(t *testing.T) { }{ { s: "5.6.47-87.0-log", - p: &Parsed{Major: 5, Minor: 6, Patch: 47, Rest: "-87.0-log", Num: 50647}, + p: &Parsed{Major: 5, Minor: 6, Patch: 47, Rest: "-87.0-log", Num: 50647, NumRest: 87}, }, { s: "5.6.48-log", p: &Parsed{Major: 5, Minor: 6, Patch: 48, Rest: "-log", Num: 50648}, }, { s: "5.7.29-32-log", - p: &Parsed{Major: 5, Minor: 7, Patch: 29, Rest: "-32-log", Num: 50729}, + p: &Parsed{Major: 5, Minor: 7, Patch: 29, Rest: "-32-log", Num: 50729, NumRest: 32}, }, { s: "5.7.30-log", p: &Parsed{Major: 5, Minor: 7, Patch: 30, Rest: "-log", Num: 50730}, }, { s: "8.0.19-10", - p: &Parsed{Major: 8, Minor: 0, Patch: 19, Rest: "-10", Num: 80019}, + p: &Parsed{Major: 8, Minor: 0, Patch: 19, Rest: "-10", Num: 80019, NumRest: 10}, }, { s: "8.0.20", p: &Parsed{Major: 8, Minor: 0, Patch: 20, Rest: "", Num: 80020},