Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add minimal dependency-injection based app.go wiring #9442

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions app/cli/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package cli

import (
"os"

"github.com/cosmos/cosmos-sdk/app/internal"

"github.com/spf13/cobra"
tmcli "github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/app"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/debug"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/auth/types"
vestingcli "github.com/cosmos/cosmos-sdk/x/auth/vesting/client/cli"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
)

type Options struct {
DefaultAppConfig *app.Config
DefaultHome string
EnvPrefix string
}

func Run(options Options) {
rootCmd := newRootCmd(options)

if err := svrcmd.Execute(rootCmd, options.DefaultHome); err != nil {
switch e := err.(type) {
case server.ErrorCode:
os.Exit(e.Code)

default:
os.Exit(1)
}
}
}

func newRootCmd(options Options) *cobra.Command {
a, err := internal.NewApp(options.DefaultAppConfig)
if err != nil {
panic(err)
}

initClientCtx := client.Context{}.
WithJSONCodec(a.Codec()).
WithInterfaceRegistry(a.InterfaceRegistry()).
WithTxConfig(a.TxConfig()).
WithLegacyAmino(a.Amino()).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithHomeDir(options.DefaultHome).
WithViper("") // In simapp, we don't use any prefix for env variables.

rootCmd := &cobra.Command{
Use: "simd",
Short: "simulation app",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// set the default command outputs
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())

initClientCtx = client.ReadHomeFlag(initClientCtx, cmd)

initClientCtx, err := config.ReadFromClientConfig(initClientCtx)
if err != nil {
return err
}

if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
return err
}

return server.InterceptConfigsPreRunHandler(cmd)
},
}

initRootCmd(options, rootCmd, a)

return rootCmd
}

func initRootCmd(options Options, rootCmd *cobra.Command, a *internal.AppProvider) {
cfg := sdk.GetConfig()
cfg.Seal()

rootCmd.AddCommand(
genutilcli.InitCmd(simapp.ModuleBasics, options.DefaultHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, options.DefaultHome),
genutilcli.MigrateGenesisCmd(),
genutilcli.GenTxCmd(simapp.ModuleBasics, a.TxConfig(), banktypes.GenesisBalancesIterator{}, options.DefaultHome),
genutilcli.ValidateGenesisCmd(simapp.ModuleBasics),
//AddGenesisAccountCmd(options.DefaultHome),
tmcli.NewCompletionCmd(rootCmd, true),
//TODO: testnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
config.Cmd(),
)

server.AddCommands(rootCmd, options.DefaultHome, a.AppCreator, a.AppExportor, addModuleInitFlags)

// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
rpc.StatusCommand(),
queryCommand(),
txCommand(),
keys.Commands(options.DefaultHome),
)

// add rosetta
rootCmd.AddCommand(server.RosettaCommand(a.InterfaceRegistry(), a.Codec()))
}

func addModuleInitFlags(startCmd *cobra.Command) {
crisis.AddModuleInitFlags(startCmd)
}

func queryCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Querying subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
authcmd.GetAccountCmd(),
rpc.ValidatorCommand(),
rpc.BlockCommand(),
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
)

simapp.ModuleBasics.AddQueryCommands(cmd)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

return cmd
}

func txCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
authcmd.GetSignCommand(),
authcmd.GetSignBatchCommand(),
authcmd.GetMultiSignCommand(),
authcmd.GetMultiSignBatchCmd(),
authcmd.GetValidateSignaturesCommand(),
flags.LineBreak,
authcmd.GetBroadcastCommand(),
authcmd.GetEncodeCommand(),
authcmd.GetDecodeCommand(),
flags.LineBreak,
vestingcli.GetTxCmd(),
)

simapp.ModuleBasics.AddTxCommands(cmd)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

return cmd
}
Loading