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 and powershell(if needed?)

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

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

I added the main completion for all commands
but this still could be improved with
better custom completion functions.
see: cmd/podman/completion/completion.go

Flag completion is for now only added to
podman generate systemd --restart-policy
for now
just to show how to implement this on flags

This is still WIP

Closes #6440

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
  • Loading branch information
Luap99 committed May 31, 2020
1 parent 5fb9f00 commit f5017cf
Show file tree
Hide file tree
Showing 111 changed files with 1,391 additions and 546 deletions.
10 changes: 6 additions & 4 deletions cmd/podman/auto-update.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/containers/common/pkg/auth"
"github.com/containers/libpod/cmd/podman/completion"
"github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/errorhandling"
Expand All @@ -18,10 +19,11 @@ var (
Auto-update policies are specified with the "io.containers.autoupdate" label.
Note that this command is experimental. Please refer to the podman-auto-update(1) man page for details.`
autoUpdateCommand = &cobra.Command{
Use: "auto-update [flags]",
Short: "Auto update containers according to their auto-update policy",
Long: autoUpdateDescription,
RunE: autoUpdate,
Use: "auto-update [flags]",
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 Down
64 changes: 64 additions & 0 deletions cmd/podman/completion/bash.go
@@ -0,0 +1,64 @@
package completion

import (
"os"

"github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

const (
bashAutoCompletionDescription = `Creates bash completion and writes to stdout
To load in your current session run
source <(podman completion bash)
To make it available in all your bash session, add this to your ~/.bashrc file:
echo 'source <(podman completion bash)' >>~/.bashrc
As an alternative, if 'bash-completion' is installed on your system, you can add it in:
/etc/bash_completion.d/{your_file_name}`
)

var (
bashCmd = &cobra.Command{
Use: "bash",
Short: "Creates bash completion",
Long: bashAutoCompletionDescription,
RunE: bashCompletion,
ValidArgsFunction: AutocompleteNone,
Example: "podman completion bash",
}
)

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: bashCmd,
Parent: completionCmd,
})
flags := bashCmd.Flags()
flags.StringVarP(&completionOptions.File, "file", "f", "", "Output the completion to file rather then stdout")
}

//Generate the bash completion with the internal cobra function.
func bashCompletion(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.Errorf("`%s` takes no arguments", cmd.CommandPath())
}

var err error
if completionOptions.File == "" {
err = completionCmd.Root().GenBashCompletion(os.Stdout)
} else {
err = completionCmd.Root().GenBashCompletionFile(completionOptions.File)
}

if err != nil {
return err
}

return nil
}

0 comments on commit f5017cf

Please sign in to comment.