This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
completion.go
91 lines (71 loc) · 2.31 KB
/
completion.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cmd
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/spf13/cobra"
)
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: completionUsage(),
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
zshHead := "#compdef border0\ncompdef _border0 border0\n"
os.Stdout.Write([]byte(zshHead))
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletion(os.Stdout)
}
},
}
func completionUsage() string {
brewPrefix := "/usr/local"
if runtime.GOOS == "darwin" {
out, err := exec.Command("brew", "--prefix").CombinedOutput()
trimmed := strings.TrimSpace(string(out))
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: cannot execute `brew --prefix` %s, %s\n\n", trimmed, err)
} else {
brewPrefix = trimmed
}
}
return `To load completions:
Bash:
$ source <(border0 completion bash)
# To load completions for each session, execute once:
# Linux:
$ border0 completion bash > /etc/bash_completion.d/border0
# macOS:
$ border0 completion bash > ` + brewPrefix + `/etc/bash_completion.d/border0
Zsh:
# If shell completion is not already enabled in your environment,
# you will need to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# To load completions for each session, execute once:
$ border0 completion zsh > "${fpath[1]}/_border0"
# You will need to start a new shell for this setup to take effect.
fish:
$ border0 completion fish | source
# To load completions for each session, execute once:
$ border0 completion fish > ~/.config/fish/completions/border0.fish
PowerShell:
PS> border0 completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> border0 completion powershell > border0.ps1
# and source this file from your PowerShell profile.
`
}
func init() {
rootCmd.AddCommand(completionCmd)
}