2019-04-18 07:48:52 +08:00
|
|
|
package platformutil
|
2019-03-24 12:30:29 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2024-07-03 13:15:15 +08:00
|
|
|
"github.com/containerd/platforms"
|
2019-03-24 12:30:29 +08:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
)
|
|
|
|
|
2019-04-18 07:48:52 +08:00
|
|
|
func Parse(platformsStr []string) ([]specs.Platform, error) {
|
2019-03-24 12:30:29 +08:00
|
|
|
if len(platformsStr) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
out := make([]specs.Platform, 0, len(platformsStr))
|
|
|
|
for _, s := range platformsStr {
|
|
|
|
parts := strings.Split(s, ",")
|
|
|
|
if len(parts) > 1 {
|
2019-04-18 07:48:52 +08:00
|
|
|
p, err := Parse(parts)
|
2019-03-24 12:30:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = append(out, p...)
|
|
|
|
continue
|
|
|
|
}
|
2019-05-07 07:55:23 +08:00
|
|
|
p, err := parse(s)
|
2019-03-24 12:30:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = append(out, platforms.Normalize(p))
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2019-04-18 08:55:27 +08:00
|
|
|
|
2019-05-07 07:55:23 +08:00
|
|
|
func parse(in string) (specs.Platform, error) {
|
|
|
|
if strings.EqualFold(in, "local") {
|
|
|
|
return platforms.DefaultSpec(), nil
|
|
|
|
}
|
|
|
|
return platforms.Parse(in)
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:55:27 +08:00
|
|
|
func Dedupe(in []specs.Platform) []specs.Platform {
|
|
|
|
m := map[string]struct{}{}
|
|
|
|
out := make([]specs.Platform, 0, len(in))
|
|
|
|
for _, p := range in {
|
|
|
|
p := platforms.Normalize(p)
|
|
|
|
key := platforms.Format(p)
|
|
|
|
if _, ok := m[key]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m[key] = struct{}{}
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-04-25 11:08:43 +08:00
|
|
|
func FormatInGroups(gg ...[]specs.Platform) []string {
|
|
|
|
m := map[string]struct{}{}
|
|
|
|
out := make([]string, 0, len(gg))
|
|
|
|
for i, g := range gg {
|
|
|
|
for _, p := range g {
|
|
|
|
p := platforms.Normalize(p)
|
|
|
|
key := platforms.Format(p)
|
|
|
|
if _, ok := m[key]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m[key] = struct{}{}
|
|
|
|
v := platforms.Format(p)
|
|
|
|
if i == 0 {
|
|
|
|
v += "*"
|
|
|
|
}
|
|
|
|
out = append(out, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:55:27 +08:00
|
|
|
func Format(in []specs.Platform) []string {
|
|
|
|
if len(in) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out := make([]string, 0, len(in))
|
|
|
|
for _, p := range in {
|
|
|
|
out = append(out, platforms.Format(p))
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|