summaryrefslogtreecommitdiff
path: root/cmd/podman/completion/completion.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-11-13 16:46:51 +0100
committerGitHub <noreply@github.com>2020-11-13 16:46:51 +0100
commit2993e97dec9d998d2eca7c5aee918b1429596a85 (patch)
tree9bdb3b5c766d913a8d1980ad017276e1f1a51b7c /cmd/podman/completion/completion.go
parent6d9d9fee30b5982858d51a666f0c335ba47323a0 (diff)
parentae3816614de1c2a0c9ab9cd05afebc5b1dda6429 (diff)
downloadpodman-2993e97dec9d998d2eca7c5aee918b1429596a85.tar.gz
podman-2993e97dec9d998d2eca7c5aee918b1429596a85.tar.bz2
podman-2993e97dec9d998d2eca7c5aee918b1429596a85.zip
Merge pull request #6442 from Luap99/podman-autocomplete
Shell completion
Diffstat (limited to 'cmd/podman/completion/completion.go')
-rw-r--r--cmd/podman/completion/completion.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/cmd/podman/completion/completion.go b/cmd/podman/completion/completion.go
new file mode 100644
index 000000000..ead8d1f05
--- /dev/null
+++ b/cmd/podman/completion/completion.go
@@ -0,0 +1,93 @@
+package completion
+
+import (
+ "fmt"
+ "io"
+ "os"
+ "strings"
+
+ commonComp "github.com/containers/common/pkg/completion"
+ "github.com/containers/podman/v2/cmd/podman/registry"
+ "github.com/containers/podman/v2/pkg/domain/entities"
+ "github.com/spf13/cobra"
+)
+
+const (
+ completionDescription = `Generate shell autocompletions.
+ Valid arguments are bash, zsh, and fish.
+ Please refer to the man page to see how you can load these completions.`
+)
+
+var (
+ file string
+ noDesc bool
+ shells = []string{"bash", "zsh", "fish"}
+ completionCmd = &cobra.Command{
+ Use: fmt.Sprintf("completion [options] {%s}", strings.Join(shells, "|")),
+ Short: "Generate shell autocompletions",
+ Long: completionDescription,
+ ValidArgs: shells,
+ Args: cobra.ExactValidArgs(1),
+ RunE: completion,
+ Example: `podman completion bash
+ podman completion zsh -f _podman
+ podman completion fish --no-desc`,
+ //dont show this command to users
+ Hidden: true,
+ }
+)
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
+ Command: completionCmd,
+ })
+ flags := completionCmd.Flags()
+ fileFlagName := "file"
+ flags.StringVarP(&file, fileFlagName, "f", "", "Output the completion to file rather than stdout.")
+ _ = completionCmd.RegisterFlagCompletionFunc(fileFlagName, commonComp.AutocompleteDefault)
+
+ flags.BoolVar(&noDesc, "no-desc", false, "Don't include descriptions in the completion output.")
+}
+
+func completion(cmd *cobra.Command, args []string) error {
+ var w io.Writer
+
+ if file != "" {
+ file, err := os.Create(file)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+ w = file
+ } else {
+ w = os.Stdout
+ }
+
+ var err error
+ switch args[0] {
+ case "bash":
+ if noDesc {
+ err = cmd.Root().GenBashCompletion(w)
+ } else {
+ err = cmd.Root().GenBashCompletionWithDesc(w)
+ }
+ case "zsh":
+ if noDesc {
+ err = cmd.Root().GenZshCompletionNoDesc(w)
+ } else {
+ err = cmd.Root().GenZshCompletion(w)
+ }
+ case "fish":
+ err = cmd.Root().GenFishCompletion(w, !noDesc)
+ }
+
+ if err != nil {
+ return err
+ }
+
+ _, err = io.WriteString(w, fmt.Sprintf(
+ "\n# This file is generated with %q; see: podman-completion(1)\n", cmd.CommandPath(),
+ ))
+ return err
+}