-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.go
98 lines (93 loc) · 2.52 KB
/
autocomplete.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
92
93
94
95
96
97
98
package cmds
import (
"io"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/transmutate-io/atomicswap/internal/cmdutil"
"github.com/transmutate-io/atomicswap/internal/flagutil"
"github.com/transmutate-io/atomicswap/internal/flagutil/exitcodes"
)
var (
AutoCompleteCmd = &cobra.Command{
Use: "autocomplete",
Short: "generate shell autocomple",
Aliases: []string{"auto", "a"},
}
)
func init() {
fishCommand := &cobra.Command{
Use: "fish",
Short: "fish autocomplete script",
Aliases: []string{"f"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cmdAutoComplete(cmd, args, func(w io.Writer) error {
return cmd.Root().GenFishCompletion(w, flagutil.MustBool(cmd.Flags(), "desc"))
})
},
}
fishCommand.Flags().BoolP("desc", "d", false, "include descriptions")
cmdutil.AddCommands(AutoCompleteCmd, []*cobra.Command{
{
Use: "auto",
Short: "autocomplete script (try to guess the shell)",
Aliases: []string{"a"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cmdAutoComplete(cmd, args, nil)
},
},
fishCommand,
{
Use: "bash",
Short: "bash autocomplete script",
Aliases: []string{"b"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cmdAutoComplete(cmd, args, cmd.Root().GenBashCompletion)
},
},
{
Use: "zsh",
Short: "zsh autocomplete script",
Aliases: []string{"z"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cmdAutoComplete(cmd, args, cmd.Root().GenZshCompletion)
},
},
{
Use: "powershell",
Short: "powershell autocomplete script",
Aliases: []string{"p"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cmdAutoComplete(cmd, args, cmd.Root().GenPowerShellCompletion)
},
},
})
}
func cmdAutoComplete(cmd *cobra.Command, args []string, gen func(io.Writer) error) {
if gen == nil {
// try to guess shell
switch sh := filepath.Base(os.Getenv("SHELL")); sh {
case "bash":
gen = cmd.Root().GenBashCompletion
case "fish":
gen = func(w io.Writer) error { return cmd.Root().GenFishCompletion(w, true) }
case "zsh":
gen = cmd.Root().GenZshCompletion
default:
if os.Getenv("ComSpec") == "" {
cmdutil.ErrorExit(exitcodes.UnknownShell, "")
}
gen = cmd.Root().GenPowerShellCompletion
}
}
out, closeOut := flagutil.MustOpenOutput(cmd.Flags())
defer closeOut()
if err := gen(out); err != nil {
cmdutil.ErrorExit(exitcodes.UnknownShell, err)
}
}