-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
71 lines (59 loc) · 1.37 KB
/
main.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
package main
import (
"fmt"
"log"
"sort"
"github.com/aquasecurity/go-version/pkg/semver"
)
func main() {
compareVersions()
sortVersions()
satisfiedByVersion()
}
func compareVersions() {
fmt.Println("=== compare ===")
v1, err := semver.Parse("1.2.0")
if err != nil {
log.Fatal(err)
}
v2, err := semver.Parse("1.2.1")
if err != nil {
log.Fatal(err)
}
// Comparison example. There is also GreaterThan, Equal, and just
// a simple Compare that returns an int allowing easy >=, <=, etc.
if v1.LessThan(v2) {
fmt.Printf("%s is less than %s\n", v1, v2)
}
}
func sortVersions() {
fmt.Println("\n=== sort ===")
versionsRaw := []string{"1.1.0", "0.7.1", "1.4.0", "1.4.0-alpha", "1.4.1-beta", "1.4.0-alpha.2+20130313144700"}
versions := make([]semver.Version, len(versionsRaw))
for i, raw := range versionsRaw {
v, err := semver.Parse(raw)
if err != nil {
log.Fatal(err)
}
versions[i] = v
}
// After this, the versions are properly sorted
sort.Sort(semver.Collection(versions))
for _, v := range versions {
fmt.Println(v)
}
}
func satisfiedByVersion() {
fmt.Println("\n=== constraint ===")
v, err := semver.Parse("2.1.0")
if err != nil {
log.Fatal(err)
}
c, err := semver.NewConstraints(">= 1.0, < 1.4 || > 2.0", semver.WithPreRelease(true))
if err != nil {
log.Fatal(err)
}
if c.Check(v) {
fmt.Printf("%s satisfies constraints '%s'", v, c)
}
}