-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
bash.go
64 lines (52 loc) · 1.68 KB
/
bash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
}