Skip to content

Commit

Permalink
Add shell completion with cobra
Browse files Browse the repository at this point in the history
Allow automatic generation for shell completion scripts
with the internal cobra functions (requires v1.0.0+).

This should replace the handwritten completion scripts
and even adds support for fish. With this approach it is
less likley that completions and code are out of sync.

We can now create the scripts with
- podman completion bash
- podman completion zsh
- podman completion fish

To test the completion run:
source <(podman completion bash)

The same works for podman-remote and podman --remote and
it will complete your remote containers/images with
the correct endpoints values from --url/--connection.

The completion logic is written in go and provided by the
cobra library. The completion functions lives in
`cmd/podman/completion/completion.go`.

The unit test at cmd/podman/shell_completion_test.go checks
if each command and flag has an autocompletion function set.
This prevents that commands and flags have no shell completion set.

This commit does not replace the current autocompletion scripts.

Closes #6440

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
  • Loading branch information
Luap99 committed Nov 12, 2020
1 parent df4bf5c commit b5d1d89
Show file tree
Hide file tree
Showing 124 changed files with 3,078 additions and 966 deletions.
15 changes: 10 additions & 5 deletions cmd/podman/auto-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/containers/common/pkg/auth"
"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/errorhandling"
Expand All @@ -20,10 +21,11 @@ var (
or similar units that create new containers in order to run the updated images.
Please refer to the podman-auto-update(1) man page for details.`
autoUpdateCommand = &cobra.Command{
Use: "auto-update [options]",
Short: "Auto update containers according to their auto-update policy",
Long: autoUpdateDescription,
RunE: autoUpdate,
Use: "auto-update [options]",
Short: "Auto update containers according to their auto-update policy",
Long: autoUpdateDescription,
RunE: autoUpdate,
ValidArgsFunction: completion.AutocompleteNone,
Example: `podman auto-update
podman auto-update --authfile ~/authfile.json`,
}
Expand All @@ -36,7 +38,10 @@ func init() {
})

flags := autoUpdateCommand.Flags()
flags.StringVar(&autoUpdateOptions.Authfile, "authfile", auth.GetDefaultAuthFile(), "Path to the authentication file. Use REGISTRY_AUTH_FILE environment variable to override")

authfileFlagName := "authfile"
flags.StringVar(&autoUpdateOptions.Authfile, authfileFlagName, auth.GetDefaultAuthFile(), "Path to the authentication file. Use REGISTRY_AUTH_FILE environment variable to override")
_ = autoUpdateCommand.RegisterFlagCompletionFunc(authfileFlagName, completion.AutocompleteDefault)
}

func autoUpdate(cmd *cobra.Command, args []string) error {
Expand Down

0 comments on commit b5d1d89

Please sign in to comment.