Skip to content

Commit

Permalink
refactored,ready for review
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbono3 committed Mar 29, 2021
1 parent 1258d25 commit fe12325
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 38 deletions.
15 changes: 15 additions & 0 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
return clientCtx, nil
}

// ReadHomeFlag checks if home flag is changed.
// If this is a case, we update HomeDir field of Client Context
/* Discovered a bug with Cory
./build/simd init andrei --home ./test
cd test/config there is no client.toml configuration file
*/
func ReadHomeFlag(clientCtx Context, cmd *cobra.Command) Context {
if cmd.Flags().Changed(flags.FlagHome) {
rootDir, _ := cmd.Flags().GetString(flags.FlagHome)
clientCtx = clientCtx.WithHomeDir(rootDir)
}

return clientCtx
}

// GetClientQueryContext returns a Context from a command with fields set based on flags
// defined in AddQueryFlagsToCmd. An error is returned if any flag query fails.
//
Expand Down
4 changes: 0 additions & 4 deletions client/config/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
switch key {
case flags.FlagChainID:
cmd.Println(conf.ChainID)
case flags.FlagKeyringDir:
cmd.Println(conf.KeyringDir)
case flags.FlagKeyringBackend:
cmd.Println(conf.KeyringBackend)
case tmcli.OutputFlag:
Expand All @@ -69,8 +67,6 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
switch key {
case flags.FlagChainID:
conf.SetChainID(value)
case flags.FlagKeyringDir:
conf.SetKeyringDir(value)
case flags.FlagKeyringBackend:
conf.SetKeyringBackend(value)
case tmcli.OutputFlag:
Expand Down
16 changes: 6 additions & 10 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,21 @@ const (

type ClientConfig struct {
ChainID string `mapstructure:"chain-id" json:"chain-id"`
KeyringDir string `mapstructure:"keyringdir" json:"keyringdir"`
KeyringBackend string `mapstructure:"keyring-backend" json:"keyring-backend"`
Output string `mapstructure:"output" json:"output"`
Node string `mapstructure:"node" json:"node"`
BroadcastMode string `mapstructure:"broadcast-mode" json:"broadcast-mode"`
}

// DefaultClientConfig returns the reference to ClientConfig with default values.
func DefaultClientConfig(keyringDir string) *ClientConfig {
return &ClientConfig{chainID, keyringDir, keyringBackend, output, node, broadcastMode}
// defaultClientConfig returns the reference to ClientConfig with default values.
func defaultClientConfig() *ClientConfig {
return &ClientConfig{chainID, keyringBackend, output, node, broadcastMode}
}

func (c *ClientConfig) SetChainID(chainID string) {
c.ChainID = chainID
}

func (c *ClientConfig) SetKeyringDir(keyringDir string) {
c.KeyringDir = keyringDir
}

func (c *ClientConfig) SetKeyringBackend(keyringBackend string) {
c.KeyringBackend = keyringBackend
}
Expand All @@ -59,7 +54,7 @@ func (c *ClientConfig) SetBroadcastMode(broadcastMode string) {
func ReadFromClientConfig(ctx client.Context) (client.Context, error) {
configPath := filepath.Join(ctx.HomeDir, "config")
configFilePath := filepath.Join(configPath, "client.toml")
conf := DefaultClientConfig(ctx.HomeDir)
conf := defaultClientConfig()

// if config.toml file does not exist we create it and write default ClientConfig values into it.
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
Expand All @@ -78,7 +73,7 @@ func ReadFromClientConfig(ctx client.Context) (client.Context, error) {
}
// we need to update KeyringDir field on Client Context first cause it is used in NewKeyringFromBackend
ctx = ctx.WithOutputFormat(conf.Output).
WithKeyringDir(conf.KeyringDir).
WithKeyringDir(ctx.HomeDir).
WithChainID(conf.ChainID)

keyring, err := client.NewKeyringFromBackend(ctx, conf.KeyringBackend)
Expand All @@ -88,6 +83,7 @@ func ReadFromClientConfig(ctx client.Context) (client.Context, error) {

ctx = ctx.WithKeyring(keyring)

// https://github.com/cosmos/cosmos-sdk/issues/8986
client, err := client.NewClientFromNode(conf.Node)
if err != nil {
return ctx, fmt.Errorf("couldn't get client from nodeURI: %v", err)
Expand Down
3 changes: 1 addition & 2 deletions client/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const defaultConfigTemplate = `# This is a TOML config file.
chain-id = "{{ .ChainID }}"
keyringdir = "{{ .KeyringDir }}"
keyring-backend = "{{ .KeyringBackend }}"
output = "{{ .Output }}"
node = "{{ .Node }}"
Expand All @@ -40,7 +39,7 @@ func writeConfigToFile(configFilePath string, config *ClientConfig) error {
return err
}

return ioutil.WriteFile(configFilePath, buffer.Bytes(), 0644)
return ioutil.WriteFile(configFilePath, buffer.Bytes(), 0600)
}

// ensureConfigPath creates a directory configPath if it does not exist
Expand Down
21 changes: 2 additions & 19 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"gopkg.in/yaml.v2"
Expand All @@ -14,7 +13,6 @@ import (
"github.com/pkg/errors"
rpcclient "github.com/tendermint/tendermint/rpc/client"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -227,29 +225,14 @@ func (ctx Context) WithInterfaceRegistry(interfaceRegistry codectypes.InterfaceR
return ctx
}

// WithViper returns the context with Viper field
// WithViper returns the context with Viper field. This Viper instance is used to read
// client-side config from the config file.
func (ctx Context) WithViper() Context {
v := viper.New()
ctx.Viper = v
return ctx
}

// WithHomeFlag checks if home flag is changed.
// If this is a case, we update HomeDir field of Client Context
/* Discovered a bug with Cory
./build/simd init andrei --home ./test
cd test/config there is no client.toml configuration file
*/

func (ctx Context) WithHomeFlag(cmd *cobra.Command) Context {
if cmd.Flags().Changed(flags.FlagHome) {
rootDir, _ := cmd.Flags().GetString(flags.FlagHome)
ctx = ctx.WithHomeDir(rootDir)
}

return ctx
}

// PrintString prints the raw string to ctx.Output if it's defined, otherwise to os.Stdout
func (ctx Context) PrintString(str string) error {
return ctx.PrintBytes([]byte(str))
Expand Down
1 change: 1 addition & 0 deletions client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func ReadPageRequest(flagSet *pflag.FlagSet) (*query.PageRequest, error) {

// NewClientFromNode sets up Client implementation that communicates with a Tendermint node over
// JSON RPC and WebSockets
// https://github.com/cosmos/cosmos-sdk/issues/8986
func NewClientFromNode(nodeURI string) (*rpchttp.HTTP, error) {
return rpchttp.New(nodeURI, "/websocket")
}
6 changes: 3 additions & 3 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
Use: "simd",
Short: "simulation app",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
initClientCtx = initClientCtx.WithHomeFlag(cmd)
initClientCtx = client.ReadHomeFlag(initClientCtx, cmd)

initClientCtx, err := config.ReadFromClientConfig(initClientCtx)
if err != nil {
Expand Down Expand Up @@ -113,7 +113,7 @@ func queryCommand() *cobra.Command {
Short: "Querying subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
Expand All @@ -136,7 +136,7 @@ func txCommand() *cobra.Command {
Short: "Transactions subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
Expand Down

0 comments on commit fe12325

Please sign in to comment.