-
Notifications
You must be signed in to change notification settings - Fork 534
Expand file tree
/
Copy pathcmpver.go
More file actions
186 lines (158 loc) · 4.01 KB
/
cmpver.go
File metadata and controls
186 lines (158 loc) · 4.01 KB
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
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package cmpver
import (
"encoding/hex"
"fmt"
"strings"
semver "github.com/Masterminds/semver"
)
type Operation string
const (
Greater Operation = ">"
GreaterOrEqual Operation = ">="
Less Operation = "<"
LessOrEqual Operation = "<="
)
// Compare compare ver1 and ver2 by operation
//
// For example:
//
// Compare(ver1, Greater, ver2) is same as "ver1 > ver2"
//
// Dirty versions and pre versions are regarded as standard version.
// For example: 'v5.1.2-dev' and 'v5.1.2-betav1' are regarded as 'v5.1.2'.
//
// Latest, nightly or master version is larger than any version
func Compare(ver1 string, op Operation, ver2 string) (bool, error) {
err := validateOperation(op)
if err != nil {
return false, err
}
c, err := NewConstraint(op, ver2)
if err != nil {
return false, err
}
return c.Check(ver1)
}
// CompareByStr compare ver1 and ver2
//
// For example:
//
// CompareByStr(ver1, ">", ver2) is same as "ver1 > ver2"
//
// Dirty versions and pre versions are regarded as standard version.
// For example: 'v5.1.2-dev' and 'v5.1.2-betav1' are regarded as 'v5.1.2'.
//
// Latest, nightly or master version is larger than any version
func CompareByStr(ver1, opstr, ver2 string) (bool, error) {
op := Operation(opstr)
return Compare(ver1, op, ver2)
}
type Constraint struct {
op Operation
version string
constraint *semver.Constraints
}
func NewConstraint(op Operation, version string) (*Constraint, error) {
err := validateOperation(op)
if err != nil {
return nil, err
}
// default to support dirty version
constraint, err := semver.NewConstraint(fmt.Sprintf("%s%s", op, version))
if err != nil {
return nil, err
}
return &Constraint{
op: Operation(op),
version: version,
constraint: constraint,
}, nil
}
// Check tests if a version satisfies the constraints.
//
// Dirty versions and pre versions are regarded as standard version.
// For example: 'v5.1.2-dev' and 'v5.1.2-betav1' are regarded as 'v5.1.2'.
//
// sha256, Latest, nightly or master version is larger than any version
func (c *Constraint) Check(version string) (bool, error) {
if isSha(version) {
return compareSha(c.op), nil
}
if isLatest(version) {
return compareLatest(c.op), nil
}
ver, err := semver.NewVersion(version)
if err != nil {
return false, err
}
// set prerelease to empty str in order to support dirty version
ckVer := *ver
if ver.Prerelease() != "" {
ckVer, err = ver.SetPrerelease("")
if err != nil {
return false, err
}
}
return c.constraint.Check(&ckVer), nil
}
func validateOperation(op Operation) error {
switch op {
case Greater, GreaterOrEqual, Less, LessOrEqual:
return nil
}
return fmt.Errorf("not support operation %s", op)
}
func isLatest(version string) bool {
switch {
case version == "latest",
version == "nightly",
version == "master",
strings.HasPrefix(version, "latest-"),
strings.HasPrefix(version, "nightly-"),
strings.HasPrefix(version, "master-"):
return true
}
return false
}
func compareLatest(op Operation) bool {
// latest is greater than any version
switch op {
case Greater, GreaterOrEqual:
return true
case Less, LessOrEqual:
return false
}
return false
}
func isSha(version string) bool {
if len(version) != 64 {
return false
}
_, err := hex.DecodeString(version)
if err != nil {
return false
}
return true
}
func compareSha(op Operation) bool {
// Sha is greater than any version
switch op {
case Greater, GreaterOrEqual:
return true
case Less, LessOrEqual:
return false
}
return false
}