forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR cosmos#2223: Gaia CLI Config Command
* Allow a gaia-cli config file to be created Closes cosmos#1613. Closes cosmos#1275. Closes cosmos#1956. * Add homedir to Gopkg.toml * Updates from code review * Post-rebase fixes * Update test * Code review refactor * Fixes from code review * Fix import * Fix broken test * Fixes from rebase * Fix formatting
- Loading branch information
1 parent
24413a3
commit 2fb3493
Showing
18 changed files
with
298 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,131 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/mitchellh/go-homedir" | ||
"bufio" | ||
"path" | ||
"os" | ||
"io/ioutil" | ||
"github.com/pelletier/go-toml" | ||
"fmt" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type cliConfig struct { | ||
Home string `toml:"home"` | ||
ChainID string `toml:"chain_id"` | ||
TrustNode bool `toml:"trust_node"` | ||
Encoding string `toml:"encoding"` | ||
Output string `toml:"output"` | ||
Node string `toml:"node"` | ||
Trace bool `toml:"trace"` | ||
} | ||
|
||
// ConfigCmd returns a CLI command to interactively create a | ||
// Gaia CLI config file. | ||
func ConfigCmd() *cobra.Command { | ||
cfg := &cobra.Command{ | ||
Use: "config", | ||
Short: "Interactively creates a Gaia CLI config file", | ||
RunE: runConfigCmd, | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
func runConfigCmd(cmd *cobra.Command, args [] string) error { | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stdin := BufferStdin() | ||
gaiaCLIHome, err := handleGaiaCLIHome(home, stdin) | ||
if err != nil { | ||
return err | ||
} | ||
node, err := handleNode(stdin) | ||
if err != nil { | ||
return err | ||
} | ||
trustNode, err := handleTrustNode(stdin) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
encoding := "btc" | ||
output := "text" | ||
var chainID string | ||
chainID, err = types.DefaultChainID() | ||
if err != nil { | ||
fmt.Println("Couldn't populate ChainID, so using an empty one.") | ||
} | ||
|
||
cfg := &cliConfig{ | ||
Home: gaiaCLIHome, | ||
ChainID: chainID, | ||
TrustNode: trustNode, | ||
Encoding: encoding, | ||
Output: output, | ||
Node: node, | ||
Trace: false, | ||
} | ||
|
||
return createGaiaCLIConfig(cfg) | ||
} | ||
|
||
func handleGaiaCLIHome(dir string, stdin *bufio.Reader) (string, error) { | ||
dirName := ".gaiacli" | ||
home, err := GetString(fmt.Sprintf("Where is your gaiacli home directory? (Default: ~/%s)", dirName), stdin) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if home == "" { | ||
home = path.Join(dir, dirName) | ||
} | ||
|
||
return home, nil | ||
} | ||
|
||
func handleNode(stdin *bufio.Reader) (string, error) { | ||
defaultNode := "tcp://localhost:26657" | ||
node, err := GetString(fmt.Sprintf("Where is your validator node running? (Default: %s)", defaultNode), stdin) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if node == "" { | ||
node = defaultNode | ||
} | ||
|
||
return node, nil | ||
} | ||
|
||
func handleTrustNode(stdin *bufio.Reader) (bool, error) { | ||
return GetConfirmation("Do you trust this node?", stdin) | ||
} | ||
|
||
func createGaiaCLIConfig(cfg *cliConfig) error { | ||
cfgPath := path.Join(cfg.Home, "config") | ||
err := os.MkdirAll(cfgPath, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := toml.Marshal(*cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cfgFile := path.Join(cfgPath, "config.toml") | ||
if info, err := os.Stat(cfgFile); err == nil && !info.IsDir() { | ||
err = os.Rename(cfgFile, path.Join(cfgPath, "config.toml-old")) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return ioutil.WriteFile(cfgFile, data, os.ModePerm) | ||
} |
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
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
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
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
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
Oops, something went wrong.