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

172 lines
4.6 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/spf13/cobra"
"github.com/spf13/viper"
"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/flags"
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"
2020-04-30 14:13:31 +00:00
"github.com/cosmos/cosmos-sdk/codec"
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-07-18 18:05:26 +00:00
"github.com/cosmos/cosmos-sdk/x/auth"
2019-06-20 13:37:57 +00:00
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
2019-07-18 18:05:26 +00:00
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/cosmos-sdk/x/bank"
2019-06-20 13:37:57 +00:00
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
2019-07-18 18:05:26 +00:00
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/migrate/rest_v0_3"
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
// Set the global config
// config is not sealed as the cli supports two coin types for legacy reasons.
2019-06-07 11:59:19 +00:00
config := sdk.GetConfig()
2019-06-25 13:29:56 +00:00
app.SetBech32AddressPrefixes(config)
app.SetBip44CoinType(config)
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
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(flags.FlagChainID, "", "Chain ID of tendermint node")
2019-06-07 11:59:19 +00:00
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-07-18 18:05:26 +00:00
queryCmd(cdc),
txCmd(cdc),
flags.LineBreak,
2019-06-07 11:59:19 +00:00
lcd.ServeCommand(cdc, registerRoutes),
flags.LineBreak,
getModifiedKeysCmd(),
flags.LineBreak,
2019-07-18 18:05:26 +00:00
version.Cmd,
flags.NewCompletionCmd(rootCmd, true),
2018-08-14 22:13:54 +00:00
)
2019-07-19 14:06:33 +00:00
// Add flags and prefix all env exposed with KA
2019-07-18 18:05:26 +00:00
executor := cli.PrepareMainCmd(rootCmd, "KA", app.DefaultCLIHome)
2019-06-07 11:59:19 +00:00
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
2020-04-30 14:13:31 +00:00
func queryCmd(cdc *codec.Codec) *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(
2019-07-18 18:05:26 +00:00
authcmd.GetAccountCmd(cdc),
flags.LineBreak,
2019-06-07 11:59:19 +00:00
rpc.ValidatorCommand(cdc),
rpc.BlockCommand(),
2019-07-18 18:05:26 +00:00
authcmd.QueryTxsByEventsCmd(cdc),
authcmd.QueryTxCmd(cdc),
flags.LineBreak,
2018-07-14 22:27:56 +00:00
)
2018-09-01 23:29:51 +00:00
2019-07-18 18:05:26 +00:00
// add modules' query commands
app.ModuleBasics.AddQueryCommands(queryCmd, cdc)
2019-06-07 11:59:19 +00:00
return queryCmd
}
2020-04-30 14:13:31 +00:00
func txCmd(cdc *codec.Codec) *cobra.Command {
2019-06-07 11:59:19 +00:00
txCmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
}
txCmd.AddCommand(
bankcmd.SendTxCmd(cdc),
flags.LineBreak,
2019-06-07 11:59:19 +00:00
authcmd.GetSignCommand(cdc),
authcmd.GetMultiSignCommand(cdc),
flags.LineBreak,
2019-07-18 18:05:26 +00:00
authcmd.GetBroadcastCommand(cdc),
authcmd.GetEncodeCommand(cdc),
2019-09-25 19:50:03 +00:00
authcmd.GetDecodeCommand(cdc),
flags.LineBreak,
2018-05-25 13:46:33 +00:00
)
2019-07-18 18:05:26 +00:00
// add modules' tx commands
app.ModuleBasics.AddTxCommands(txCmd, cdc)
// remove auth and bank commands as they're mounted under the root tx command
var cmdsToRemove []*cobra.Command
for _, cmd := range txCmd.Commands() {
if cmd.Use == auth.ModuleName || cmd.Use == bank.ModuleName {
cmdsToRemove = append(cmdsToRemove, cmd)
}
2019-06-07 11:59:19 +00:00
}
2019-07-18 18:05:26 +00:00
txCmd.RemoveCommand(cmdsToRemove...)
2019-06-07 11:59:19 +00:00
return txCmd
}
// registerRoutes registers the routes from the different modules for the LCD.
func registerRoutes(rs *lcd.RestServer) {
2019-07-18 18:05:26 +00:00
client.RegisterRoutes(rs.CliCtx, rs.Mux)
authrest.RegisterTxRoutes(rs.CliCtx, rs.Mux)
app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux)
app.RegisterSimulateRoutes(rs.CliCtx, rs.Mux)
// register legacy endpoints compatible with v0.3.x of kava
rest_v0_3.RegisterRoutes(rs.CliCtx, rs.Mux)
2019-06-20 13:37:57 +00:00
}
2019-07-19 14:06:33 +00:00
// initConfig reads in and sets options from a config file (if one exists)
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")
2019-07-19 14:06:33 +00:00
2019-06-07 11:59:19 +00:00
if _, err := os.Stat(cfgFile); err == nil {
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
return err
}
}
if err := viper.BindPFlag(flags.FlagChainID, cmd.PersistentFlags().Lookup(flags.FlagChainID)); err != nil {
2019-06-07 11:59:19 +00:00
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
}