generated from WillAbides/goproject-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver-select.go
186 lines (171 loc) · 4.82 KB
/
semver-select.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"sort"
"strconv"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/alecthomas/kong"
)
const description = `
semver-select selects matching semvers from a list.
For example, get the newest version of go 1.15 like so:
curl -Ls 'https://golang.org/dl/?mode=json&include=all' \
| jq -r '.[].version' \
| sed 's/^go//g' \
| semver-select -i -c '1.15' -
`
var version = "unknown"
type rootCmd struct {
Version kong.VersionFlag `kong:"short=v,help='output semver-select version and exit'"`
Constraint string `kong:"required,short=c,help='semver constraint to match'"`
MaxResults int `kong:"short=n,help='maximum number of results to output'"`
IgnoreInvalid bool `kong:"short=i,help='ignore invalid candidates instead of erroring'"`
ValidateConstraint bool `kong:"help='just validate the constraint. exits non-zero if invalid'"`
GoVersions bool `kong:"name=go,help='allow go-style versions for candidates (e.g. 1.15rc1 or go1.20)'"`
Orig bool `kong:"help='output original version strings instead of normalized versions'"`
Candidates []string `kong:"arg,optional,help='candidate versions to consider -- value of \"-\" indicates stdin'"`
}
func getVersions(
args []string,
stdin io.Reader,
ignore, goVersions bool,
) ([]*semver.Version, map[*semver.Version][]string, error) {
res := make([]*semver.Version, 0, len(args))
doStdin := false
orig := map[*semver.Version][]string{}
var err error
for _, arg := range args {
if arg == "-" {
doStdin = true
break
}
res, err = addVersion(arg, ignore, goVersions, res, orig)
if err != nil {
return nil, nil, err
}
}
if !doStdin {
return res, orig, nil
}
r := bufio.NewScanner(stdin)
for r.Scan() {
res, err = addVersion(r.Text(), ignore, goVersions, res, orig)
if err != nil {
return nil, nil, err
}
}
return res, orig, nil
}
func addVersion(
ver string,
ignore, goVersions bool,
versions []*semver.Version,
orig map[*semver.Version][]string,
) ([]*semver.Version, error) {
v, err := semver.NewVersion(ver)
if err != nil && goVersions {
v, err = parseGoVersion(ver)
}
if err != nil {
if ignore {
return versions, nil
}
return nil, fmt.Errorf("could not parse version %q", ver)
}
orig[v] = append(orig[v], ver)
return append(versions, v), nil
}
func main() {
var cli rootCmd
parser := kong.Must(
&cli,
kong.Vars{"version": version},
kong.Description(strings.TrimSpace(description)),
)
run(parser, &cli, os.Stdin, os.Args[1:])
}
func run(parser *kong.Kong, cli *rootCmd, stdin io.Reader, args []string) {
k, err := parser.Parse(args)
if err != nil {
parser.Fatalf(err.Error())
return
}
c, err := semver.NewConstraint(cli.Constraint)
if err != nil {
k.Fatalf("invalid constraint: %q", cli.Constraint)
return
}
if cli.ValidateConstraint {
return
}
if len(cli.Candidates) == 0 {
k.Fatalf("no candidates provided")
return
}
versions, orig, err := getVersions(cli.Candidates, stdin, cli.IgnoreInvalid, cli.GoVersions)
if err != nil {
k.Fatalf(err.Error())
return
}
for _, s := range results(c, cli.MaxResults, versions, orig, cli.Orig) {
fmt.Fprintln(parser.Stdout, s)
}
}
func results(
c *semver.Constraints,
max int,
versions []*semver.Version,
orig map[*semver.Version][]string,
useOrig bool,
) []string {
candidates := make([]*semver.Version, 0, len(versions))
for _, v := range versions {
if c.Check(v) {
candidates = append(candidates, v)
}
}
sort.Sort(sort.Reverse(semver.Collection(candidates)))
if max > 0 && max < len(candidates) {
candidates = candidates[:max]
}
result := make([]string, 0, len(candidates))
for _, candidate := range candidates {
if useOrig {
result = append(result, orig[candidate]...)
continue
}
result = append(result, candidate.String())
}
return result
}
var goPattern = regexp.MustCompile(`^(?:go)?([1-9]\d*)(?:\.(0|[1-9]\d*))?(?:\.(0|[1-9]\d*))?([a-zA-Z][a-zA-Z0-9.-]*)?$`)
func parseGoVersion(ver string) (*semver.Version, error) {
matches := goPattern.FindStringSubmatch(ver)
if len(matches) == 0 {
return nil, fmt.Errorf("could not parse version %q", ver)
}
var major, minor, patch uint64
var err error
major, err = strconv.ParseUint(matches[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("could not parse major version %q: %v", ver, err)
}
if matches[2] != "" {
minor, err = strconv.ParseUint(matches[2], 10, 64)
if err != nil {
return nil, fmt.Errorf("could not parse minor version %q: %v", ver, err)
}
}
if matches[3] != "" {
patch, err = strconv.ParseUint(matches[3], 10, 64)
if err != nil {
return nil, fmt.Errorf("could not parse patch version %q: %v", ver, err)
}
}
return semver.New(major, minor, patch, matches[4], ""), nil
}