This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
completion_bash.go
53 lines (46 loc) · 1.5 KB
/
completion_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
package cmd
import (
"os"
"github.com/bpicode/fritzctl/assert"
"github.com/bpicode/fritzctl/completion"
"github.com/mitchellh/cli"
)
type completionBashCommand struct {
cli *cli.CLI
}
func (cmd *completionBashCommand) Help() string {
return `Outputs fritzctl shell completion for the given shell (bash)
This depends on the bash-completion binary. Example installation instructions:
OS X:
$ brew install bash-completion
$ source $(brew --prefix)/etc/bash_completion
$ fritzctl completion bash > ~/.fritzctl-completion
$ source ~/.fritzctl-completion
Ubuntu:
$ apt-get install bash-completion
$ source /etc/bash-completion
$ source <(fritzctl completion bash)
Additionally, you may want to output completion to a file and source in your .bashrc`
}
func (cmd *completionBashCommand) Synopsis() string {
return "outputs fritzctl shell completion for the given shell (bash)"
}
func (cmd *completionBashCommand) Run(args []string) int {
commands := make([]string, len(cmd.cli.Commands))
for command := range cmd.cli.Commands {
commands = append(commands, command)
}
bash := completion.BourneAgain(cmd.cli.Name, commands)
bash.AddFlag("--help")
bash.Add("--version")
err := bash.Export(os.Stdout)
assert.NoError(err, "error exporting shell completion:", err)
return 0
}
// CompletionBash returns a factory creating commands for the
// bash completion bindings of this app.
func CompletionBash(c *cli.CLI) cli.CommandFactory {
return func() (cli.Command, error) {
return &completionBashCommand{cli: c}, nil
}
}