Passed
Push — development ( db54e3...2d43b6 )
by Clive
01:27
created

cmd/completion.go   A

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 25
dl 0
loc 71
rs 10
c 0
b 0
f 0
1
/*
2
Copyright © 2022 Clive Walkden <[email protected]>
3
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
10
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
13
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
THE SOFTWARE.
21
*/
22
package cmd
23
24
import (
25
	"fmt"
26
	"os"
27
28
	"github.com/spf13/cobra"
29
)
30
31
// completionCmd represents the completion command
32
var completionCmd = &cobra.Command{
33
	Use:   "completion [bash|zsh|fish|powershell]",
34
	Short: "Generate completion script",
35
	Long: fmt.Sprintf(`To load completions:
36
37
Bash:
38
39
  $ source <(%[1]s completion bash)
40
41
  # To load completions for each session, execute once:
42
  # Linux:
43
  $ %[1]s completion bash > /etc/bash_completion.d/%[1]s
44
  # macOS:
45
  $ %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s
46
47
Zsh:
48
49
  # If shell completion is not already enabled in your environment,
50
  # you will need to enable it.  You can execute the following once:
51
52
  $ echo "autoload -U compinit; compinit" >> ~/.zshrc
53
54
  # To load completions for each session, execute once:
55
  $ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
56
57
  # You will need to start a new shell for this setup to take effect.
58
59
fish:
60
61
  $ %[1]s completion fish | source
62
63
  # To load completions for each session, execute once:
64
  $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
65
66
PowerShell:
67
68
  PS> %[1]s completion powershell | Out-String | Invoke-Expression
69
70
  # To load completions for every new session, run:
71
  PS> %[1]s completion powershell > %[1]s.ps1
72
  # and source this file from your PowerShell profile.
73
`, rootCmd.Root().Name()),
74
	DisableFlagsInUseLine: true,
75
	ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
76
	Args:                  cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
77
	Run: func(cmd *cobra.Command, args []string) {
78
		switch args[0] {
79
		case "bash":
80
			cmd.Root().GenBashCompletion(os.Stdout)
81
		case "zsh":
82
			cmd.Root().GenZshCompletion(os.Stdout)
83
		case "fish":
84
			cmd.Root().GenFishCompletion(os.Stdout, true)
85
		case "powershell":
86
			cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
87
		}
88
	},
89
}
90
91
func init() {
92
	rootCmd.AddCommand(completionCmd)
93
}
94