Skip to content

Commit

Permalink
addressed reviewers comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbono3 committed Mar 23, 2021
1 parent b8e6f0e commit ff6eeeb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 66 deletions.
5 changes: 2 additions & 3 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/tendermint/tendermint/libs/cli"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -125,7 +124,7 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont
keyringBackend, _ := flagSet.GetString(flags.FlagKeyringBackend)

if keyringBackend != "" {
kr, err := NewKeyringFromFlags(clientCtx, keyringBackend)
kr, err := NewKeyringFromBackend(clientCtx, keyringBackend)
if err != nil {
return clientCtx, err
}
Expand All @@ -139,7 +138,7 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont
if rpcURI != "" {
clientCtx = clientCtx.WithNodeURI(rpcURI)

client, err := rpchttp.New(rpcURI, "/websocket")
client, err := NewClientFromNode(rpcURI)
if err != nil {
return clientCtx, err
}
Expand Down
19 changes: 7 additions & 12 deletions client/config/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"encoding/json"
"errors"
"fmt"
"path/filepath"

Expand All @@ -27,7 +26,6 @@ func Cmd() *cobra.Command {
}

func runConfigCmd(cmd *cobra.Command, args []string) error {

clientCtx := client.GetClientContextFromCmd(cmd)
configPath := filepath.Join(clientCtx.HomeDir, "config")

Expand All @@ -44,11 +42,13 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {

case 1:
// it's a get

key := args[0]

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 @@ -64,12 +64,13 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {

case 2:
// it's set

key, value := args[0], args[1]

switch key {
case flags.FlagChainID:
conf.SetChainID(value)
case flags.FlagKeyringDir:
conf.SetKeyringDir(value)
case flags.FlagKeyringBackend:
conf.SetKeyringBackend(value)
case tmcli.OutputFlag:
Expand All @@ -82,19 +83,13 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
return errUnknownConfigKey(key)
}

configTemplate, err := initConfigTemplate()
if err != nil {
return fmt.Errorf("could not initiate config template: %v", err)
}

confFile := filepath.Join(configPath, "client.toml")
if err := writeConfigFile(confFile, conf, configTemplate); err != nil {
if err := writeConfigToFile(confFile, conf); err != nil {
return fmt.Errorf("could not write client config to the file: %v", err)
}

default:
// print error
return errors.New("cound not execute config command")
panic("cound not execute config command")
}

return nil
Expand Down
28 changes: 13 additions & 15 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,26 @@ 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() *ClientConfig {
return &ClientConfig{chainID, keyringBackend, output, node, broadcastMode}
func DefaultClientConfig(keyringDir string) *ClientConfig {
return &ClientConfig{chainID, keyringDir, 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 @@ -52,24 +57,17 @@ func (c *ClientConfig) SetBroadcastMode(broadcastMode string) {

// ReadFromClientConfig reads values from client.toml file and updates them in client Context
func ReadFromClientConfig(ctx client.Context) (client.Context, error) {

configPath := filepath.Join(ctx.HomeDir, "config")
configFilePath := filepath.Join(configPath, "client.toml")

conf := DefaultClientConfig()
conf := DefaultClientConfig(ctx.HomeDir)

// 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) {
if err := ensureConfigPath(configPath); err != nil {
return ctx, fmt.Errorf("couldn't make client config: %v", err)
}

configTemplate, err := initConfigTemplate()
if err != nil {
return ctx, fmt.Errorf("could not initiate config template: %v", err)
}

if err := writeConfigFile(configFilePath, conf, configTemplate); err != nil {
if err := writeConfigToFile(configFilePath, conf); err != nil {
return ctx, fmt.Errorf("could not write client config to the file: %v", err)
}
}
Expand All @@ -78,19 +76,19 @@ func ReadFromClientConfig(ctx client.Context) (client.Context, error) {
if err != nil {
return ctx, fmt.Errorf("couldn't get client config: %v", err)
}
// we need to update KeyringDir field on Client Context first cause it is used in NewKeyringFromFlags
// we need to update KeyringDir field on Client Context first cause it is used in NewKeyringFromBackend
ctx = ctx.WithOutputFormat(conf.Output).
WithKeyringDir(ctx.HomeDir).
WithKeyringDir(conf.KeyringDir).
WithChainID(conf.ChainID)

keyring, err := client.NewKeyringFromFlags(ctx, conf.KeyringBackend)
keyring, err := client.NewKeyringFromBackend(ctx, conf.KeyringBackend)
if err != nil {
return ctx, fmt.Errorf("couldn't get key ring: %v", err)
}

ctx = ctx.WithKeyring(keyring)

client, err := newClientFromNodeFlag(conf.Node)
client, err := client.NewClientFromNode(conf.Node)
if err != nil {
return ctx, fmt.Errorf("couldn't get client from nodeURI: %v", err)
}
Expand Down
39 changes: 10 additions & 29 deletions client/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package config

import (
"bytes"
"io/ioutil"
"os"
"text/template"

"github.com/spf13/viper"
tmos "github.com/tendermint/tendermint/libs/os"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

const defaultConfigTemplate = `# This is a TOML config file.
Expand All @@ -19,56 +18,38 @@ const defaultConfigTemplate = `# This is a TOML config file.
chain-id = "{{ .ChainID }}"
keyringdir = "{{ .KeyringDir }}"
keyring-backend = "{{ .KeyringBackend }}"
output = "{{ .Output }}"
node = "{{ .Node }}"
broadcast-mode = "{{ .BroadcastMode }}"
`

// initConfigTemplate initiates config template that will be used in
// writeConfigFile
func initConfigTemplate() (*template.Template, error) {
// writeConfigToFile parses defaultConfigTemplate, renders config using the template and writes it to
// configFilePath.
func writeConfigToFile(configFilePath string, config *ClientConfig) error {
var buffer bytes.Buffer

tmpl := template.New("clientConfigFileTemplate")
configTemplate, err := tmpl.Parse(defaultConfigTemplate)
if err != nil {
return nil, err
return err
}

return configTemplate, nil
}

// writeConfigFile renders config using the template and writes it to
// configFilePath.
func writeConfigFile(configFilePath string, config *ClientConfig, configTemplate *template.Template) error {
var buffer bytes.Buffer

if err := configTemplate.Execute(&buffer, config); err != nil {
return err
}

tmos.MustWriteFile(configFilePath, buffer.Bytes(), 0644)
return nil

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

// ensureConfigPath creates a directory configPath if it does not exist
func ensureConfigPath(configPath string) error {
if err := os.MkdirAll(configPath, os.ModePerm); err != nil {
return err
}

return nil
}

// newClientFromNodeFlag sets up Client implementation that communicates with a Tendermint node over
// JSON RPC and WebSockets
func newClientFromNodeFlag(nodeURI string) (*rpchttp.HTTP, error) {
return rpchttp.New(nodeURI, "/websocket")
return os.MkdirAll(configPath, os.ModePerm)
}

// getClientConfig reads values from client.toml file and unmarshalls them into ClientConfig
func getClientConfig(configPath string, v *viper.Viper) (*ClientConfig, error) {

v.AddConfigPath(configPath)
v.SetConfigName("client")
v.SetConfigType("toml")
Expand Down
12 changes: 5 additions & 7 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ func (ctx Context) WithChainID(chainID string) Context {

// WithHomeDir returns a copy of the Context with HomeDir set.
func (ctx Context) WithHomeDir(dir string) Context {
ctx.HomeDir = dir
if dir != "" {
ctx.HomeDir = dir
}
return ctx
}

Expand Down Expand Up @@ -242,11 +244,7 @@ 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)

// maybe I should make this check if rootDir != "" inside WithHomeDir?
if rootDir != "" {
ctx = ctx.WithHomeDir(rootDir)
}
ctx = ctx.WithHomeDir(rootDir)
}

return ctx
Expand Down Expand Up @@ -362,7 +360,7 @@ func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddres
return info.GetAddress(), info.GetName(), info.GetType(), nil
}

func NewKeyringFromFlags(ctx Context, backend string) (keyring.Keyring, error) {
func NewKeyringFromBackend(ctx Context, backend string) (keyring.Keyring, error) {
if ctx.GenerateOnly || ctx.Simulate {
return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.KeyringDir, ctx.Input, ctx.KeyringOptions...)
}
Expand Down
8 changes: 8 additions & 0 deletions client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package client
import (
"github.com/spf13/pflag"

rpchttp "github.com/tendermint/tendermint/rpc/client/http"

"github.com/cosmos/cosmos-sdk/client/flags"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
Expand Down Expand Up @@ -67,3 +69,9 @@ func ReadPageRequest(flagSet *pflag.FlagSet) (*query.PageRequest, error) {
CountTotal: countTotal,
}, nil
}

// NewClientFromNode sets up Client implementation that communicates with a Tendermint node over
// JSON RPC and WebSockets
func NewClientFromNode(nodeURI string) (*rpchttp.HTTP, error) {
return rpchttp.New(nodeURI, "/websocket")
}

0 comments on commit ff6eeeb

Please sign in to comment.