Skip to content

Commit d9775f8

Browse files
committed
image: Add convert command
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent 05c9d45 commit d9775f8

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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]",
28+
Short: "Convert multi-platform images",
29+
Args: cli.ExactArgs(0),
30+
RunE: func(cmd *cobra.Command, _ []string) error {
31+
return runConvert(cmd.Context(), dockerCli, args)
32+
},
33+
Aliases: []string{"convert"},
34+
Annotations: map[string]string{
35+
"aliases": "docker image convert, docker convert",
36+
},
37+
}
38+
39+
flags := cmd.Flags()
40+
flags.StringArrayVar(&args.Platforms, "platforms", nil, "Include only the specified platforms in the destination image")
41+
flags.BoolVar(&args.NoAttestations, "no-attestations", false, "Do not include image attestations")
42+
flags.BoolVar(&args.OnlyAvailablePlatforms, "available", false, "Only include platforms locally available to the daemon")
43+
flags.StringArrayVar(&args.Dst, "to", nil, "Target image references")
44+
flags.StringVar(&args.Src, "from", "", "Source image reference")
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+
if args.Src == "" {
54+
return fmt.Errorf("No source image specified")
55+
}
56+
57+
var dstRefs []reference.NamedTagged
58+
for _, dst := range args.Dst {
59+
dstRef, err := reference.ParseNormalizedNamed(dst)
60+
if err != nil {
61+
return fmt.Errorf("invalid destination image reference: %s: %w", dst, err)
62+
}
63+
64+
dstRef = reference.TagNameOnly(dstRef)
65+
dstRefTagged := dstRef.(reference.NamedTagged)
66+
dstRefs = append(dstRefs, dstRefTagged)
67+
}
68+
69+
opts := imagetypes.ConvertOptions{
70+
NoAttestations: args.NoAttestations,
71+
OnlyAvailablePlatforms: args.OnlyAvailablePlatforms,
72+
}
73+
74+
for _, platform := range args.Platforms {
75+
p, err := platforms.Parse(platform)
76+
if err != nil {
77+
return err
78+
}
79+
opts.Platforms = append(opts.Platforms, p)
80+
}
81+
82+
return dockerCLI.Client().ImageConvert(ctx, args.Src, dstRefs, opts)
83+
}

0 commit comments

Comments
 (0)