Skip to content

Commit 10cb14a

Browse files
cmd/gh-downloader: Use a regexp to do version parsing
1 parent 50637d4 commit 10cb14a

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

cmd/gh-downloader/tag.go

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,67 @@
11
package main
22

33
import (
4-
"fmt"
4+
"regexp"
5+
"strconv"
56
"strings"
67
)
78

9+
var versionRe = regexp.MustCompile(`((\w+)-)?v?(\d|x).(\d|x).(\d|x)(-(\w+))?`)
10+
811
type tag struct {
912
Project string
1013
Major int
1114
Minor int
1215
Patch int
16+
Suffix string
1317
}
1418

1519
func newTag(name string) *tag {
16-
var (
17-
t tag
20+
var ms = versionRe.FindStringSubmatch(name)
1821

19-
version = name
20-
)
22+
if len(ms) != 8 {
23+
return nil
24+
}
2125

22-
if splittedTagName := strings.Split(name, "-"); len(splittedTagName) > 1 {
23-
t.Project = strings.Join(splittedTagName[:len(splittedTagName)-1], "-")
24-
version = splittedTagName[len(splittedTagName)-1]
26+
return &tag{
27+
Project: ms[2],
28+
Major: parseVersionNumber(ms[3]),
29+
Minor: parseVersionNumber(ms[4]),
30+
Patch: parseVersionNumber(ms[5]),
31+
Suffix: ms[7],
2532
}
33+
}
2634

27-
if n, err := fmt.Sscanf(
28-
strings.TrimPrefix(version, "v"),
29-
"%d.%d.%d",
30-
&t.Major,
31-
&t.Minor,
32-
&t.Patch,
33-
); n != 3 || err != nil {
34-
return nil
35+
func parseVersionNumber(sv string) int {
36+
if v, err := strconv.Atoi(sv); err == nil {
37+
return v
3538
}
3639

37-
return &t
40+
return -1
3841
}
3942

4043
func (t1 *tag) Less(t2 *tag) bool {
4144
if t1.Project != t2.Project {
42-
return false
45+
return strings.Compare(t1.Project, t2.Project) < 0
46+
}
47+
48+
if t1.Major != t2.Major {
49+
return t1.Major < t2.Major
50+
}
51+
52+
if t1.Minor != t2.Minor {
53+
return t1.Minor < t2.Minor
54+
}
55+
56+
if t1.Patch != t2.Patch {
57+
return t1.Patch < t2.Patch
58+
}
59+
60+
if t1.Suffix == "" || t2.Suffix == "" {
61+
return t2.Suffix == ""
4362
}
4463

45-
return t1.Major < t2.Major ||
46-
(t1.Major == t2.Major && t1.Minor < t2.Minor) ||
47-
(t1.Major == t2.Major && t1.Minor == t2.Minor && t1.Patch < t2.Patch)
64+
return strings.Compare(t1.Suffix, t2.Suffix) < 0
4865
}
4966

5067
func filterReleases(rs releases, scheme string) releases {
@@ -65,7 +82,8 @@ func filterReleases(rs releases, scheme string) releases {
6582
if r.Project == schemeTag.Project &&
6683
(schemeTag.Major == -1 || schemeTag.Major == r.Major) &&
6784
(schemeTag.Minor == -1 || schemeTag.Minor == r.Minor) &&
68-
(schemeTag.Patch == -1 || schemeTag.Patch == r.Patch) {
85+
(schemeTag.Patch == -1 || schemeTag.Patch == r.Patch) &&
86+
(schemeTag.Suffix == "*" || schemeTag.Suffix == r.Suffix) {
6987
filteredReleases = append(filteredReleases, release)
7088
}
7189
}

0 commit comments

Comments
 (0)