From ce1aebcc307622fe000f4d201727fe1cf5b874cb Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 8 Oct 2024 13:09:50 +0200 Subject: [PATCH 1/3] cli/command/completion: add Platforms Add a utility for completing platform strings. Platforms offers completion for platform-strings. It provides a non-exhaustive list of platforms to be used for completion. Platform-strings are based on [runtime.GOOS] and [runtime.GOARCH], but with (optional) variants added. A list of recognised os/arch combinations from the Go runtime can be obtained through "go tool dist list". Some noteworthy exclusions from this list: - arm64 images ("windows/arm64", "windows/arm64/v8") do not yet exist for windows. - we don't (yet) include `os-variant` for completion (as can be used for Windows images) - we don't (yet) include platforms for which we don't build binaries, such as BSD platforms (freebsd, netbsd, openbsd), android, macOS (darwin). - we currently exclude architectures that may have unofficial builds, but don't have wide adoption (and no support), such as loong64, mipsXXX, ppc64 (non-le) to prevent confusion. Signed-off-by: Sebastiaan van Stijn --- cli/command/completion/functions.go | 44 ++++++++++++++++++++++++ cli/command/completion/functions_test.go | 15 ++++++++ 2 files changed, 59 insertions(+) create mode 100644 cli/command/completion/functions_test.go diff --git a/cli/command/completion/functions.go b/cli/command/completion/functions.go index 30acf1e96e..b217ec54b0 100644 --- a/cli/command/completion/functions.go +++ b/cli/command/completion/functions.go @@ -145,3 +145,47 @@ func FileNames(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCom func NoComplete(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveNoFileComp } + +var commonPlatforms = []string{ + "linux", + "linux/386", + "linux/amd64", + "linux/arm", + "linux/arm/v5", + "linux/arm/v6", + "linux/arm/v7", + "linux/arm64", + "linux/arm64/v8", + + // IBM power and z platforms + "linux/ppc64le", + "linux/s390x", + + // Not yet supported + "linux/riscv64", + + "windows", + "windows/amd64", + + "wasip1", + "wasip1/wasm", +} + +// Platforms offers completion for platform-strings. It provides a non-exhaustive +// list of platforms to be used for completion. Platform-strings are based on +// [runtime.GOOS] and [runtime.GOARCH], but with (optional) variants added. A +// list of recognised os/arch combinations from the Go runtime can be obtained +// through "go tool dist list". +// +// Some noteworthy exclusions from this list: +// +// - arm64 images ("windows/arm64", "windows/arm64/v8") do not yet exist for windows. +// - we don't (yet) include `os-variant` for completion (as can be used for Windows images) +// - we don't (yet) include platforms for which we don't build binaries, such as +// BSD platforms (freebsd, netbsd, openbsd), android, macOS (darwin). +// - we currently exclude architectures that may have unofficial builds, +// but don't have wide adoption (and no support), such as loong64, mipsXXX, +// ppc64 (non-le) to prevent confusion. +func Platforms(_ *cobra.Command, _ []string, _ string) (platforms []string, _ cobra.ShellCompDirective) { + return commonPlatforms, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cli/command/completion/functions_test.go b/cli/command/completion/functions_test.go new file mode 100644 index 0000000000..a4bb6f5438 --- /dev/null +++ b/cli/command/completion/functions_test.go @@ -0,0 +1,15 @@ +package completion + +import ( + "testing" + + "github.com/spf13/cobra" + "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" +) + +func TestCompletePlatforms(t *testing.T) { + values, directives := Platforms(nil, nil, "") + assert.Check(t, is.Equal(directives&cobra.ShellCompDirectiveNoFileComp, cobra.ShellCompDirectiveNoFileComp), "Should not perform file completion") + assert.Check(t, is.DeepEqual(values, commonPlatforms)) +} From 8c7f713db609b84475b7033c6dd946c1d6b32afa Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 8 Oct 2024 13:13:32 +0200 Subject: [PATCH 2/3] cli/command/container: add shell completion for --platform flags With this patch, completion is provided for `--platform` flags: docker run --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 Signed-off-by: Sebastiaan van Stijn --- cli/command/container/create.go | 1 + cli/command/container/run.go | 1 + 2 files changed, 2 insertions(+) diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 978e1465d3..31ddeaad01 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -83,6 +83,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command { _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 2767d1f488..a3fc5f983a 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -74,6 +74,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) From d42cf96e151972a04cc22eebce823f9578d61833 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 8 Oct 2024 13:15:28 +0200 Subject: [PATCH 3/3] 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 }