-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
versions.go
75 lines (64 loc) · 1.64 KB
/
versions.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
package migrations
import (
"bufio"
"context"
"errors"
"fmt"
"path"
"sort"
"strings"
"github.com/blang/semver/v4"
)
const distVersions = "versions"
// LatestDistVersion returns the latest version, of the specified distribution,
// that is available on the distribution site.
func LatestDistVersion(ctx context.Context, fetcher Fetcher, dist string, stableOnly bool) (string, error) {
vs, err := DistVersions(ctx, fetcher, dist, false)
if err != nil {
return "", err
}
for i := len(vs) - 1; i >= 0; i-- {
ver := vs[i]
if stableOnly && strings.Contains(ver, "-rc") {
continue
}
if strings.Contains(ver, "-dev") {
continue
}
return ver, nil
}
return "", errors.New("could not find a non dev version")
}
// DistVersions returns all versions of the specified distribution, that are
// available on the distriburion site. List is in ascending order, unless
// sortDesc is true.
func DistVersions(ctx context.Context, fetcher Fetcher, dist string, sortDesc bool) ([]string, error) {
rc, err := fetcher.Fetch(ctx, path.Join(dist, distVersions))
if err != nil {
return nil, err
}
defer rc.Close()
prefix := "v"
var vers []semver.Version
scan := bufio.NewScanner(rc)
for scan.Scan() {
ver, err := semver.Make(strings.TrimLeft(scan.Text(), prefix))
if err != nil {
continue
}
vers = append(vers, ver)
}
if scan.Err() != nil {
return nil, fmt.Errorf("could not read versions: %s", scan.Err())
}
if sortDesc {
sort.Sort(sort.Reverse(semver.Versions(vers)))
} else {
sort.Sort(semver.Versions(vers))
}
out := make([]string, len(vers))
for i := range vers {
out[i] = prefix + vers[i].String()
}
return out, nil
}