-
Notifications
You must be signed in to change notification settings - Fork 1
/
opts.go
179 lines (156 loc) · 4.84 KB
/
opts.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package grpctl
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"google.golang.org/protobuf/reflect/protoreflect"
)
// WithContextFunc will add commands to the cobra command through the file descriptors provided.
func WithFileDescriptors(descriptors ...protoreflect.FileDescriptor) CommandOption {
return func(cmd *cobra.Command) error {
err := CommandFromFileDescriptors(cmd, descriptors...)
if err != nil {
return err
}
return nil
}
}
// WithContextFunc will modify the context before the main command is run but not in the completion stage.
func WithContextFunc(f func(context.Context, *cobra.Command) (context.Context, error)) CommandOption {
return func(cmd *cobra.Command) error {
existingPreRun := cmd.PersistentPreRunE
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if existingPreRun != nil {
err := existingPreRun(cmd, args)
if err != nil {
return err
}
}
ctx, err := f(cmd.Root().Context(), cmd)
if err != nil {
return err
}
cmd.Root().SetContext(ctx)
return nil
}
return nil
}
}
// WithContextDescriptorFunc will modify the context before the main command is run but not in the completion stage.
func WithContextDescriptorFunc(f func(context.Context, *cobra.Command, protoreflect.MethodDescriptor) (context.Context, error)) CommandOption {
return func(cmd *cobra.Command) error {
existingPreRun := cmd.PersistentPreRunE
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if existingPreRun != nil {
err := existingPreRun(cmd, args)
if err != nil {
return err
}
}
a := cmd.Root().Context().Value(methodDescriptorKey{})
method, ok := a.(protoreflect.MethodDescriptor)
if !ok {
return nil
}
ctx, err := f(cmd.Root().Context(), cmd, method)
if err != nil {
return err
}
cmd.Root().SetContext(ctx)
return nil
}
return nil
}
}
// WithArgs will set the args of the command as args[1:].
func WithArgs(args []string) CommandOption {
return func(cmd *cobra.Command) error {
cmd.SetArgs(args[1:])
return nil
}
}
// WithReflection will enable grpc reflection on the command. Use this as an alternative to WithFileDescriptors.
func WithReflection(args []string) CommandOption {
return func(cmd *cobra.Command) error {
var err error
cmd.ValidArgsFunction = func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
fds, err2 := reflectFileDesc(args)
if err2 != nil {
err = err2
return nil, cobra.ShellCompDirectiveNoFileComp
}
var opts []string
err2 = CommandFromFileDescriptors(cmd, fds...)
if err2 != nil {
err = err2
return nil, cobra.ShellCompDirectiveNoFileComp
}
return opts, cobra.ShellCompDirectiveNoFileComp
}
fds, err := reflectFileDesc(args[1:])
if err != nil {
return err
}
if err = persistentFlags(cmd, ""); err != nil {
return err
}
err = CommandFromFileDescriptors(cmd, fds...)
if err != nil {
return err
}
return nil
}
}
func WithCompletion() CommandOption {
return func(command *cobra.Command) error {
cmd := &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: fmt.Sprintf(`To load completions:
Bash:
$ source <(%[1]s completion bash)
# To load completions for each session, execute once:
# Linux:
$ %[1]s completion bash > /etc/bash_completion.d/%[1]s
# macOS:
$ %[1]s completion bash > /usr/local/etc/bash_completion.d/%[1]s
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:
$ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
# You will need to start a new shell for this setup to take effect.
fish:
$ %[1]s completion fish | source
# To load completions for each session, execute once:
$ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
PowerShell:
PS> %[1]s completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> %[1]s completion powershell > %[1]s.ps1
# and source this file from your PowerShell profile.
`, command.Root().Name()),
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
switch args[0] {
case "bash":
err = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
err = cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}
return err
},
}
command.AddCommand(cmd)
return nil
}
}