Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Nov 1, 2024
1 parent 5b7fc8a commit da1a2f6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
63 changes: 61 additions & 2 deletions client/v2/autocli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
"cosmossdk.io/client/v2/autocli/flag"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/x/tx/signing"

"github.com/cosmos/cosmos-sdk/client"
sdkflags "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/gogoproto/proto"
)

// AppOptions are autocli options for an app. These options can be built via depinject based on an app config. Ex:
Expand All @@ -36,8 +40,18 @@ type AppOptions struct {

// ClientCtx contains the necessary information needed to execute the commands.
ClientCtx client.Context

// SkipValidation is used to skip the validation of the autocli options. This is useful when
// constructing the options manually and not using depinject.
// TODO: replace custom type once `ignored` tag is available in depinject
// in https://github.com/cosmos/cosmos-sdk/pull/22409
// at the point it can be made private and ignored. i.e.
// skipValidation bool `ignored:"true"`
SkipValidation SkipValidationBool `optional:"true"`
}

type SkipValidationBool bool

// EnhanceRootCommand enhances the provided root command with autocli AppOptions,
// only adding missing commands and doesn't override commands already
// in the root command. This allows for the graceful integration of autocli with
Expand Down Expand Up @@ -73,8 +87,10 @@ func (appOptions AppOptions) EnhanceRootCommand(rootCmd *cobra.Command) error {
}

func (appOptions AppOptions) EnhanceRootCommandWithBuilder(rootCmd *cobra.Command, builder *Builder) error {
if err := builder.ValidateAndComplete(); err != nil {
return err
if !appOptions.SkipValidation {
if err := builder.ValidateAndComplete(); err != nil {
return err
}
}

// extract any custom commands from modules
Expand Down Expand Up @@ -124,3 +140,46 @@ func (appOptions AppOptions) EnhanceRootCommandWithBuilder(rootCmd *cobra.Comman

return nil
}

func NewAppOptionsSkeleton(
modulesConfig depinject.Config,
moduleOptions map[string]*autocliv1.ModuleOptions,
) (AppOptions, error) {
interfaceRegistry, err := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
SigningOptions: signing.Options{
AddressCodec: nopAddressCodec{},
ValidatorAddressCodec: nopAddressCodec{},
},
})
if err != nil {
return AppOptions{}, err
}
cfg := struct {
depinject.In
Modules map[string]appmodule.AppModule
}{
Modules: nil,
}
err = depinject.Inject(depinject.Configs(
modulesConfig,
depinject.Supply(
log.NewNopLogger(),
)), &cfg)
if err != nil {
return AppOptions{}, err
}

return AppOptions{
Modules: cfg.Modules,
ClientCtx: client.Context{InterfaceRegistry: interfaceRegistry},
ModuleOptions: moduleOptions,
SkipValidation: true,
}, nil
}

type nopAddressCodec struct{}

func (nopAddressCodec) StringToBytes(_ string) ([]byte, error) { return nil, nil }

func (nopAddressCodec) BytesToString(_ []byte) (string, error) { return "", nil }
2 changes: 1 addition & 1 deletion client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 // indirect
cosmossdk.io/errors v1.0.1 // indirect
cosmossdk.io/log v1.4.1 // indirect
cosmossdk.io/log v1.4.1
cosmossdk.io/math v1.3.0
cosmossdk.io/schema v0.3.1-0.20241010135032-192601639cac // indirect
cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc // indirect
Expand Down
19 changes: 15 additions & 4 deletions simapp/v2/simdv2/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ func NewRootCmd[T transaction.Tx](
return nil, err
}

nodeCmds := nodeservice.NewNodeCommands()
autoCLIModuleOpts := make(map[string]*autocliv1.ModuleOptions)
autoCLIModuleOpts[nodeCmds.Name()] = nodeCmds.AutoCLIOptions()
autoCliOpts, err := autocli.NewAppOptionsSkeleton(
depinject.Configs(simapp.AppConfig(), depinject.Supply(runtime.GlobalConfig{})),
autoCLIModuleOpts,
)
if err != nil {
return rootCommand, nil
}

if err = autoCliOpts.EnhanceRootCommand(rootCommand); err != nil {
return rootCommand, nil
}
subCommand, configMap, logger, err := factory.ParseCommand(rootCommand, args)
if err != nil {
if errors.Is(err, pflag.ErrHelp) {
Expand All @@ -48,7 +62,6 @@ func NewRootCmd[T transaction.Tx](
}

var (
autoCliOpts autocli.AppOptions
moduleManager *runtime.MM[T]
clientCtx client.Context
simApp *simapp.SimApp[T]
Expand Down Expand Up @@ -93,9 +106,7 @@ func NewRootCmd[T transaction.Tx](
if err != nil {
return nil, err
}
nodeCmds := nodeservice.NewNodeCommands()
autoCliOpts.ModuleOptions = make(map[string]*autocliv1.ModuleOptions)
autoCliOpts.ModuleOptions[nodeCmds.Name()] = nodeCmds.AutoCLIOptions()
autoCliOpts.ModuleOptions = autoCLIModuleOpts
if err := autoCliOpts.EnhanceRootCommand(rootCommand); err != nil {
return nil, err
}
Expand Down

0 comments on commit da1a2f6

Please sign in to comment.