tendermint init to get the moniker

This commit is contained in:
Jae Kwon 2015-05-12 18:29:00 -07:00
parent 85c8850ac2
commit dd3d35f9f1
2 changed files with 45 additions and 12 deletions

View File

@ -16,6 +16,7 @@ func main() {
fmt.Println(`Tendermint fmt.Println(`Tendermint
Commands: Commands:
init Initialize the TMROOT directory
node Run the tendermint node node Run the tendermint node
gen_account Generate new account keypair gen_account Generate new account keypair
gen_validator Generate new validator keypair gen_validator Generate new validator keypair
@ -26,6 +27,9 @@ Commands:
} }
switch args[0] { switch args[0] {
case "init":
config.ParseFlags(args[1:])
logger.Reset()
case "node": case "node":
config.ParseFlags(args[1:]) config.ParseFlags(args[1:])
logger.Reset() logger.Reset()

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"bufio"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -32,11 +33,11 @@ func SetApp(a *confer.Config) {
} }
// NOTE: If you change this, maybe also change initDefaults() // NOTE: If you change this, maybe also change initDefaults()
var defaultConfig = `# This is a TOML config file. var defaultConfigTmpl = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml # For more information, see https://github.com/toml-lang/toml
network = "tendermint_testnet_5" network = "tendermint_testnet_5"
moniker = "anonymous" moniker = "__MONIKER__"
node_laddr = "0.0.0.0:46656" node_laddr = "0.0.0.0:46656"
seeds = "goldenalchemist.chaintest.net:46656" seeds = "goldenalchemist.chaintest.net:46656"
fast_sync = true fast_sync = true
@ -120,6 +121,7 @@ var DefaultGenesis = `{
] ]
}` }`
// If not defined in the process args nor config file, then use these defaults.
// NOTE: If you change this, maybe also change defaultConfig // NOTE: If you change this, maybe also change defaultConfig
func initDefaults(rootDir string) { func initDefaults(rootDir string) {
app.SetDefault("network", "tendermint_testnet0") app.SetDefault("network", "tendermint_testnet0")
@ -149,8 +151,15 @@ func Init(rootDir string) {
genesisFile := path.Join(rootDir, "genesis.json") genesisFile := path.Join(rootDir, "genesis.json")
// Write default config file if missing. // Write default config file if missing.
checkWriteFile(configFile, defaultConfig) if !fileExists(configFile) {
checkWriteFile(genesisFile, DefaultGenesis) // Ask user for moniker
moniker := getInput("Type hostname: ", "anonymous")
defaultConfig := strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
writeFile(configFile, defaultConfig)
}
if !fileExists(genesisFile) {
writeFile(genesisFile, DefaultGenesis)
}
// Initialize Config // Initialize Config
app = confer.NewConfig() app = confer.NewConfig()
@ -164,22 +173,42 @@ func Init(rootDir string) {
//app.Debug() //app.Debug()
} }
// Check if a file exists; if not, ensure the directory is made and write the file func getInput(prompt string, defaultValue string) string {
func checkWriteFile(configFile, contents string) { fmt.Print(prompt)
if _, err := os.Stat(configFile); os.IsNotExist(err) { reader := bufio.NewReader(os.Stdin)
if strings.Index(configFile, "/") != -1 { line, err := reader.ReadString('\n')
err := os.MkdirAll(filepath.Dir(configFile), 0700) if err != nil {
log.Warn("Error reading stdin", "err", err)
return defaultValue
} else {
line = strings.TrimSpace(line)
if line == "" {
return defaultValue
}
return line
}
}
func fileExists(file string) bool {
_, err := os.Stat(file)
return !os.IsNotExist(err)
}
func writeFile(file, contents string) {
if _, err := os.Stat(file); os.IsNotExist(err) {
if strings.Index(file, "/") != -1 {
err := os.MkdirAll(filepath.Dir(file), 0700)
if err != nil { if err != nil {
fmt.Printf("Could not create directory: %v", err) fmt.Printf("Could not create directory: %v", err)
os.Exit(1) os.Exit(1)
} }
} }
err := ioutil.WriteFile(configFile, []byte(contents), 0600) err := ioutil.WriteFile(file, []byte(contents), 0600)
if err != nil { if err != nil {
fmt.Printf("Could not write config file: %v", err) fmt.Printf("Could not write file: %v", err)
os.Exit(1) os.Exit(1)
} }
fmt.Printf("Config file written to %v.\n", configFile) fmt.Printf("File written to %v.\n", file)
} }
} }