Skip to content

Commit ec7036f

Browse files
authored
Add command line completion (#190)
1 parent db7d4c9 commit ec7036f

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

cmd/completion.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func init() {
11+
RootCmd.AddCommand(completionCmd)
12+
}
13+
14+
var completionCmd = &cobra.Command{
15+
Use: "completion [bash|zsh|fish|powershell]",
16+
Short: "Generate command line completion script",
17+
Long: `To load completions:
18+
Bash:
19+
$ source <(gopherciser completion bash)
20+
21+
# To load completions for each session, execute once:
22+
Linux:
23+
$ gopherciser completion bash > /etc/bash_completion.d/gopherciser
24+
MacOS:
25+
$ gopherciser_osx completion bash > /usr/local/etc/bash_completion.d/gopherciser_osx
26+
27+
Zsh:
28+
# If shell completion is not already enabled in your environment you will
29+
# need to enable it. You can execute the following once:
30+
31+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
32+
33+
# To load completions for each session, execute once:
34+
$ gopherciser completion zsh > "${fpath[1]}/_gopherciser"
35+
36+
# You might want to put the completion script (_gopherciser) at another
37+
# path within the fpath. To view the fpath, execute:
38+
$ echo $fpath
39+
40+
# You will need to start a new shell for this setup to take effect.
41+
42+
Fish:
43+
$ gopherciser completion fish | source
44+
45+
# To load completions for each session, execute once:
46+
$ gopherciser completion fish > ~/.config/fish/completions/gopherciser.fish
47+
48+
Powershell:
49+
PS> gopherciser.exe completion powershell | Out-String | Invoke-Expression
50+
51+
# To load completions for every new session, run:
52+
PS> gopherciser completion powershell > gopherciser.ps1
53+
# and source this file from your powershell profile.
54+
`,
55+
DisableFlagsInUseLine: true,
56+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
57+
Args: cobra.ExactValidArgs(1),
58+
Run: func(cmd *cobra.Command, args []string) {
59+
var err error
60+
switch args[0] {
61+
case "bash":
62+
err = cmd.Root().GenBashCompletion(os.Stdout)
63+
case "zsh":
64+
err = cmd.Root().GenZshCompletion(os.Stdout)
65+
case "fish":
66+
err = cmd.Root().GenFishCompletion(os.Stdout, true)
67+
case "powershell":
68+
err = cmd.Root().GenPowerShellCompletion(os.Stdout)
69+
}
70+
if err != nil {
71+
fmt.Fprintln(os.Stderr, err)
72+
os.Exit(1)
73+
}
74+
},
75+
}

cmd/execute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func (err SummaryTypeError) Error() string {
9696
var executeCmd = &cobra.Command{
9797
Use: "execute",
9898
Aliases: []string{"x"},
99-
Short: "execute exerciser scenario towards a sense installation",
100-
Long: `execute exerciser scenario towards a sense installation`,
99+
Short: "Execute gopherciser scenario towards a sense environment",
100+
Long: `Execute gopherciser scenario towards a sense environment.`,
101101
Run: func(cmd *cobra.Command, args []string) {
102102
defer func() {
103103
var panicErr error = nil

docs/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ Commands:
3636
* `objdef` (or `od`): Export and validate object definitions files.
3737
* `script` (or `s`): Execute script command.
3838
* `version` (or `ver`): Show the version information.
39+
* `completion` : Generate command line completion script.
3940

4041
Flags:
4142

4243
* `-h`, `--help`: Show the help for a command (`gopherciser [command] --help`).
4344

45+
#### Completion command
46+
`gopherciser completion [bash|zsh|fish|powershell]`
47+
48+
Run `gopherciser completion --help` and follow the instructions to install command line completion for your shell.
49+
50+
4451
#### Execute command
4552

4653
`gopherciser execute [flags]`

0 commit comments

Comments
 (0)