forked from joshcarp/grpctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopts.go
117 lines (110 loc) · 3.03 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
package grpctl
import (
"context"
"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) GrptlOption {
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)) GrptlOption {
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
}
}
custCtx, ctx, ok := getContext(cmd)
if !ok {
return nil
}
ctx, err := f(ctx, cmd)
if err != nil {
return err
}
custCtx.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)) GrptlOption {
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
}
}
custCtx, ctx, ok := getContext(cmd)
if !ok {
return nil
}
a := ctx.Value(methodDescriptorKey{})
method, ok := a.(protoreflect.MethodDescriptor)
if !ok {
return nil
}
ctx, err := f(ctx, cmd, method)
if err != nil {
return err
}
custCtx.setContext(ctx)
return nil
}
return nil
}
}
// WithArgs will set the args of the command as args[1:].
func WithArgs(args []string) GrptlOption {
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) GrptlOption {
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
}
}