-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpm_constraint.go
69 lines (57 loc) · 1.54 KB
/
rpm_constraint.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
package version
import (
"fmt"
)
type rpmConstraint struct {
raw string
expression constraintExpression
}
func newRpmConstraint(raw string) (rpmConstraint, error) {
if raw == "" {
// an empty constraint is always satisfied
return rpmConstraint{}, nil
}
constraints, err := newConstraintExpression(raw, newRpmComparator)
if err != nil {
return rpmConstraint{}, fmt.Errorf("unable to parse rpm constraint phrase: %w", err)
}
return rpmConstraint{
raw: raw,
expression: constraints,
}, nil
}
func newRpmComparator(unit constraintUnit) (Comparator, error) {
ver, err := newRpmVersion(unit.version)
if err != nil {
return nil, fmt.Errorf("unable to parse constraint version (%s): %w", unit.version, err)
}
return &ver, nil
}
func (c rpmConstraint) supported(format Format) bool {
return format == RpmFormat
}
func (c rpmConstraint) Satisfied(version *Version) (bool, error) {
if c.raw == "" && version != nil {
// an empty constraint is always satisfied
return true, nil
} else if version == nil {
if c.raw != "" {
// a non-empty constraint with no version given should always fail
return false, nil
}
return true, nil
}
if !c.supported(version.Format) {
return false, fmt.Errorf("(rpm) unsupported format: %s", version.Format)
}
if version.rich.rpmVer == nil {
return false, fmt.Errorf("no rich rpm version given: %+v", version)
}
return c.expression.satisfied(version)
}
func (c rpmConstraint) String() string {
if c.raw == "" {
return "none (rpm)"
}
return fmt.Sprintf("%s (rpm)", c.raw)
}