Skip to content

Commit bd8a2c9

Browse files
committed
opts: implement PlatformSlice option
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 28f19a9 commit bd8a2c9

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

cli/command/image/remove.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import (
66
"fmt"
77

88
cerrdefs "github.com/containerd/errdefs"
9-
"github.com/containerd/platforms"
109
"github.com/docker/cli/cli"
1110
"github.com/docker/cli/cli/command"
1211
"github.com/docker/cli/cli/command/completion"
12+
"github.com/docker/cli/opts"
1313
"github.com/docker/docker/api/types/image"
14+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1415
"github.com/spf13/cobra"
1516
)
1617

1718
type removeOptions struct {
1819
force bool
1920
noPrune bool
20-
platforms []string
21+
platforms []ocispec.Platform
2122
}
2223

2324
// NewRemoveCommand creates a new `docker remove` command
@@ -42,8 +43,7 @@ func NewRemoveCommand(dockerCLI command.Cli) *cobra.Command {
4243
flags.BoolVarP(&options.force, "force", "f", false, "Force removal of the image")
4344
flags.BoolVar(&options.noPrune, "no-prune", false, "Do not delete untagged parents")
4445

45-
// TODO(thaJeztah): create a "platforms" option for this (including validation / parsing).
46-
flags.StringSliceVar(&options.platforms, "platform", nil, `Remove only the given platform variant. Formatted as "os[/arch[/variant]]" (e.g., "linux/amd64")`)
46+
flags.Var(opts.NewPlatformSlice(&options.platforms), "platform", `Remove only the given platform variant. Formatted as "os[/arch[/variant]]" (e.g., "linux/amd64")`)
4747
_ = flags.SetAnnotation("platform", "version", []string{"1.50"})
4848

4949
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
@@ -63,14 +63,7 @@ func runRemove(ctx context.Context, dockerCLI command.Cli, opts removeOptions, i
6363
options := image.RemoveOptions{
6464
Force: opts.force,
6565
PruneChildren: !opts.noPrune,
66-
}
67-
68-
for _, v := range opts.platforms {
69-
p, err := platforms.Parse(v)
70-
if err != nil {
71-
return err
72-
}
73-
options.Platforms = append(options.Platforms, p)
66+
Platforms: opts.platforms,
7467
}
7568

7669
// TODO(thaJeztah): this logic can likely be simplified: do we want to print "not found" errors at all when using "force"?

opts/platforms.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
var values []string
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

Comments
 (0)