Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
111 changed files
with
1,391 additions
and
546 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.