Skip to content

Commit

Permalink
Enable bash completion for podman commands
Browse files Browse the repository at this point in the history
I added a command completion that generates
the bash script that would enable the shell
completion, using the cobra function GenBashCompletion.

Closes containers#3878

Signed-off-by: Neville Cain <[email protected]>
  • Loading branch information
NevilleC committed Jan 8, 2020
1 parent c41fd09 commit 4ce5e0e
Show file tree
Hide file tree
Showing 6 changed files with 10,688 additions and 3,363 deletions.
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ Well, you can now create your own branch, apply changes on it, and then submitti

For further reading about branching [you can read this document](https://herve.beraud.io/containers/linux/podman/isolate/environment/2019/02/06/how-to-hack-on-podman.html).

### Adding/Updating a new command

When you add/update a new command, dont't forget to run 'make completion' to update
the bash completion script.

There is no need to update zsh completion script as it autogenerates completion.

## Submitting Pull Requests

No Pull Request (PR) is too small! Typos, additional comments in the code,
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ CROSS_BUILD_TARGETS := \

all: binaries docs

completion: binaries
bin/podman completion bash > completions/bash/podman

default: help

define PRINT_HELP_PYSCRIPT
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func buildCmd(c *cliconfig.BuildValues) error {
}

if len(cliArgs) > 0 {
// The context directory could be a URL. Try to handle that.
// The context directory could be a URL. Try to handle that.
tempDir, subDir, err := imagebuildah.TempDirForURL("", "buildah", cliArgs[0])
if err != nil {
return errors.Wrapf(err, "error prepping temporary context directory")
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
// implemented.
var mainCommands = []*cobra.Command{
_attachCommand,
_bashCompletionCommand,
_buildCommand,
_commitCommand,
_diffCommand,
Expand Down
49 changes: 49 additions & 0 deletions cmd/podman/shell_auto_completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"os"

"github.com/spf13/cobra"
)

const (
bashAutoCompletionDescription = `To load completion run
podman completion bash >>~/.bashrc
Reload your shell.
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 _bashCompletionCommand = &cobra.Command{
Use: "completion",
Short: "Generates shell scripts for auto-completion",
// We do not intend to show this command to the user so
// we marked it as hidden. We should use them by using
// the "make completion" target to update the shell scripts
// enabling the auto-completion for podman.
Hidden: true,
Example: `podman completion --help`,
}

func init() {
_bashCompletionCommand.AddCommand(
&cobra.Command{
Use: "bash",
Short: "generate auto-completion for bash",
Long: bashAutoCompletionDescription,
RunE: func(cmd *cobra.Command, args []string) error {
// Writing the shell script to stdout allows the most flexible use
// as the user can redirect the outputt where it needs it.
return rootCmd.GenBashCompletion(os.Stdout)
},
Example: `podman completion bash >>~/.bashrc`,
},
)
}
Loading

0 comments on commit 4ce5e0e

Please sign in to comment.