|
| 1 | +package opts |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/containerd/platforms" |
| 7 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 8 | +) |
| 9 | + |
| 10 | +func NewPlatformSlice(val *[]ocispec.Platform) *PlatformSlice { |
| 11 | + return &PlatformSlice{values: val} |
| 12 | +} |
| 13 | + |
| 14 | +// PlatformSlice is a Value type for passing multiple platforms. |
| 15 | +type PlatformSlice struct { |
| 16 | + values *[]ocispec.Platform |
| 17 | +} |
| 18 | + |
| 19 | +func (m *PlatformSlice) Set(value string) error { |
| 20 | + vals := strings.Split(value, ",") |
| 21 | + for _, val := range vals { |
| 22 | + p, err := platforms.Parse(val) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + *m.values = append(*m.values, p) |
| 27 | + } |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +// Type returns the type of this option |
| 32 | +func (*PlatformSlice) Type() string { |
| 33 | + return "platforms" |
| 34 | +} |
| 35 | + |
| 36 | +// String returns a string representation of this option. |
| 37 | +func (m *PlatformSlice) String() string { |
| 38 | + return strings.Join(m.GetSlice(), ", ") |
| 39 | +} |
| 40 | + |
| 41 | +// GetSlice returns the platforms as a string-slice. |
| 42 | +func (m *PlatformSlice) GetSlice() []string { |
| 43 | + values := make([]string, 0, len(*m.values)) |
| 44 | + for _, v := range *m.values { |
| 45 | + values = append(values, platforms.FormatAll(v)) |
| 46 | + } |
| 47 | + return values |
| 48 | +} |
| 49 | + |
| 50 | +// Value returns the platforms |
| 51 | +func (m *PlatformSlice) Value() []ocispec.Platform { |
| 52 | + return *m.values |
| 53 | +} |
0 commit comments