-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver.go
75 lines (64 loc) · 1.99 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
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
package semver
import "fmt"
// Valid checks if a provided string is a valid version
func Valid(str string) bool {
if _, err := ParseVersion(str); err == nil {
return true
}
return false
}
func toVersion(e interface{}) (*Version, error) {
switch v := e.(type) {
case *Version:
return v, nil
case string:
return ParseVersion(v)
default:
return nil, fmt.Errorf("unknown element type")
}
}
func toExpression(e interface{}) (Expression, error) {
switch v := e.(type) {
case Expression:
return v, nil
case string:
return ParseExpr(v)
default:
return nil, fmt.Errorf("unknown element type: %T", v)
}
}
// Satisfies receives a version (*Version or its string representation) and an expression (Expression or its string representation)
// and returns whether the version satisfies the expression or not
func Satisfies(version interface{}, expr interface{}) (bool, error) {
var err error
var v *Version
var e Expression
if v, err = toVersion(version); err != nil {
return false, fmt.Errorf("Cannot parse version: %v", err)
}
if e, err = toExpression(expr); err != nil {
return false, fmt.Errorf("Cannot parse expression: %v", err)
}
return e.Matches(v), nil
}
// ONLY VERSIONS FOR NOW (to be implemented: ranges and GlobVersion )
// Greater checks if element e1 is greater than e2
func Greater(v1 Comparable, v2 Comparable) bool {
return v1.(*Version).Greater(v2.(*Version))
}
// Less checks if element e1 is less than e2
func Less(v1 Comparable, v2 Comparable) bool {
return v1.(*Version).Less(v2.(*Version))
}
// GreaterOrEqual checks if element e1 is greater or equal than e2
func GreaterOrEqual(v1 Comparable, v2 Comparable) bool {
return v1.(*Version).GreaterOrEqual(v2.(*Version))
}
// LessOrEqual checks if element e1 is less or equal to e2
func LessOrEqual(v1 Comparable, v2 Comparable) bool {
return v1.(*Version).LessOrEqual(v2.(*Version))
}
// Equal checks if element e1 is equal to e2
func Equal(v1 Comparable, v2 Comparable) bool {
return v1.(*Version).Equal(v2.(*Version))
}