-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
200 lines (171 loc) · 4.21 KB
/
version.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
)
var allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789"
var versionRegex = regexp.MustCompile(`^v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?$`)
type versions []*version
func (v versions) Len() int { return len(v) }
func (v versions) Less(i, j int) bool { return v[i].compare(v[j]) > 0 }
func (v versions) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
// version represents a single semantic version.
type version struct {
major, minor, patch uint64
prerelease string
metadata string
incompatible bool
original string
}
func (v *version) cleanString() string {
return fmt.Sprintf("v%d.%d.%d", v.major, v.minor, v.patch)
}
func getProxyVersions(url string, skipPrelease bool) versions {
resp, err := http.Get(fmt.Sprintf("https://proxy.golang.org/%s/@v/list", url))
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var vs versions
versionsString := strings.Split(string(body), "\n")
for _, vss := range versionsString {
v, err := parseVersion(vss)
if err != nil {
// If has error parsing skip it
continue
}
if skipPrelease && v.prerelease != "" {
continue
}
vs = append(vs, v)
}
sort.Sort(vs)
return vs
}
// parseVersion parses a given version mod
func parseVersion(v string) (*version, error) {
m := versionRegex.FindStringSubmatch(v)
if m == nil {
return nil, errors.New("Invalid Semantic Version")
}
var err error
// Regex breakdown
major := m[1]
minor := m[2]
patch := m[3]
prerelease := m[5]
metadata := m[8]
sv := &version{
prerelease: prerelease,
metadata: metadata,
original: v,
incompatible: strings.Contains(v, "incompatible"),
}
// Major check
sv.major, err = strconv.ParseUint(major, 10, 64)
if err != nil {
return nil, fmt.Errorf("Error parsing version segment: %s", err)
}
// Minor check
if minor != "" {
sv.minor, err = strconv.ParseUint(strings.TrimPrefix(minor, "."), 10, 64)
if err != nil {
return nil, fmt.Errorf("Error parsing version segment: %s", err)
}
} else {
sv.minor = 0
}
// Patch check
if patch != "" {
sv.patch, err = strconv.ParseUint(strings.TrimPrefix(patch, "."), 10, 64)
if err != nil {
return nil, fmt.Errorf("Error parsing version segment: %s", err)
}
} else {
sv.patch = 0
}
// Prerelease check
if sv.prerelease != "" {
if err = validatePrerelease(sv.prerelease); err != nil {
return nil, err
}
}
// Metadata check
if sv.metadata != "" {
if err = validateMetadata(sv.metadata); err != nil {
return nil, err
}
}
return sv, nil
}
func (v *version) compare(v2 *version) int {
if d := compareSegment(v.major, v2.major); d != 0 {
return d
}
if d := compareSegment(v.minor, v2.minor); d != 0 {
return d
}
if d := compareSegment(v.patch, v2.patch); d != 0 {
return d
}
// Major, minor, and patch are the same lets check prerelease
if v.prerelease == "" && v2.prerelease == "" {
return 0
}
if v.prerelease == "" {
return 1
}
if v2.prerelease == "" {
return -1
}
return 0
}
func compareSegment(v, o uint64) int {
if v < o {
return -1
}
if v > o {
return 1
}
return 0
}
// Like strings.ContainsAny but does an only instead of any.
func containsOnly(s string, comp string) bool {
return strings.IndexFunc(s, func(r rune) bool {
return !strings.ContainsRune(comp, r)
}) == -1
}
// validatePrerelease loops through values to check for valid characters
func validatePrerelease(p string) error {
eparts := strings.Split(p, ".")
for _, p := range eparts {
if containsOnly(p, "0123456789") {
if len(p) > 1 && p[0] == '0' {
return errors.New("Version segment starts with 0")
}
} else if !containsOnly(p, allowed) {
return errors.New("Invalid Prerelease string")
}
}
return nil
}
// validateMetadata loops through values to check for valid characters
func validateMetadata(m string) error {
eparts := strings.Split(m, ".")
for _, p := range eparts {
if !containsOnly(p, allowed) {
return errors.New("Invalid Metadata string")
}
}
return nil
}