From ba203d3f26ae07d67e2fd710841278c2bd7deec1 Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Fri, 5 Jul 2019 12:46:13 -0400 Subject: [PATCH] Set up CLI framework without logic for query, tx, SDK rest, and web3 api commands (#61) --- app/ethermint.go | 5 +++ cmd/emintcli/main.go | 81 ++++++++++++++++++++++++++++++++++++++++++-- go.mod | 2 ++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/app/ethermint.go b/app/ethermint.go index 63141cdb..84df14ce 100644 --- a/app/ethermint.go +++ b/app/ethermint.go @@ -1,6 +1,8 @@ package app import ( + "os" + bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,6 +31,9 @@ const appName = "Ethermint" // application multi-store keys var ( + // default home directories for the application CLI + DefaultCLIHome = os.ExpandEnv("$HOME/.emintcli") + storeKeyAccount = sdk.NewKVStoreKey("acc") storeKeyStorage = sdk.NewKVStoreKey("contract_storage") storeKeyMain = sdk.NewKVStoreKey("main") diff --git a/cmd/emintcli/main.go b/cmd/emintcli/main.go index e769e031..1d3856db 100644 --- a/cmd/emintcli/main.go +++ b/cmd/emintcli/main.go @@ -1,7 +1,82 @@ package main +import ( + "os" + "path" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/keys" + "github.com/cosmos/cosmos-sdk/client/rpc" + sdk "github.com/cosmos/cosmos-sdk/types" + + emintapp "github.com/cosmos/ethermint/app" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/tendermint/tendermint/libs/cli" +) + func main() { - // TODO: Implement CLI commands and logic - // - // Ref: https://github.com/cosmos/ethermint/issues/432 + cobra.EnableCommandSorting = false + + // TODO: Set up codec + + // Read in the configuration file for the sdk + config := sdk.GetConfig() + config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) + config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) + config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) + config.Seal() + + rootCmd := &cobra.Command{ + Use: "emintcli", + Short: "Ethermint Client", + } + + // 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) + } + + // Construct Root Command + rootCmd.AddCommand( + rpc.StatusCommand(), + client.ConfigCmd(emintapp.DefaultCLIHome), + // TODO: Set up query command + // TODO: Set up tx command + // TODO: Set up rest routes (if included, different from web3 api) + // TODO: Set up web3 API setup command? + client.LineBreak, + keys.Commands(), + client.LineBreak, + ) + + executor := cli.PrepareMainCmd(rootCmd, "EM", emintapp.DefaultCLIHome) + err := executor.Execute() + if err != nil { + panic(err) + } +} + +func initConfig(cmd *cobra.Command) error { + home, err := cmd.PersistentFlags().GetString(cli.HomeFlag) + if err != nil { + 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 + } + return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag)) } diff --git a/go.mod b/go.mod index 2e962470..be5a83f2 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,8 @@ require ( github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709 // indirect github.com/pkg/errors v0.8.1 github.com/rjeczalik/notify v0.9.2 // indirect + github.com/spf13/cobra v0.0.5 + github.com/spf13/viper v1.3.2 github.com/stretchr/testify v1.3.0 github.com/syndtr/goleveldb v0.0.0-20181105012736-f9080354173f // indirect github.com/tendermint/lint v0.0.1 // indirect