-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathcommon.go
More file actions
247 lines (208 loc) · 7.07 KB
/
Copy pathcommon.go
File metadata and controls
247 lines (208 loc) · 7.07 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package autocli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"google.golang.org/protobuf/reflect/protoreflect"
"sigs.k8s.io/yaml"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/client/v2/internal/flags"
"cosmossdk.io/client/v2/internal/util"
"github.com/cosmos/cosmos-sdk/client"
)
type cmdType int
const (
queryCmdType cmdType = iota
msgCmdType
)
func (b *Builder) buildMethodCommandCommon(descriptor protoreflect.MethodDescriptor, options *autocliv1.RpcCommandOptions, exec func(cmd *cobra.Command, input protoreflect.Message) error) (*cobra.Command, error) {
if options == nil {
// use the defaults
options = &autocliv1.RpcCommandOptions{}
}
short := options.Short
if short == "" {
short = fmt.Sprintf("Execute the %s RPC method", descriptor.Name())
}
inputDesc := descriptor.Input()
inputType := util.ResolveMessageType(b.TypeResolver, inputDesc)
use := options.Use
if use == "" {
use = protoNameToCliName(descriptor.Name())
}
cmd := &cobra.Command{
SilenceUsage: false,
Use: use,
Long: options.Long,
Short: short,
Example: options.Example,
Aliases: options.Alias,
SuggestFor: options.SuggestFor,
Deprecated: options.Deprecated,
Version: options.Version,
}
binder, err := b.AddMessageFlags(cmd.Context(), cmd.Flags(), inputType, options)
if err != nil {
return nil, err
}
cmd.Args = binder.CobraArgs
cmd.RunE = func(cmd *cobra.Command, args []string) error {
input, err := binder.BuildMessage(args)
if err != nil {
return err
}
// signer related logic, triggers only when there is a signer defined
if binder.SignerInfo.FieldName != "" {
if binder.SignerInfo.IsFlag {
// the client context uses the from flag to determine the signer.
// this sets the signer flags to the from flag value if a custom signer flag is set.
// marks the custom flag as required.
if binder.SignerInfo.FieldName != flags.FlagFrom {
if err := cmd.MarkFlagRequired(binder.SignerInfo.FieldName); err != nil {
return err
}
signer, err := cmd.Flags().GetString(binder.SignerInfo.FieldName)
if err != nil {
return fmt.Errorf("failed to get signer flag: %w", err)
}
if err := cmd.Flags().Set(flags.FlagFrom, signer); err != nil {
return err
}
}
} else {
// if the signer is not a flag, it is a positional argument
// we need to get the correct positional arguments
if err := cmd.Flags().Set(flags.FlagFrom, args[binder.SignerInfo.PositionalArgIndex]); err != nil {
return err
}
}
}
return exec(cmd, input)
}
return cmd, nil
}
// enhanceCommandCommon enhances the provided query or msg command with either generated commands based on the provided module
// options or the provided custom commands for each module. If the provided query command already contains a command
// for a module, that command is not over-written by this method. This allows a graceful addition of autocli to
// automatically fill in missing commands.
func (b *Builder) enhanceCommandCommon(
cmd *cobra.Command,
cmdType cmdType,
appOptions AppOptions,
customCmds map[string]*cobra.Command,
) error {
moduleOptions := appOptions.ModuleOptions
if len(moduleOptions) == 0 {
moduleOptions = make(map[string]*autocliv1.ModuleOptions)
}
for name, module := range appOptions.Modules {
if _, ok := moduleOptions[name]; !ok {
if module, ok := module.(HasAutoCLIConfig); ok {
moduleOptions[name] = module.AutoCLIOptions()
} else {
moduleOptions[name] = nil
}
}
}
for moduleName, modOpts := range moduleOptions {
hasModuleOptions := modOpts != nil
// if we have an existing command skip adding one here
if subCmd := findSubCommand(cmd, moduleName); subCmd != nil {
if hasModuleOptions { // check if we need to enhance the existing command
if err := enhanceCustomCmd(b, subCmd, cmdType, modOpts); err != nil {
return err
}
}
continue
}
// if we have a custom command use that instead of generating one
if custom, ok := customCmds[moduleName]; ok {
if hasModuleOptions { // check if we need to enhance the existing command
if err := enhanceCustomCmd(b, custom, cmdType, modOpts); err != nil {
return err
}
}
cmd.AddCommand(custom)
continue
}
// if we don't have module options, skip adding a command as we don't have anything to add
if !hasModuleOptions {
continue
}
switch cmdType {
case queryCmdType:
if err := enhanceQuery(b, moduleName, cmd, modOpts); err != nil {
return err
}
case msgCmdType:
if err := enhanceMsg(b, moduleName, cmd, modOpts); err != nil {
return err
}
}
}
return nil
}
// enhanceQuery enhances the provided query command with the autocli commands for a module.
func enhanceQuery(builder *Builder, moduleName string, cmd *cobra.Command, modOpts *autocliv1.ModuleOptions) error {
if queryCmdDesc := modOpts.Query; queryCmdDesc != nil {
subCmd := topLevelCmd(cmd.Context(), moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName))
if err := builder.AddQueryServiceCommands(subCmd, queryCmdDesc); err != nil {
return err
}
cmd.AddCommand(subCmd)
}
return nil
}
// enhanceMsg enhances the provided msg command with the autocli commands for a module.
func enhanceMsg(builder *Builder, moduleName string, cmd *cobra.Command, modOpts *autocliv1.ModuleOptions) error {
if txCmdDesc := modOpts.Tx; txCmdDesc != nil {
subCmd := topLevelCmd(cmd.Context(), moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName))
if err := builder.AddMsgServiceCommands(subCmd, txCmdDesc); err != nil {
return err
}
cmd.AddCommand(subCmd)
}
return nil
}
// enhanceCustomCmd enhances the provided custom query or msg command autocli commands for a module.
func enhanceCustomCmd(builder *Builder, cmd *cobra.Command, cmdType cmdType, modOpts *autocliv1.ModuleOptions) error {
switch cmdType {
case queryCmdType:
if modOpts.Query != nil && modOpts.Query.EnhanceCustomCommand {
if err := builder.AddQueryServiceCommands(cmd, modOpts.Query); err != nil {
return err
}
}
case msgCmdType:
if modOpts.Tx != nil && modOpts.Tx.EnhanceCustomCommand {
if err := builder.AddMsgServiceCommands(cmd, modOpts.Tx); err != nil {
return err
}
}
}
return nil
}
// outOrStdoutFormat formats the output based on the output flag and writes it to the command's output stream.
func (b *Builder) outOrStdoutFormat(cmd *cobra.Command, out []byte) error {
clientCtx := client.Context{}
if v := cmd.Context().Value(client.ClientContextKey); v != nil {
clientCtx = *(v.(*client.Context))
}
flagSet := cmd.Flags()
if clientCtx.OutputFormat == "" || flagSet.Changed(flags.FlagOutput) {
output, _ := flagSet.GetString(flags.FlagOutput)
clientCtx = clientCtx.WithOutputFormat(output)
}
var err error
outputType := clientCtx.OutputFormat
// if the output type is text, convert the json to yaml
// if output type is json or nil, default to json
if outputType == flags.OutputFormatText {
out, err = yaml.JSONToYAML(out)
if err != nil {
return err
}
}
cmd.Println(strings.TrimSpace(string(out)))
return nil
}