From d42cf96e151972a04cc22eebce823f9578d61833 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 8 Oct 2024 13:15:28 +0200 Subject: [PATCH] cli/command/image: add shell completion for --platform flags With this patch, completion is provided for `--platform` flags: docker pull --platform linux linux/amd64 linux/arm/v5 linux/arm/v7 linux/arm64/v8 linux/riscv64 wasip1 windows linux/386 linux/arm linux/arm/v6 linux/arm64 linux/ppc64le linux/s390x wasip1/wasm windows/amd64 Note that `docker buildx build` (with BuildKit) does not yet provide completion; it's provided through buildx, and uses a different format (accepting multiple comma-separated platforms). Interestingly, tab-completion for `docker build` currently uses completion for non-buildkit, and has some other issues that may have to be looked into. Signed-off-by: Sebastiaan van Stijn --- cli/command/image/build.go | 3 +++ cli/command/image/import.go | 2 ++ cli/command/image/pull.go | 2 ++ cli/command/image/push.go | 2 ++ 4 files changed, 9 insertions(+) diff --git a/cli/command/image/build.go b/cli/command/image/build.go index cb5292be85..4a258646dc 100644 --- a/cli/command/image/build.go +++ b/cli/command/image/build.go @@ -18,6 +18,7 @@ import ( "github.com/docker/cli-docs-tool/annotation" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" + "github.com/docker/cli/cli/command/completion" "github.com/docker/cli/cli/command/image/build" "github.com/docker/cli/opts" "github.com/docker/docker/api" @@ -159,6 +160,8 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command { flags.SetAnnotation("squash", "experimental", nil) flags.SetAnnotation("squash", "version", []string{"1.25"}) + _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) + return cmd } diff --git a/cli/command/image/import.go b/cli/command/image/import.go index 9920b5e0cd..f6ad73d9ba 100644 --- a/cli/command/image/import.go +++ b/cli/command/image/import.go @@ -7,6 +7,7 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" + "github.com/docker/cli/cli/command/completion" dockeropts "github.com/docker/cli/opts" "github.com/docker/docker/api/types/image" "github.com/docker/docker/pkg/jsonmessage" @@ -47,6 +48,7 @@ func NewImportCommand(dockerCli command.Cli) *cobra.Command { flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image") flags.StringVarP(&options.message, "message", "m", "", "Set commit message for imported image") command.AddPlatformFlag(flags, &options.platform) + _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) return cmd } diff --git a/cli/command/image/pull.go b/cli/command/image/pull.go index 9ec9771e28..93388253f7 100644 --- a/cli/command/image/pull.go +++ b/cli/command/image/pull.go @@ -50,6 +50,8 @@ func NewPullCommand(dockerCli command.Cli) *cobra.Command { command.AddPlatformFlag(flags, &opts.platform) command.AddTrustVerificationFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) + _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) + return cmd } diff --git a/cli/command/image/push.go b/cli/command/image/push.go index d28fd93e7a..92ef1a159a 100644 --- a/cli/command/image/push.go +++ b/cli/command/image/push.go @@ -68,6 +68,8 @@ Image index won't be pushed, meaning that other manifests, including attestation 'os[/arch[/variant]]': Explicit platform (eg. linux/amd64)`) flags.SetAnnotation("platform", "version", []string{"1.46"}) + _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) + return cmd }