Skip to content

Commit

Permalink
Merge pull request #133 from e-money/125-emd-config
Browse files Browse the repository at this point in the history
125 emd config
  • Loading branch information
haasted authored Sep 8, 2021
2 parents f0be7fc + 5c95810 commit 36b750a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
79 changes: 79 additions & 0 deletions cmd/emd/cmd/client_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
)

// createDefaultConfig is forked from [email protected]/client/config/config.go:54
// The SDK function doesn't provide a way to override the configuration defaults, so creating a default
// client.toml file had to be moved out.
func createDefaultConfig(ctx client.Context) (client.Context, error) {
configPath := filepath.Join(ctx.HomeDir, "config")
configFilePath := filepath.Join(configPath, "client.toml")
conf := &config.ClientConfig{
ChainID: "",
KeyringBackend: "os",
Output: "json",
Node: "tcp://localhost:26657",
BroadcastMode: "block",
}

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

if err := writeConfigToFile(configFilePath, conf); err != nil {
return ctx, fmt.Errorf("could not write client config to the file: %v", err)
}
}

return ctx, nil
}

// The following is forked from [email protected]/client/config/toml.go
const defaultConfigTemplate = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml
###############################################################################
### Client Configuration ###
###############################################################################
# The network chain ID
chain-id = "{{ .ChainID }}"
# The keyring's backend, where the keys are stored (os|file|kwallet|pass|test|memory)
keyring-backend = "{{ .KeyringBackend }}"
# CLI output format (text|json)
output = "{{ .Output }}"
# <host>:<port> to Tendermint RPC interface for this chain
node = "{{ .Node }}"
# Transaction broadcasting mode (sync|async|block)
broadcast-mode = "{{ .BroadcastMode }}"
`

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

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

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

return ioutil.WriteFile(configFilePath, buffer.Bytes(), 0600)
}
26 changes: 21 additions & 5 deletions cmd/emd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package cmd

import (
"errors"
"io"
"os"
"path/filepath"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/debug"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/rpc"
Expand All @@ -26,10 +32,6 @@ import (
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
"io"
"os"
"path/filepath"
"time"
)

var (
Expand All @@ -50,12 +52,25 @@ func NewRootCmd() (*cobra.Command, emoney.EncodingConfig) {
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastBlock).
WithHomeDir(emoney.DefaultNodeHome)
WithHomeDir(emoney.DefaultNodeHome).
WithViper("")

rootCmd := &cobra.Command{
Use: "emd",
Short: "e-money app",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
initClientCtx = client.ReadHomeFlag(initClientCtx, cmd)

initClientCtx, err := createDefaultConfig(initClientCtx)
if err != nil {
return err
}

initClientCtx, err = config.ReadFromClientConfig(initClientCtx)
if err != nil {
return err
}

if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
return err
}
Expand Down Expand Up @@ -100,6 +115,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig emoney.EncodingConfig) {
tmcli.NewCompletionCmd(rootCmd, true),
testnetCmd(emoney.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
config.Cmd(),
)

a := appCreator{encCfg: encodingConfig}
Expand Down
2 changes: 1 addition & 1 deletion networktest/emcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (cli Emcli) QueryUpgSched() ([]byte, error) {
}

func (cli Emcli) QueryBuybackBalance() ([]byte, error) {
args := cli.addQueryFlags("query", "buyback", "balance")
args := cli.addQueryFlags("query", "buyback", "balances")
return execCmdAndCollectResponse(args)
}

Expand Down

0 comments on commit 36b750a

Please sign in to comment.