cmd/completion.go   A
last analyzed

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 33
dl 0
loc 83
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2023 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 all
12
 * copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
 * OTHER DEALINGS IN THE SOFTWARE.
22
 */
23
24
package cmd
25
26
import (
27
	"fmt"
28
	"os"
29
30
	"github.com/spf13/cobra"
31
)
32
33
// completionCmd represents the completion command
34
var completionCmd = &cobra.Command{
35
	Use:   "completion [bash|zsh|fish|powershell]",
36
	Short: "Generate completion script",
37
	Long: fmt.Sprintf(`To load completions:
38
39
Bash:
40
41
  $ source <(%[1]s completion bash)
42
43
  # To load completions for each session, execute once:
44
  # Linux:
45
  $ %[1]s completion bash > /etc/bash_completion.d/%[1]s
46
  # macOS:
47
  $ %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s
48
49
Zsh:
50
51
  # If shell completion is not already enabled in your environment,
52
  # you will need to enable it.  You can execute the following once:
53
54
  $ echo "autoload -U compinit; compinit" >> ~/.zshrc
55
56
  # To load completions for each session, execute once:
57
  $ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
58
59
  # You will need to start a new shell for this setup to take effect.
60
61
fish:
62
63
  $ %[1]s completion fish | source
64
65
  # To load completions for each session, execute once:
66
  $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
67
68
PowerShell:
69
70
  PS> %[1]s completion powershell | Out-String | Invoke-Expression
71
72
  # To load completions for every new session, run:
73
  PS> %[1]s completion powershell > %[1]s.ps1
74
  # and source this file from your PowerShell profile.
75
`, rootCmd.Root().Name()),
76
	DisableFlagsInUseLine: true,
77
	ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
78
	Args:                  cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
79
	Run: func(cmd *cobra.Command, args []string) {
80
		switch args[0] {
81
		case "bash":
82
			err := cmd.Root().GenBashCompletion(os.Stdout)
83
			if err != nil {
84
				return
85
			}
86
		case "zsh":
87
			err := cmd.Root().GenZshCompletion(os.Stdout)
88
			if err != nil {
89
				return
90
			}
91
		case "fish":
92
			err := cmd.Root().GenFishCompletion(os.Stdout, true)
93
			if err != nil {
94
				return
95
			}
96
		case "powershell":
97
			err := cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
98
			if err != nil {
99
				return
100
			}
101
		}
102
	},
103
}
104
105
func init() {
106
	rootCmd.AddCommand(completionCmd)
107
}
108