/
semver.go
47 lines (35 loc) · 1.03 KB
/
semver.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package verify
import (
gversion "github.com/hashicorp/go-version"
)
// SemVerLessThan returns whether or not the first version string is less than the second
// according to Semantic Versioning rules (https://semver.org/).
func SemVerLessThan(s1, s2 string) bool {
v1, v2, err := parseVersions(s1, s2)
if err != nil {
return false
}
return v1.LessThan(v2)
}
// SemVerGreaterThanOrEqual returns whether or not the first version string is greater than or equal
// to the second according to Semantic Versioning rules (https://semver.org/).
func SemVerGreaterThanOrEqual(s1, s2 string) bool {
v1, v2, err := parseVersions(s1, s2)
if err != nil {
return false
}
return v1.GreaterThanOrEqual(v2)
}
func parseVersions(s1, s2 string) (*gversion.Version, *gversion.Version, error) {
v1, err := gversion.NewVersion(s1)
if err != nil {
return nil, nil, err
}
v2, err := gversion.NewVersion(s2)
if err != nil {
return nil, nil, err
}
return v1, v2, nil
}