Skip to content

Commit 9515b7c

Browse files
authored
feat: implement completion command (#416)
1 parent 5fed272 commit 9515b7c

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

cmd/completion.go

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

0 commit comments

Comments
 (0)