-
Notifications
You must be signed in to change notification settings - Fork 3
/
version.go
58 lines (51 loc) · 1.29 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
package versionKit
import (
"github.com/hashicorp/go-version"
"github.com/richelieu-yang/chimera/v3/src/core/errorKit"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"sort"
)
// NewVersion
/*
@param v (1) 不能为""
(2) e.g. ""1.0.0-alpha.1+build.123""
"1.0.0-alpha+001"
"1.3.10+meta"
"v1.3.10+meta"
*/
func NewVersion(v string) (*version.Version, error) {
if err := strKit.AssertNotEmpty(v, "v"); err != nil {
return nil, err
}
return version.NewVersion(v)
}
// CheckConstraint 判断范围...
/*
@param versionStr e.g. "1.2"
@param constraintStr e.g. ">= 1.0, < 1.4"
*/
func CheckConstraint(versionStr, constraintStr string) (bool, error) {
v, err := NewVersion(versionStr)
if err != nil {
return false, err
}
constraint, err := version.NewConstraint(constraintStr)
if err != nil {
return false, err
}
return constraint.Check(v), nil
}
// Sort 排序.
func Sort(s []string) ([]*version.Version, error) {
versions := make([]*version.Version, len(s))
for i, ele := range s {
v, err := version.NewVersion(ele)
if err != nil {
return nil, errorKit.Wrapf(err, "ele(index: %d, value: %s) of param s is invalid", i, ele)
}
versions[i] = v
}
// After this, the versions are properly sorted
sort.Sort(version.Collection(versions))
return versions, nil
}