Skip to content

Commit 54d1564

Browse files
committed
image: Add convert command
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent 94b4c10 commit 54d1564

23 files changed

+1580
-0
lines changed

cli/command/image/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func NewImageCommand(dockerCli command.Cli) *cobra.Command {
2727
newRemoveCommand(dockerCli),
2828
newInspectCommand(dockerCli),
2929
NewPruneCommand(dockerCli),
30+
NewConvertCommand(dockerCli),
3031
)
3132
return cmd
3233
}

cli/command/image/convert.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package image
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/containerd/platforms"
8+
"github.com/distribution/reference"
9+
"github.com/docker/cli/cli"
10+
"github.com/docker/cli/cli/command"
11+
imagetypes "github.com/docker/docker/api/types/image"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
type convertArgs struct {
16+
Src string
17+
Dst []string
18+
Platforms []string
19+
NoAttestations bool
20+
OnlyAvailablePlatforms bool
21+
}
22+
23+
func NewConvertCommand(dockerCli command.Cli) *cobra.Command {
24+
var args convertArgs
25+
26+
cmd := &cobra.Command{
27+
Use: "convert [OPTIONS] <from>",
28+
Short: "Convert multi-platform images",
29+
Args: cli.ExactArgs(1),
30+
RunE: func(cmd *cobra.Command, posArgs []string) error {
31+
args.Src = posArgs[0]
32+
return runConvert(cmd.Context(), dockerCli, args)
33+
},
34+
Aliases: []string{"convert"},
35+
Annotations: map[string]string{
36+
"aliases": "docker image convert, docker convert",
37+
},
38+
}
39+
40+
flags := cmd.Flags()
41+
flags.StringArrayVar(&args.Platforms, "platforms", nil, "Include only the specified platforms in the destination image")
42+
flags.BoolVar(&args.NoAttestations, "no-attestations", false, "Do not include image attestations")
43+
flags.BoolVar(&args.OnlyAvailablePlatforms, "available", false, "Only include platforms locally available to the daemon")
44+
flags.StringArrayVar(&args.Dst, "to", nil, "Target image references")
45+
46+
return cmd
47+
}
48+
49+
func runConvert(ctx context.Context, dockerCLI command.Cli, args convertArgs) error {
50+
if len(args.Dst) == 0 {
51+
return fmt.Errorf("No destination image specified")
52+
}
53+
54+
var dstRefs []reference.NamedTagged
55+
for _, dst := range args.Dst {
56+
dstRef, err := reference.ParseNormalizedNamed(dst)
57+
if err != nil {
58+
return fmt.Errorf("invalid destination image reference: %s: %w", dst, err)
59+
}
60+
61+
dstRef = reference.TagNameOnly(dstRef)
62+
dstRefTagged := dstRef.(reference.NamedTagged)
63+
dstRefs = append(dstRefs, dstRefTagged)
64+
}
65+
66+
opts := imagetypes.ConvertOptions{
67+
NoAttestations: args.NoAttestations,
68+
OnlyAvailablePlatforms: args.OnlyAvailablePlatforms,
69+
}
70+
71+
for _, platform := range args.Platforms {
72+
p, err := platforms.Parse(platform)
73+
if err != nil {
74+
return err
75+
}
76+
opts.Platforms = append(opts.Platforms, p)
77+
}
78+
79+
return dockerCLI.Client().ImageConvert(ctx, args.Src, dstRefs, opts)
80+
}

vendor.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go 1.21
99
require (
1010
dario.cat/mergo v1.0.0
1111
github.com/containerd/containerd v1.7.14
12+
github.com/containerd/platforms v0.1.1
1213
github.com/creack/pty v1.1.21
1314
github.com/distribution/reference v0.5.0
1415
github.com/docker/distribution v2.8.3+incompatible

vendor.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ github.com/containerd/containerd v1.7.14 h1:H/XLzbnGuenZEGK+v0RkwTdv2u1QFAruMe5N
4343
github.com/containerd/containerd v1.7.14/go.mod h1:YMC9Qt5yzNqXx/fO4j/5yYVIHXSRrlB3H7sxkUTvspg=
4444
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
4545
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
46+
github.com/containerd/platforms v0.1.1 h1:gp0xXBoY+1CjH54gJDon0kBjIbK2C4XSX1BGwP5ptG0=
47+
github.com/containerd/platforms v0.1.1/go.mod h1:XOM2BS6kN6gXafPLg80V6y/QUib+xoLyC3qVmHzibko=
4648
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
4749
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
4850
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=

vendor/github.com/containerd/platforms/.gitattributes

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/platforms/.golangci.yml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/platforms/LICENSE

Lines changed: 191 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/platforms/README.md

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)