From d78bc8278206ed6a58d92582ea269c74dcf8549a Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 18 May 2022 15:56:57 +0200 Subject: [PATCH] shell completion: add completion for transports names Make sure skopeo copy/inspect/delete show the transport names when shell completion is used to not regress compared to the old bash completion script. In theory I would highly recommend to set completion functions for every flag and command. This can be ensured with a test like this: https://github.com/containers/podman/blob/main/cmd/podman/shell_completion_test.go But this is a lot of work to get right and I am neither a skopeo user or maintainer so I am missing a lot of context for most options. I think this would be better handled by a person who knows skopeo better. Normally options should either use AutocompleteNone() or AutocompleteDefault() from c/common/pkg/completion. Even better would be to add custom completion functions for arguments that only accept fixed values, see AutocompleteSupportedTransports() in this commit. Signed-off-by: Paul Holzinger --- cmd/skopeo/completions.go | 16 ++++++++++++++++ cmd/skopeo/copy.go | 5 +++-- cmd/skopeo/delete.go | 5 +++-- cmd/skopeo/inspect.go | 1 + 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 cmd/skopeo/completions.go diff --git a/cmd/skopeo/completions.go b/cmd/skopeo/completions.go new file mode 100644 index 00000000..fc323a19 --- /dev/null +++ b/cmd/skopeo/completions.go @@ -0,0 +1,16 @@ +package main + +import ( + "github.com/containers/image/v5/transports" + "github.com/spf13/cobra" +) + +// autocompleteSupportedTransports list all supported transports with the colon suffix. +func autocompleteSupportedTransports(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + tps := transports.ListNames() + suggestions := make([]string, 0, len(tps)) + for _, tp := range tps { + suggestions = append(suggestions, tp+":") + } + return suggestions, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/skopeo/copy.go b/cmd/skopeo/copy.go index e6d29b67..84298fbd 100644 --- a/cmd/skopeo/copy.go +++ b/cmd/skopeo/copy.go @@ -64,8 +64,9 @@ Supported transports: See skopeo(1) section "IMAGE NAMES" for the expected format `, strings.Join(transports.ListNames(), ", ")), - RunE: commandAction(opts.run), - Example: `skopeo copy docker://quay.io/skopeo/stable:latest docker://registry.example.com/skopeo:latest`, + RunE: commandAction(opts.run), + Example: `skopeo copy docker://quay.io/skopeo/stable:latest docker://registry.example.com/skopeo:latest`, + ValidArgsFunction: autocompleteSupportedTransports, } adjustUsage(cmd) flags := cmd.Flags() diff --git a/cmd/skopeo/delete.go b/cmd/skopeo/delete.go index b02fd9bf..f1dbeb4b 100644 --- a/cmd/skopeo/delete.go +++ b/cmd/skopeo/delete.go @@ -35,8 +35,9 @@ Supported transports: %s See skopeo(1) section "IMAGE NAMES" for the expected format `, strings.Join(transports.ListNames(), ", ")), - RunE: commandAction(opts.run), - Example: `skopeo delete docker://registry.example.com/example/pause:latest`, + RunE: commandAction(opts.run), + Example: `skopeo delete docker://registry.example.com/example/pause:latest`, + ValidArgsFunction: autocompleteSupportedTransports, } adjustUsage(cmd) flags := cmd.Flags() diff --git a/cmd/skopeo/inspect.go b/cmd/skopeo/inspect.go index d45b884e..e3c6e4d2 100644 --- a/cmd/skopeo/inspect.go +++ b/cmd/skopeo/inspect.go @@ -55,6 +55,7 @@ See skopeo(1) section "IMAGE NAMES" for the expected format Example: `skopeo inspect docker://registry.fedoraproject.org/fedora skopeo inspect --config docker://docker.io/alpine skopeo inspect --format "Name: {{.Name}} Digest: {{.Digest}}" docker://registry.access.redhat.com/ubi8`, + ValidArgsFunction: autocompleteSupportedTransports, } adjustUsage(cmd) flags := cmd.Flags()