17
17
package platforms
18
18
19
19
import (
20
- "strconv"
21
- "strings"
22
-
20
+ "github.com/containerd/platforms"
23
21
specs "github.com/opencontainers/image-spec/specs-go/v1"
24
22
)
25
23
26
- // MatchComparer is able to match and compare platforms to
27
- // filter and sort platforms.
28
- type MatchComparer interface {
29
- Matcher
30
-
31
- Less (specs.Platform , specs.Platform ) bool
32
- }
33
-
34
- // platformVector returns an (ordered) vector of appropriate specs.Platform
35
- // objects to try matching for the given platform object (see platforms.Only).
36
- func platformVector (platform specs.Platform ) []specs.Platform {
37
- vector := []specs.Platform {platform }
38
-
39
- switch platform .Architecture {
40
- case "amd64" :
41
- if amd64Version , err := strconv .Atoi (strings .TrimPrefix (platform .Variant , "v" )); err == nil && amd64Version > 1 {
42
- for amd64Version -- ; amd64Version >= 1 ; amd64Version -- {
43
- vector = append (vector , specs.Platform {
44
- Architecture : platform .Architecture ,
45
- OS : platform .OS ,
46
- OSVersion : platform .OSVersion ,
47
- OSFeatures : platform .OSFeatures ,
48
- Variant : "v" + strconv .Itoa (amd64Version ),
49
- })
50
- }
51
- }
52
- vector = append (vector , specs.Platform {
53
- Architecture : "386" ,
54
- OS : platform .OS ,
55
- OSVersion : platform .OSVersion ,
56
- OSFeatures : platform .OSFeatures ,
57
- })
58
- case "arm" :
59
- if armVersion , err := strconv .Atoi (strings .TrimPrefix (platform .Variant , "v" )); err == nil && armVersion > 5 {
60
- for armVersion -- ; armVersion >= 5 ; armVersion -- {
61
- vector = append (vector , specs.Platform {
62
- Architecture : platform .Architecture ,
63
- OS : platform .OS ,
64
- OSVersion : platform .OSVersion ,
65
- OSFeatures : platform .OSFeatures ,
66
- Variant : "v" + strconv .Itoa (armVersion ),
67
- })
68
- }
69
- }
70
- case "arm64" :
71
- variant := platform .Variant
72
- if variant == "" {
73
- variant = "v8"
74
- }
75
- vector = append (vector , platformVector (specs.Platform {
76
- Architecture : "arm" ,
77
- OS : platform .OS ,
78
- OSVersion : platform .OSVersion ,
79
- OSFeatures : platform .OSFeatures ,
80
- Variant : variant ,
81
- })... )
82
- }
83
-
84
- return vector
85
- }
24
+ type MatchComparer = platforms.MatchComparer
86
25
87
26
// Only returns a match comparer for a single platform
88
27
// using default resolution logic for the platform.
@@ -92,7 +31,7 @@ func platformVector(platform specs.Platform) []specs.Platform {
92
31
// For arm/v6, will also match arm/v5
93
32
// For amd64, will also match 386
94
33
func Only (platform specs.Platform ) MatchComparer {
95
- return Ordered ( platformVector ( Normalize ( platform )) ... )
34
+ return platforms . Only ( platform )
96
35
}
97
36
98
37
// OnlyStrict returns a match comparer for a single platform.
@@ -104,100 +43,21 @@ func Only(platform specs.Platform) MatchComparer {
104
43
// OnlyStrict matches non-canonical forms.
105
44
// So, "arm64" matches "arm/64/v8".
106
45
func OnlyStrict (platform specs.Platform ) MatchComparer {
107
- return Ordered ( Normalize ( platform ) )
46
+ return platforms . OnlyStrict ( platform )
108
47
}
109
48
110
49
// Ordered returns a platform MatchComparer which matches any of the platforms
111
50
// but orders them in order they are provided.
112
- func Ordered (platforms ... specs.Platform ) MatchComparer {
113
- matchers := make ([]Matcher , len (platforms ))
114
- for i := range platforms {
115
- matchers [i ] = NewMatcher (platforms [i ])
116
- }
117
- return orderedPlatformComparer {
118
- matchers : matchers ,
119
- }
51
+ func Ordered (ps ... specs.Platform ) MatchComparer {
52
+ return platforms .Ordered (ps ... )
120
53
}
121
54
122
55
// Any returns a platform MatchComparer which matches any of the platforms
123
56
// with no preference for ordering.
124
- func Any (platforms ... specs.Platform ) MatchComparer {
125
- matchers := make ([]Matcher , len (platforms ))
126
- for i := range platforms {
127
- matchers [i ] = NewMatcher (platforms [i ])
128
- }
129
- return anyPlatformComparer {
130
- matchers : matchers ,
131
- }
57
+ func Any (ps ... specs.Platform ) MatchComparer {
58
+ return platforms .Any (ps ... )
132
59
}
133
60
134
61
// All is a platform MatchComparer which matches all platforms
135
62
// with preference for ordering.
136
- var All MatchComparer = allPlatformComparer {}
137
-
138
- type orderedPlatformComparer struct {
139
- matchers []Matcher
140
- }
141
-
142
- func (c orderedPlatformComparer ) Match (platform specs.Platform ) bool {
143
- for _ , m := range c .matchers {
144
- if m .Match (platform ) {
145
- return true
146
- }
147
- }
148
- return false
149
- }
150
-
151
- func (c orderedPlatformComparer ) Less (p1 specs.Platform , p2 specs.Platform ) bool {
152
- for _ , m := range c .matchers {
153
- p1m := m .Match (p1 )
154
- p2m := m .Match (p2 )
155
- if p1m && ! p2m {
156
- return true
157
- }
158
- if p1m || p2m {
159
- return false
160
- }
161
- }
162
- return false
163
- }
164
-
165
- type anyPlatformComparer struct {
166
- matchers []Matcher
167
- }
168
-
169
- func (c anyPlatformComparer ) Match (platform specs.Platform ) bool {
170
- for _ , m := range c .matchers {
171
- if m .Match (platform ) {
172
- return true
173
- }
174
- }
175
- return false
176
- }
177
-
178
- func (c anyPlatformComparer ) Less (p1 , p2 specs.Platform ) bool {
179
- var p1m , p2m bool
180
- for _ , m := range c .matchers {
181
- if ! p1m && m .Match (p1 ) {
182
- p1m = true
183
- }
184
- if ! p2m && m .Match (p2 ) {
185
- p2m = true
186
- }
187
- if p1m && p2m {
188
- return false
189
- }
190
- }
191
- // If one matches, and the other does, sort match first
192
- return p1m && ! p2m
193
- }
194
-
195
- type allPlatformComparer struct {}
196
-
197
- func (allPlatformComparer ) Match (specs.Platform ) bool {
198
- return true
199
- }
200
-
201
- func (allPlatformComparer ) Less (specs.Platform , specs.Platform ) bool {
202
- return false
203
- }
63
+ var All = platforms .All
0 commit comments