-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from e-money/125-emd-config
125 emd config
- Loading branch information
Showing
3 changed files
with
101 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters