Skip to content

Commit

Permalink
image: Add convert command
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
  • Loading branch information
vvoland committed May 16, 2024
1 parent fcbd3dc commit 4b3b15f
Show file tree
Hide file tree
Showing 23 changed files with 1,584 additions and 4 deletions.
1 change: 1 addition & 0 deletions cli/command/image/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewImageCommand(dockerCli command.Cli) *cobra.Command {
newRemoveCommand(dockerCli),
newInspectCommand(dockerCli),
NewPruneCommand(dockerCli),
NewConvertCommand(dockerCli),
)
return cmd
}
80 changes: 80 additions & 0 deletions cli/command/image/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package image

import (
"context"
"fmt"

"github.com/containerd/platforms"
"github.com/distribution/reference"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/spf13/cobra"
)

type convertArgs struct {
Src string
Dst []string
Platforms []string
NoAttestations bool
OnlyAvailablePlatforms bool
}

func NewConvertCommand(dockerCli command.Cli) *cobra.Command {
var args convertArgs

cmd := &cobra.Command{
Use: "convert [OPTIONS] <from>",
Short: "Convert multi-platform images",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, posArgs []string) error {
args.Src = posArgs[0]
return runConvert(cmd.Context(), dockerCli, args)
},
Aliases: []string{"convert"},
Annotations: map[string]string{
"aliases": "docker image convert, docker convert",
},
}

flags := cmd.Flags()
flags.StringArrayVar(&args.Platforms, "platforms", nil, "Include only the specified platforms in the destination image")
flags.BoolVar(&args.NoAttestations, "no-attestations", false, "Do not include image attestations")
flags.BoolVar(&args.OnlyAvailablePlatforms, "available", false, "Only include platforms locally available to the daemon")
flags.StringArrayVar(&args.Dst, "to", nil, "Target image references")

return cmd
}

func runConvert(ctx context.Context, dockerCLI command.Cli, args convertArgs) error {
if len(args.Dst) == 0 {
return fmt.Errorf("No destination image specified")
}

var dstRefs []reference.NamedTagged
for _, dst := range args.Dst {
dstRef, err := reference.ParseNormalizedNamed(dst)
if err != nil {
return fmt.Errorf("invalid destination image reference: %s: %w", dst, err)
}

dstRef = reference.TagNameOnly(dstRef)
dstRefTagged := dstRef.(reference.NamedTagged)
dstRefs = append(dstRefs, dstRefTagged)
}

opts := imagetypes.ConvertOptions{
NoAttestations: args.NoAttestations,
OnlyAvailablePlatforms: args.OnlyAvailablePlatforms,
}

for _, platform := range args.Platforms {
p, err := platforms.Parse(platform)
if err != nil {
return err
}
opts.Platforms = append(opts.Platforms, p)
}

return dockerCLI.Client().ImageConvert(ctx, args.Src, dstRefs, opts)
}
3 changes: 2 additions & 1 deletion vendor.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ replace github.com/docker/docker => github.com/vvoland/moby v20.10.3-0.202405161

require (
dario.cat/mergo v1.0.0
github.com/containerd/containerd v1.7.15
github.com/containerd/containerd v1.7.14
github.com/containerd/platforms v0.1.1
github.com/creack/pty v1.1.21
github.com/distribution/reference v0.5.0
github.com/docker/distribution v2.8.3+incompatible
Expand Down
6 changes: 4 additions & 2 deletions vendor.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ github.com/cloudflare/cfssl v1.6.4/go.mod h1:8b3CQMxfWPAeom3zBnGJ6sd+G1NkL5TXqmD
github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h6jFvWxBdQXxjopDMZyH2UVceIRfR84bdzbkoKrsWNo=
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/containerd/containerd v1.7.15 h1:afEHXdil9iAm03BmhjzKyXnnEBtjaLJefdU7DV0IFes=
github.com/containerd/containerd v1.7.15/go.mod h1:ISzRRTMF8EXNpJlTzyr2XMhN+j9K302C21/+cr3kUnY=
github.com/containerd/containerd v1.7.14 h1:H/XLzbnGuenZEGK+v0RkwTdv2u1QFAruMe5N0GNPJwA=
github.com/containerd/containerd v1.7.14/go.mod h1:YMC9Qt5yzNqXx/fO4j/5yYVIHXSRrlB3H7sxkUTvspg=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
github.com/containerd/platforms v0.1.1 h1:gp0xXBoY+1CjH54gJDon0kBjIbK2C4XSX1BGwP5ptG0=
github.com/containerd/platforms v0.1.1/go.mod h1:XOM2BS6kN6gXafPLg80V6y/QUib+xoLyC3qVmHzibko=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/containerd/platforms/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions vendor/github.com/containerd/platforms/.golangci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

191 changes: 191 additions & 0 deletions vendor/github.com/containerd/platforms/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions vendor/github.com/containerd/platforms/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4b3b15f

Please sign in to comment.