Skip to content

Commit

Permalink
remove need for depinject ignored fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Nov 4, 2024
1 parent b720ca8 commit 0c58a12
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
37 changes: 24 additions & 13 deletions client/v2/autocli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"
)

// AppOptions are autocli options for an app. These options can be built via depinject based on an app config. Ex:
// Options are input options for an autocli enabled app. These options can be built via depinject based on an app config.
// Ex:
//
// var autoCliOpts autocli.AppOptions
// err := depinject.Inject(appConfig, &encodingConfig.InterfaceRegistry, &autoCliOpts)
//
// If depinject isn't used, options can be provided manually or extracted from modules and the address codec can be provided by the auth keeper.
// One method for extracting autocli options is via the github.com/cosmos/cosmos-sdk/runtime/services.ExtractAutoCLIOptions function.
type AppOptions struct {
type Options struct {
depinject.In

// Modules are the AppModule implementations for the modules in the app.
Expand All @@ -40,17 +40,25 @@ type AppOptions struct {

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

// AppOptions are the autocli options for an app.
type AppOptions struct {
Modules map[string]appmodule.AppModule
ModuleOptions map[string]*autocliv1.ModuleOptions
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"`
skipValidation bool
}

type SkipValidationBool bool
// ProvideAppOptions returns AppOptions with the provided Options.
func ProvideAppOptions(options Options) AppOptions {
return AppOptions{
Modules: options.Modules,
ModuleOptions: options.ModuleOptions,
ClientCtx: options.ClientCtx,
}
}

// EnhanceRootCommand enhances the provided root command with autocli AppOptions,
// only adding missing commands and doesn't override commands already
Expand Down Expand Up @@ -87,7 +95,7 @@ func (appOptions AppOptions) EnhanceRootCommand(rootCmd *cobra.Command) error {
}

func (appOptions AppOptions) EnhanceRootCommandWithBuilder(rootCmd *cobra.Command, builder *Builder) error {
if !appOptions.SkipValidation {
if !appOptions.skipValidation {
if err := builder.ValidateAndComplete(); err != nil {
return err
}
Expand Down Expand Up @@ -141,7 +149,10 @@ func (appOptions AppOptions) EnhanceRootCommandWithBuilder(rootCmd *cobra.Comman
return nil
}

func NewAppOptionsSkeleton(
// NewAppOptionsFromConfig returns AppOptions for an app based on the provided modulesConfig and moduleOptions.
// It returns an AppOptions instance usable for CLI parsing but not execution. For an execution usable AppOptions
// see ProvideAppOptions, which expects input to be filled by depinject.
func NewAppOptionsFromConfig(
modulesConfig depinject.Config,
moduleOptions map[string]*autocliv1.ModuleOptions,
) (AppOptions, error) {
Expand Down Expand Up @@ -174,7 +185,7 @@ func NewAppOptionsSkeleton(
Modules: cfg.Modules,
ClientCtx: client.Context{InterfaceRegistry: interfaceRegistry},
ModuleOptions: moduleOptions,
SkipValidation: true,
skipValidation: true,
}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simapp

import (
"cosmossdk.io/client/v2/autocli"
_ "embed"
"fmt"

Expand Down Expand Up @@ -58,6 +59,8 @@ func AppConfig() depinject.Config {
multisigdepinject.ProvideAccount,
basedepinject.ProvideAccount,
lockupdepinject.ProvideAllLockupAccounts,
// autocli
autocli.ProvideAppOptions,

// provide base account options
basedepinject.ProvideSecp256K1PubKey,
Expand Down
2 changes: 1 addition & 1 deletion simapp/v2/simdv2/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewRootCmd[T transaction.Tx](
nodeCmds := nodeservice.NewNodeCommands()
autoCLIModuleOpts := make(map[string]*autocliv1.ModuleOptions)
autoCLIModuleOpts[nodeCmds.Name()] = nodeCmds.AutoCLIOptions()
autoCliOpts, err := autocli.NewAppOptionsSkeleton(
autoCliOpts, err := autocli.NewAppOptionsFromConfig(
depinject.Configs(simapp.AppConfig(), depinject.Supply(runtime.GlobalConfig{})),
autoCLIModuleOpts,
)
Expand Down

0 comments on commit 0c58a12

Please sign in to comment.