~bigbes/ci-cacher

ref: 63e1653aea40a9dca690f49bd8017e72c867d61b ci-cacher/cmd/completion.go -rw-r--r-- 1.5 KiB
63e1653a — Eugene Blikh Add builds.sr.ht CI: unit, e2e, and tag-only publish 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cmd

import (
	"os"

	"github.com/spf13/cobra"
)

var completionCmd = &cobra.Command{
	Use:   "completion [bash|zsh|fish|powershell]",
	Short: "Generate shell completion scripts",
	Long: `Generate shell completion scripts for cacher.

To load completions:

Bash:
  $ source <(cacher completion bash)
  # To load completions for each session, execute once:
  # Linux:
  $ cacher completion bash > /etc/bash_completion.d/cacher
  # macOS:
  $ cacher completion bash > $(brew --prefix)/etc/bash_completion.d/cacher

Zsh:
  $ source <(cacher completion zsh)
  # To load completions for each session, execute once:
  $ cacher completion zsh > "${fpath[1]}/_cacher"

Fish:
  $ cacher completion fish | source
  # To load completions for each session, execute once:
  $ cacher completion fish > ~/.config/fish/completions/cacher.fish

PowerShell:
  PS> cacher completion powershell | Out-String | Invoke-Expression
`,
	DisableFlagsInUseLine: true,
	ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
	Args:                  cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
	RunE: func(cmd *cobra.Command, args []string) error {
		switch args[0] {
		case "bash":
			return rootCmd.GenBashCompletion(os.Stdout)
		case "zsh":
			return rootCmd.GenZshCompletion(os.Stdout)
		case "fish":
			return rootCmd.GenFishCompletion(os.Stdout, true)
		case "powershell":
			return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout)
		}
		return nil
	},
}

func init() {
	rootCmd.AddCommand(completionCmd)
}