-
Notifications
You must be signed in to change notification settings - Fork 2
/
goversion.go
139 lines (124 loc) · 2.97 KB
/
goversion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"log/slog"
"maps"
"slices"
"sort"
"strconv"
"strings"
)
func toolchains(modvers []ModuleVersion) Toolchains {
// todo: replace with something less horrible and more efficient.
type version struct {
minor int
patch int
rc int
orig string
}
versions := map[version]ModuleVersion{}
for _, mv := range modvers {
v := mv.Version
// "v0.0.1-go1.X(.Y|rcZ|betaZ)-$goos.$goarch"
if !strings.HasPrefix(v, "v0.0.1-") {
slog.Warn("unrecognized toolchain version, ignoring", "version", mv.Version)
continue
}
v = strings.TrimPrefix(v, "v0.0.1-")
i := strings.LastIndex(v, ".") // For ".$goos-$goarch"
if i < 0 {
slog.Warn("no .$goos-$goarch suffix in go toolchain, ignoring", "version", mv.Version)
continue
}
v = v[:i]
goversion := v
if v == "go1.9.2rc2" {
continue
}
if !strings.HasPrefix(v, "go1.") {
slog.Warn("version does not start with 'go1.', ignoring", "version", mv.Version)
continue
}
v = v[len("go1."):]
// Read minor.
n := 0
for _, c := range v {
if c >= '0' && c <= '9' {
n++
} else {
break
}
}
if n == 0 {
slog.Warn("no minor", "version", mv.Version)
continue
}
minor, err := strconv.ParseInt(v[:n], 10, 32)
if err != nil {
slog.Warn("parsing go version number, bad minor", "version", mv.Version, "err", err)
continue
}
v = v[n:]
var patch, rc int64
if v == "" {
patch = 0
} else if strings.HasPrefix(v, "rc") || strings.HasPrefix(v, "beta") {
if strings.HasPrefix(v, "rc") {
v = v[2:]
} else {
v = v[4:]
}
rc, err = strconv.ParseInt(v, 10, 32)
if err != nil {
slog.Error("parsing go version number, bad release candidate", "version", mv.Version, "err", err)
continue
}
} else if !strings.HasPrefix(v, ".") {
slog.Error("parsing go version number, no dot after minor", "version", mv.Version)
continue
} else if patch, err = strconv.ParseInt(v[1:], 10, 32); err != nil {
slog.Error("parsing go version number, bad patch", "version", mv.Version, "err", err)
continue
}
versions[version{int(minor), int(patch), int(rc), goversion}] = mv
}
l := slices.Collect(maps.Keys(versions))
// Sort newest first.
sort.Slice(l, func(i, j int) bool {
a, b := l[i], l[j]
if a.minor != b.minor {
return a.minor > b.minor
}
if a.patch != b.patch {
return a.patch > b.patch
}
if a.rc != b.rc && (a.rc == 0 || b.rc == 0) {
return a.rc == 0
}
return a.rc > b.rc
})
var gonext, gocur, goprev *version
var tc Toolchains
// Look for next.
if len(l) > 0 && l[0].rc != 0 {
gonext = &l[0]
tc.Next = gonext.orig
tc.NextFound = versions[l[0]].Discovered
}
for len(l) > 0 && l[0].rc != 0 {
l = l[1:]
}
if len(l) > 0 {
gocur = &l[0]
tc.Cur = gocur.orig
tc.CurFound = versions[l[0]].Discovered
}
for len(l) > 0 && (l[0].minor == gocur.minor || l[0].rc != 0) {
l = l[1:]
}
if len(l) > 0 {
goprev = &l[0]
tc.Prev = goprev.orig
tc.PrevFound = versions[l[0]].Discovered
}
return tc
}