0g-chain/cmd/kvcli/main.go

197 lines
5.8 KiB
Go
Raw Normal View History

2018-05-25 13:46:33 +00:00
package main
import (
2019-06-07 11:59:19 +00:00
"fmt"
"os"
"path"
2018-05-25 13:46:33 +00:00
2019-06-20 13:37:57 +00:00
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/spf13/cobra"
"github.com/spf13/viper"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"
2018-05-25 13:46:33 +00:00
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
2019-06-07 11:59:19 +00:00
"github.com/cosmos/cosmos-sdk/client/lcd"
2018-05-25 13:46:33 +00:00
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
2019-06-07 11:59:19 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-05-25 13:46:33 +00:00
"github.com/cosmos/cosmos-sdk/version"
2019-06-20 16:29:13 +00:00
"github.com/kava-labs/kava/app"
2018-09-19 19:22:16 +00:00
2019-06-20 13:37:57 +00:00
at "github.com/cosmos/cosmos-sdk/x/auth"
auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
dist "github.com/cosmos/cosmos-sdk/x/distribution/client/rest"
gv "github.com/cosmos/cosmos-sdk/x/gov"
gov "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
mintrest "github.com/cosmos/cosmos-sdk/x/mint/client/rest"
sl "github.com/cosmos/cosmos-sdk/x/slashing"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
st "github.com/cosmos/cosmos-sdk/x/staking"
staking "github.com/cosmos/cosmos-sdk/x/staking/client/rest"
2018-05-25 13:46:33 +00:00
2019-06-20 13:37:57 +00:00
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
crisisclient "github.com/cosmos/cosmos-sdk/x/crisis/client"
distcmd "github.com/cosmos/cosmos-sdk/x/distribution"
distClient "github.com/cosmos/cosmos-sdk/x/distribution/client"
govClient "github.com/cosmos/cosmos-sdk/x/gov/client"
mintclient "github.com/cosmos/cosmos-sdk/x/mint/client"
slashingclient "github.com/cosmos/cosmos-sdk/x/slashing/client"
stakingclient "github.com/cosmos/cosmos-sdk/x/staking/client"
2018-05-25 13:46:33 +00:00
)
func main() {
2019-06-07 11:59:19 +00:00
// Configure cobra to sort commands
2018-05-25 13:46:33 +00:00
cobra.EnableCommandSorting = false
2019-06-07 11:59:19 +00:00
// Instantiate the codec for the command line application
cdc := app.MakeCodec()
2018-05-25 13:46:33 +00:00
2019-06-07 11:59:19 +00:00
// Read in the configuration file for the sdk
config := sdk.GetConfig()
2019-06-25 13:29:56 +00:00
app.SetBech32AddressPrefixes(config)
2019-06-07 11:59:19 +00:00
config.Seal()
2018-05-25 13:46:33 +00:00
2019-06-07 11:59:19 +00:00
// TODO: setup keybase, viper object, etc. to be passed into
// the below functions and eliminate global vars, like we do
// with the cdc
2019-06-20 13:37:57 +00:00
// Module clients hold cli commnads (tx,query) and lcd routes
// TODO: Make the lcd command take a list of ModuleClient
mc := []sdk.ModuleClients{
govClient.NewModuleClient(gv.StoreKey, cdc),
distClient.NewModuleClient(distcmd.StoreKey, cdc),
stakingclient.NewModuleClient(st.StoreKey, cdc),
mintclient.NewModuleClient(mint.StoreKey, cdc),
slashingclient.NewModuleClient(sl.StoreKey, cdc),
crisisclient.NewModuleClient(sl.StoreKey, cdc),
}
2019-06-07 11:59:19 +00:00
rootCmd := &cobra.Command{
2019-06-20 17:08:58 +00:00
Use: "kvcli",
Short: "Command line interface for interacting with kvd",
2018-08-14 22:13:54 +00:00
}
2019-06-07 11:59:19 +00:00
// Add --chain-id to persistent flags and mark it required
rootCmd.PersistentFlags().String(client.FlagChainID, "", "Chain ID of tendermint node")
rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error {
return initConfig(rootCmd)
2018-08-14 22:13:54 +00:00
}
2019-06-07 11:59:19 +00:00
// Construct Root Command
2018-05-25 13:46:33 +00:00
rootCmd.AddCommand(
2019-06-07 11:59:19 +00:00
rpc.StatusCommand(),
client.ConfigCmd(app.DefaultCLIHome),
2019-06-20 13:37:57 +00:00
queryCmd(cdc, mc),
txCmd(cdc, mc),
2018-08-14 22:13:54 +00:00
client.LineBreak,
2019-06-07 11:59:19 +00:00
lcd.ServeCommand(cdc, registerRoutes),
client.LineBreak,
keys.Commands(),
client.LineBreak,
2019-06-20 13:37:57 +00:00
version.VersionCmd,
2019-06-07 11:59:19 +00:00
client.NewCompletionCmd(rootCmd, true),
2018-08-14 22:13:54 +00:00
)
2019-06-07 11:59:19 +00:00
// Add flags and prefix all env exposed with GA
executor := cli.PrepareMainCmd(rootCmd, "GA", app.DefaultCLIHome)
err := executor.Execute()
if err != nil {
fmt.Printf("Failed executing CLI command: %s, exiting...\n", err)
os.Exit(1)
2018-08-14 22:13:54 +00:00
}
2019-06-07 11:59:19 +00:00
}
2018-08-14 22:13:54 +00:00
2019-06-20 13:37:57 +00:00
func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
2019-06-07 11:59:19 +00:00
queryCmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Querying subcommands",
2018-07-14 22:27:56 +00:00
}
2019-06-07 11:59:19 +00:00
queryCmd.AddCommand(
rpc.ValidatorCommand(cdc),
rpc.BlockCommand(),
tx.SearchTxCmd(cdc),
tx.QueryTxCmd(cdc),
client.LineBreak,
2019-06-20 13:37:57 +00:00
authcmd.GetAccountCmd(at.StoreKey, cdc),
2018-07-14 22:27:56 +00:00
)
2018-09-01 23:29:51 +00:00
2019-06-20 13:37:57 +00:00
for _, m := range mc {
mQueryCmd := m.GetQueryCmd()
if mQueryCmd != nil {
queryCmd.AddCommand(mQueryCmd)
}
}
2019-06-07 11:59:19 +00:00
return queryCmd
}
2019-06-20 13:37:57 +00:00
func txCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
2019-06-07 11:59:19 +00:00
txCmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
}
txCmd.AddCommand(
bankcmd.SendTxCmd(cdc),
client.LineBreak,
authcmd.GetSignCommand(cdc),
authcmd.GetMultiSignCommand(cdc),
tx.GetBroadcastCommand(cdc),
tx.GetEncodeCommand(cdc),
2018-05-25 13:46:33 +00:00
client.LineBreak,
)
2019-06-20 13:37:57 +00:00
for _, m := range mc {
txCmd.AddCommand(m.GetTxCmd())
2019-06-07 11:59:19 +00:00
}
return txCmd
}
// registerRoutes registers the routes from the different modules for the LCD.
// NOTE: details on the routes added for each module are in the module documentation
// NOTE: If making updates here you also need to update the test helper in client/lcd/test_helper.go
func registerRoutes(rs *lcd.RestServer) {
2019-06-20 13:37:57 +00:00
rpc.RegisterRoutes(rs.CliCtx, rs.Mux)
tx.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, at.StoreKey)
bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
dist.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, distcmd.StoreKey)
staking.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
slashing.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
gov.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
mintrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
}
2019-06-07 11:59:19 +00:00
func initConfig(cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(cli.HomeFlag)
2018-08-14 22:13:54 +00:00
if err != nil {
2019-06-07 11:59:19 +00:00
return err
}
cfgFile := path.Join(home, "config", "config.toml")
if _, err := os.Stat(cfgFile); err == nil {
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
return err
}
}
if err := viper.BindPFlag(client.FlagChainID, cmd.PersistentFlags().Lookup(client.FlagChainID)); err != nil {
return err
}
if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil {
return err
2018-08-14 22:13:54 +00:00
}
2019-06-07 11:59:19 +00:00
return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag))
2018-05-25 13:46:33 +00:00
}