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

167 lines
4.3 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"
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"
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"
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
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-07-18 18:05:26 +00:00
queryCmd(cdc),
txCmd(cdc),
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-07-18 18:05:26 +00:00
version.Cmd,
2019-06-07 11:59:19 +00:00
client.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
2019-07-18 18:05:26 +00:00
func queryCmd(cdc *amino.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),
client.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),
2019-06-07 11:59:19 +00:00
client.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
}
2019-07-18 18:05:26 +00:00
func txCmd(cdc *amino.Codec) *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),
2019-07-18 18:05:26 +00:00
client.LineBreak,
authcmd.GetBroadcastCommand(cdc),
authcmd.GetEncodeCommand(cdc),
2019-09-25 19:50:03 +00:00
authcmd.GetDecodeCommand(cdc),
2018-05-25 13:46:33 +00:00
client.LineBreak,
)
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)
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(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
}