-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_cmd_wrap.go
181 lines (157 loc) · 5.06 KB
/
query_cmd_wrap.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
180
181
package osmocli
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
grpc1 "github.com/gogo/protobuf/grpc"
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// global variable set on index command.
// helps populate Longs, when not set in QueryDescriptor.
var lastQueryModuleName string
type QueryDescriptor struct {
Use string
Short string
Long string
HasPagination bool
QueryFnName string
Flags FlagDesc
// Map of FieldName -> FlagName
CustomFlagOverrides map[string]string
// Map of FieldName -> CustomParseFn
CustomFieldParsers map[string]CustomFieldParserFn
ParseQuery func(args []string, flags *pflag.FlagSet) (proto.Message, error)
ModuleName string
numArgs int
}
func QueryIndexCmd(moduleName string) *cobra.Command {
cmd := IndexCmd(moduleName)
cmd.Short = fmt.Sprintf("Querying commands for the %s module", moduleName)
lastQueryModuleName = moduleName
return cmd
}
func AddQueryCmd[Q proto.Message, querier any](cmd *cobra.Command, newQueryClientFn func(grpc1.ClientConn) querier, f func() (*QueryDescriptor, Q)) {
desc, _ := f()
subCmd := BuildQueryCli[Q](desc, newQueryClientFn)
cmd.AddCommand(subCmd)
}
func (desc *QueryDescriptor) FormatLong(moduleName string) {
desc.Long = FormatLongDesc(desc.Long, NewLongMetadata(moduleName).WithShort(desc.Short))
}
func prepareDescriptor[reqP proto.Message](desc *QueryDescriptor) {
if !desc.HasPagination {
desc.HasPagination = ParseHasPagination[reqP]()
}
if desc.QueryFnName == "" {
desc.QueryFnName = ParseExpectedQueryFnName[reqP]()
}
if strings.Contains(desc.Long, "{") {
if desc.ModuleName == "" {
desc.ModuleName = lastQueryModuleName
}
desc.FormatLong(desc.ModuleName)
}
desc.numArgs = ParseNumFields[reqP]() - len(desc.CustomFlagOverrides)
if desc.HasPagination {
desc.numArgs = desc.numArgs - 1
}
}
func BuildQueryCli[reqP proto.Message, querier any](desc *QueryDescriptor, newQueryClientFn func(grpc1.ClientConn) querier) *cobra.Command {
prepareDescriptor[reqP](desc)
if desc.ParseQuery == nil {
desc.ParseQuery = func(args []string, fs *pflag.FlagSet) (proto.Message, error) {
flagAdvice := FlagAdvice{
HasPagination: desc.HasPagination,
CustomFlagOverrides: desc.CustomFlagOverrides,
CustomFieldParsers: desc.CustomFieldParsers,
}.Sanitize()
return ParseFieldsFromFlagsAndArgs[reqP](flagAdvice, fs, args)
}
}
cmd := &cobra.Command{
Use: desc.Use,
Short: desc.Short,
Long: desc.Long,
Args: cobra.ExactArgs(desc.numArgs),
RunE: queryLogic(desc, newQueryClientFn),
}
flags.AddQueryFlagsToCmd(cmd)
AddFlags(cmd, desc.Flags)
if desc.HasPagination {
cmdName := strings.Split(desc.Use, " ")[0]
flags.AddPaginationFlagsToCmd(cmd, cmdName)
}
return cmd
}
// SimpleQueryCmd builds a query, for the common, simple case.
// It detects that the querier function name is the same as the ProtoMessage name,
// with just the "Query" and "Request" args chopped off.
// It expects all proto fields to appear as arguments, in order.
func SimpleQueryCmd[reqP proto.Message, querier any](use string, short string, long string,
moduleName string, newQueryClientFn func(grpc1.ClientConn) querier,
) *cobra.Command {
desc := QueryDescriptor{
Use: use,
Short: short,
Long: FormatLongDesc(long, NewLongMetadata(moduleName).WithShort(short)),
}
return BuildQueryCli[reqP](&desc, newQueryClientFn)
}
func GetParams[reqP proto.Message, querier any](moduleName string,
newQueryClientFn func(grpc1.ClientConn) querier,
) *cobra.Command {
return BuildQueryCli[reqP](&QueryDescriptor{
Use: "params [flags]",
Short: fmt.Sprintf("Get the params for the x/%s module", moduleName),
QueryFnName: "Params",
}, newQueryClientFn)
}
func callQueryClientFn(ctx context.Context, fnName string, req proto.Message, q any) (res proto.Message, err error) {
qVal := reflect.ValueOf(q)
method := qVal.MethodByName(fnName)
if (method == reflect.Value{}) {
return nil, fmt.Errorf("Method %s does not exist on the querier."+
" You likely need to override QueryFnName in your Query descriptor", fnName)
}
args := []reflect.Value{
reflect.ValueOf(ctx),
reflect.ValueOf(req),
}
results := method.Call(args)
if len(results) != 2 {
panic("We got something wrong")
}
if !results[1].IsNil() {
//nolint:forcetypeassert
err = results[1].Interface().(error)
return res, err
}
//nolint:forcetypeassert
res = results[0].Interface().(proto.Message)
return res, nil
}
func queryLogic[querier any](desc *QueryDescriptor,
newQueryClientFn func(grpc1.ClientConn) querier,
) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := newQueryClientFn(clientCtx)
req, err := desc.ParseQuery(args, cmd.Flags())
if err != nil {
return err
}
res, err := callQueryClientFn(cmd.Context(), desc.QueryFnName, req, queryClient)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
}