-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Paweł Gronowski <[email protected]>
- Loading branch information
Showing
23 changed files
with
1,580 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.