|
|
@ -7,13 +7,23 @@ import ( |
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
"sync" |
|
|
|
|
|
|
|
flag "github.com/spf13/pflag" |
|
|
|
"github.com/tendermint/confer" |
|
|
|
) |
|
|
|
|
|
|
|
var rootDir string |
|
|
|
var App *confer.Config |
|
|
|
var app *confer.Config |
|
|
|
var appMtx sync.Mutex |
|
|
|
|
|
|
|
func App() *confer.Config { |
|
|
|
appMtx.Lock() |
|
|
|
defer appMtx.Unlock() |
|
|
|
if app == nil { |
|
|
|
Init("") |
|
|
|
} |
|
|
|
return app |
|
|
|
} |
|
|
|
|
|
|
|
// NOTE: If you change this, maybe also change initDefaults()
|
|
|
|
var defaultConfig = `# This is a TOML config file. |
|
|
@ -75,44 +85,27 @@ var defaultGenesis = ` |
|
|
|
` |
|
|
|
|
|
|
|
// NOTE: If you change this, maybe also change defaultConfig
|
|
|
|
func initDefaults() { |
|
|
|
App.SetDefault("Network", "tendermint_testnet0") |
|
|
|
App.SetDefault("ListenAddr", "0.0.0.0:8080") |
|
|
|
App.SetDefault("DB.Backend", "leveldb") |
|
|
|
App.SetDefault("DB.Dir", rootDir+"/data") |
|
|
|
App.SetDefault("Log.Stdout.Level", "info") |
|
|
|
App.SetDefault("Log.File.Dir", rootDir+"/log") |
|
|
|
App.SetDefault("Log.File.Level", "debug") |
|
|
|
App.SetDefault("RPC.HTTP.ListenAddr", "127.0.0.1:8081") |
|
|
|
|
|
|
|
App.SetDefault("GenesisFile", rootDir+"/genesis.json") |
|
|
|
App.SetDefault("AddrBookFile", rootDir+"/addrbook.json") |
|
|
|
App.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json") |
|
|
|
} |
|
|
|
|
|
|
|
// Check if a file exists; if not, ensure the directory is made and write the file
|
|
|
|
func checkWriteFile(configFile, contents string) { |
|
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) { |
|
|
|
if strings.Index(configFile, "/") != -1 { |
|
|
|
err := os.MkdirAll(filepath.Dir(configFile), 0700) |
|
|
|
if err != nil { |
|
|
|
fmt.Printf("Could not create directory: %v", err) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
} |
|
|
|
err := ioutil.WriteFile(configFile, []byte(contents), 0600) |
|
|
|
if err != nil { |
|
|
|
fmt.Printf("Could not write config file: %v", err) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
fmt.Printf("Config file written to %v.\n", configFile) |
|
|
|
} |
|
|
|
func initDefaults(rootDir string) { |
|
|
|
app.SetDefault("Network", "tendermint_testnet0") |
|
|
|
app.SetDefault("ListenAddr", "0.0.0.0:8080") |
|
|
|
app.SetDefault("DB.Backend", "leveldb") |
|
|
|
app.SetDefault("DB.Dir", rootDir+"/data") |
|
|
|
app.SetDefault("Log.Stdout.Level", "info") |
|
|
|
app.SetDefault("Log.File.Dir", rootDir+"/log") |
|
|
|
app.SetDefault("Log.File.Level", "debug") |
|
|
|
app.SetDefault("RPC.HTTP.ListenAddr", "0.0.0.0:8081") |
|
|
|
|
|
|
|
app.SetDefault("GenesisFile", rootDir+"/genesis.json") |
|
|
|
app.SetDefault("AddrBookFile", rootDir+"/addrbook.json") |
|
|
|
app.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json") |
|
|
|
} |
|
|
|
|
|
|
|
func init() { |
|
|
|
func Init(rootDir string) { |
|
|
|
|
|
|
|
// Get RootDir
|
|
|
|
rootDir = os.Getenv("TMROOT") |
|
|
|
if rootDir == "" { |
|
|
|
rootDir = os.Getenv("TMROOT") |
|
|
|
} |
|
|
|
if rootDir == "" { |
|
|
|
rootDir = os.Getenv("HOME") + "/.tendermint" |
|
|
|
} |
|
|
@ -124,15 +117,34 @@ func init() { |
|
|
|
checkWriteFile(genesisFile, defaultGenesis) |
|
|
|
|
|
|
|
// Initialize Config
|
|
|
|
App = confer.NewConfig() |
|
|
|
initDefaults() |
|
|
|
app = confer.NewConfig() |
|
|
|
initDefaults(rootDir) |
|
|
|
paths := []string{configFile} |
|
|
|
if err := App.ReadPaths(paths...); err != nil { |
|
|
|
if err := app.ReadPaths(paths...); err != nil { |
|
|
|
log.Warn("Error reading configuration", "paths", paths, "error", err) |
|
|
|
} |
|
|
|
|
|
|
|
// Confused?
|
|
|
|
// App.Debug()
|
|
|
|
// app.Debug()
|
|
|
|
} |
|
|
|
|
|
|
|
// Check if a file exists; if not, ensure the directory is made and write the file
|
|
|
|
func checkWriteFile(configFile, contents string) { |
|
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) { |
|
|
|
if strings.Index(configFile, "/") != -1 { |
|
|
|
err := os.MkdirAll(filepath.Dir(configFile), 0700) |
|
|
|
if err != nil { |
|
|
|
fmt.Printf("Could not create directory: %v", err) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
} |
|
|
|
err := ioutil.WriteFile(configFile, []byte(contents), 0600) |
|
|
|
if err != nil { |
|
|
|
fmt.Printf("Could not write config file: %v", err) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
fmt.Printf("Config file written to %v.\n", configFile) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func ParseFlags(args []string) { |
|
|
@ -141,20 +153,20 @@ func ParseFlags(args []string) { |
|
|
|
|
|
|
|
// Declare flags
|
|
|
|
flags.BoolVar(&printHelp, "help", false, "Print this help message.") |
|
|
|
flags.String("listen_addr", App.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)") |
|
|
|
flags.String("seed_node", App.GetString("SeedNode"), "Address of seed node") |
|
|
|
flags.String("rpc_http_listen_addr", App.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required") |
|
|
|
flags.String("listen_addr", app.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)") |
|
|
|
flags.String("seed_node", app.GetString("SeedNode"), "Address of seed node") |
|
|
|
flags.String("rpc_http_listen_addr", app.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required") |
|
|
|
flags.Parse(args) |
|
|
|
if printHelp { |
|
|
|
flags.PrintDefaults() |
|
|
|
os.Exit(0) |
|
|
|
} |
|
|
|
|
|
|
|
// Merge parsed flag values onto App.
|
|
|
|
App.BindPFlag("ListenAddr", flags.Lookup("listen_addr")) |
|
|
|
App.BindPFlag("SeedNode", flags.Lookup("seed_node")) |
|
|
|
App.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr")) |
|
|
|
// Merge parsed flag values onto app.
|
|
|
|
app.BindPFlag("ListenAddr", flags.Lookup("listen_addr")) |
|
|
|
app.BindPFlag("SeedNode", flags.Lookup("seed_node")) |
|
|
|
app.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr")) |
|
|
|
|
|
|
|
// Confused?
|
|
|
|
//App.Debug()
|
|
|
|
//app.Debug()
|
|
|
|
} |