1
1
package autocli
2
2
3
3
import (
4
+ "github.com/cosmos/gogoproto/proto"
4
5
"github.com/spf13/cobra"
5
6
"google.golang.org/grpc"
6
7
"google.golang.org/protobuf/reflect/protoregistry"
@@ -9,12 +10,15 @@ import (
9
10
"cosmossdk.io/client/v2/autocli/flag"
10
11
"cosmossdk.io/core/appmodule"
11
12
"cosmossdk.io/depinject"
13
+ "cosmossdk.io/log"
14
+ "cosmossdk.io/x/tx/signing"
12
15
13
16
"github.com/cosmos/cosmos-sdk/client"
14
17
sdkflags "github.com/cosmos/cosmos-sdk/client/flags"
18
+ "github.com/cosmos/cosmos-sdk/codec/types"
15
19
)
16
20
17
- // AppOptions are autocli options for an app. These options can be built via depinject based on an app config. Ex:
21
+ // AppOptions are input options for an autocli enabled app. These options can be built via depinject based on an app config.
18
22
// Ex:
19
23
//
20
24
// var autoCliOpts autocli.AppOptions
@@ -36,6 +40,8 @@ type AppOptions struct {
36
40
37
41
// ClientCtx contains the necessary information needed to execute the commands.
38
42
ClientCtx client.Context
43
+
44
+ skipValidation bool
39
45
}
40
46
41
47
// EnhanceRootCommand enhances the provided root command with autocli AppOptions,
@@ -76,8 +82,10 @@ func (appOptions AppOptions) EnhanceRootCommand(rootCmd *cobra.Command) error {
76
82
}
77
83
78
84
func (appOptions AppOptions ) EnhanceRootCommandWithBuilder (rootCmd * cobra.Command , builder * Builder ) error {
79
- if err := builder .ValidateAndComplete (); err != nil {
80
- return err
85
+ if ! appOptions .skipValidation {
86
+ if err := builder .ValidateAndComplete (); err != nil {
87
+ return err
88
+ }
81
89
}
82
90
83
91
// extract any custom commands from modules
@@ -127,3 +135,49 @@ func (appOptions AppOptions) EnhanceRootCommandWithBuilder(rootCmd *cobra.Comman
127
135
128
136
return nil
129
137
}
138
+
139
+ // NewAppOptionsFromConfig returns AppOptions for an app based on the provided modulesConfig and moduleOptions.
140
+ // It returns an AppOptions instance usable for CLI parsing but not execution. For an execution usable AppOptions
141
+ // see ProvideAppOptions, which expects input to be filled by depinject.
142
+ func NewAppOptionsFromConfig (
143
+ modulesConfig depinject.Config ,
144
+ moduleOptions map [string ]* autocliv1.ModuleOptions ,
145
+ ) (AppOptions , error ) {
146
+ interfaceRegistry , err := types .NewInterfaceRegistryWithOptions (types.InterfaceRegistryOptions {
147
+ ProtoFiles : proto .HybridResolver ,
148
+ SigningOptions : signing.Options {
149
+ AddressCodec : nopAddressCodec {},
150
+ ValidatorAddressCodec : nopAddressCodec {},
151
+ },
152
+ })
153
+ if err != nil {
154
+ return AppOptions {}, err
155
+ }
156
+ cfg := struct {
157
+ depinject.In
158
+ Modules map [string ]appmodule.AppModule
159
+ }{
160
+ Modules : nil ,
161
+ }
162
+ err = depinject .Inject (depinject .Configs (
163
+ modulesConfig ,
164
+ depinject .Supply (
165
+ log .NewNopLogger (),
166
+ )), & cfg )
167
+ if err != nil {
168
+ return AppOptions {}, err
169
+ }
170
+
171
+ return AppOptions {
172
+ Modules : cfg .Modules ,
173
+ ClientCtx : client.Context {InterfaceRegistry : interfaceRegistry },
174
+ ModuleOptions : moduleOptions ,
175
+ skipValidation : true ,
176
+ }, nil
177
+ }
178
+
179
+ type nopAddressCodec struct {}
180
+
181
+ func (nopAddressCodec ) StringToBytes (_ string ) ([]byte , error ) { return nil , nil }
182
+
183
+ func (nopAddressCodec ) BytesToString (_ []byte ) (string , error ) { return "" , nil }
0 commit comments