Skip to content

Commit da538ee

Browse files
authored
Merge pull request #63 from developer-guy/main
feat: add completion cmd
2 parents 79f631a + 7fc5003 commit da538ee

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

cmd/completion.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"os"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var completionCmd = &cobra.Command{
26+
Use: "completion [bash|zsh|fish|powershell]",
27+
Short: "Generate completion script",
28+
Long: `To load completions:
29+
Bash:
30+
$ source <(depstat completion bash)
31+
# To load completions for each session, execute once:
32+
# Linux:
33+
$ depstat completion bash > /etc/bash_completion.d/depstat
34+
# macOS:
35+
$ depstat completion bash > /usr/local/etc/bash_completion.d/depstat
36+
Zsh:
37+
# If shell completion is not already enabled in your environment,
38+
# you will need to enable it. You can execute the following once:
39+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
40+
# To load completions for each session, execute once:
41+
$ depstat completion zsh > "${fpath[1]}/_depstat"
42+
# You will need to start a new shell for this setup to take effect.
43+
fish:
44+
$ depstat completion fish | source
45+
# To load completions for each session, execute once:
46+
$ depstat completion fish > ~/.config/fish/completions/depstat.fish
47+
PowerShell:
48+
PS> depstat completion powershell | Out-String | Invoke-Expression
49+
# To load completions for every new session, run:
50+
PS> depstat completion powershell > depstat.ps1
51+
# and source this file from your PowerShell profile.
52+
`,
53+
DisableFlagsInUseLine: true,
54+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
55+
Args: cobra.ExactValidArgs(1),
56+
Run: func(cmd *cobra.Command, args []string) {
57+
switch args[0] {
58+
case "bash":
59+
_ = cmd.Root().GenBashCompletion(os.Stdout)
60+
case "zsh":
61+
_ = cmd.Root().GenZshCompletion(os.Stdout)
62+
case "fish":
63+
_ = cmd.Root().GenFishCompletion(os.Stdout, true)
64+
case "powershell":
65+
_ = cmd.Root().GenPowerShellCompletion(os.Stdout)
66+
}
67+
},
68+
}
69+
70+
func init() {
71+
rootCmd.AddCommand(completionCmd)
72+
}

0 commit comments

Comments
 (0)