From a99885cd439a02b5aa60e395f1499dcd12a247f1 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Mon, 20 Feb 2017 11:13:44 +0400 Subject: [PATCH] do not overwrite existing files when doing `tendermint init` (Fixes #410) --- cmd/tendermint/init.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/cmd/tendermint/init.go b/cmd/tendermint/init.go index 8a7a7b096..5ec1efefb 100644 --- a/cmd/tendermint/init.go +++ b/cmd/tendermint/init.go @@ -1,24 +1,35 @@ package main import ( - . "github.com/tendermint/go-common" + "os" + + cmn "github.com/tendermint/go-common" "github.com/tendermint/tendermint/types" ) func init_files() { - privValidator := types.GenPrivValidator() - privValidator.SetFile(config.GetString("priv_validator_file")) - privValidator.Save() + privValFile := config.GetString("priv_validator_file") + if _, err := os.Stat(privValFile); os.IsNotExist(err) { + privValidator := types.GenPrivValidator() + privValidator.SetFile(privValFile) + privValidator.Save() - genDoc := types.GenesisDoc{ - ChainID: Fmt("test-chain-%v", RandStr(6)), - } - genDoc.Validators = []types.GenesisValidator{types.GenesisValidator{ - PubKey: privValidator.PubKey, - Amount: 10, - }} + genFile := config.GetString("genesis_file") - genDoc.SaveAs(config.GetString("genesis_file")) + if _, err := os.Stat(genFile); os.IsNotExist(err) { + genDoc := types.GenesisDoc{ + ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)), + } + genDoc.Validators = []types.GenesisValidator{types.GenesisValidator{ + PubKey: privValidator.PubKey, + Amount: 10, + }} - log.Notice("Initialized tendermint", "genesis", config.GetString("genesis_file"), "priv_validator", config.GetString("priv_validator_file")) + genDoc.SaveAs(genFile) + } + + log.Notice("Initialized tendermint", "genesis", config.GetString("genesis_file"), "priv_validator", config.GetString("priv_validator_file")) + } else { + log.Notice("Already initialized", "priv_validator", config.GetString("priv_validator_file")) + } }