You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. flag "github.com/spf13/pflag"
  9. "github.com/tendermint/confer"
  10. )
  11. var rootDir string
  12. var App *confer.Config
  13. // NOTE: If you change this, maybe also change initDefaults()
  14. var defaultConfig = `# This is a TOML config file.
  15. # For more information, see https://github.com/toml-lang/toml
  16. Network = "tendermint_testnet0"
  17. ListenAddr = "0.0.0.0:8080"
  18. # First node to connect to. Command-line overridable.
  19. # SeedNode = "a.b.c.d:pppp"
  20. [DB]
  21. # The only other available backend is "memdb"
  22. Backend = "leveldb"
  23. # The leveldb data directory.
  24. # Dir = "<YOUR_HOME_DIRECTORY>/.tendermint/data"
  25. [RPC.HTTP]
  26. # For the RPC API HTTP server. Port required.
  27. ListenAddr = "0.0.0.0:8081"
  28. [Alert]
  29. # TODO: Document options
  30. [SMTP]
  31. # TODO: Document options
  32. `
  33. // NOTE: If you change this, maybe also change defaultConfig
  34. func initDefaults() {
  35. App.SetDefault("Network", "tendermint_testnet0")
  36. App.SetDefault("ListenAddr", "0.0.0.0:8080")
  37. App.SetDefault("DB.Backend", "leveldb")
  38. App.SetDefault("DB.Dir", rootDir+"/data")
  39. App.SetDefault("Log.Level", "debug")
  40. App.SetDefault("Log.Dir", rootDir+"/log")
  41. App.SetDefault("RPC.HTTP.ListenAddr", "0.0.0.0:8081")
  42. App.SetDefault("GenesisFile", rootDir+"/genesis.json")
  43. App.SetDefault("AddrbookFile", rootDir+"/addrbook.json")
  44. App.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json")
  45. }
  46. func init() {
  47. // Get RootDir
  48. rootDir = os.Getenv("TMROOT")
  49. if rootDir == "" {
  50. rootDir = os.Getenv("HOME") + "/.tendermint"
  51. }
  52. configFile := rootDir + "/config.toml"
  53. // Write default config file if missing.
  54. if _, err := os.Stat(configFile); os.IsNotExist(err) {
  55. if strings.Index(configFile, "/") != -1 {
  56. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  57. if err != nil {
  58. fmt.Printf("Could not create directory: %v", err)
  59. os.Exit(1)
  60. }
  61. }
  62. err := ioutil.WriteFile(configFile, []byte(defaultConfig), 0600)
  63. if err != nil {
  64. fmt.Printf("Could not write config file: %v", err)
  65. os.Exit(1)
  66. }
  67. fmt.Printf("Config file written to %v. Please edit & run again\n", configFile)
  68. os.Exit(1)
  69. }
  70. // Initialize Config
  71. App = confer.NewConfig()
  72. initDefaults()
  73. paths := []string{configFile}
  74. if err := App.ReadPaths(paths...); err != nil {
  75. log.Warn("Error reading configuration", "paths", paths, "error", err)
  76. }
  77. // Confused?
  78. // App.Debug()
  79. }
  80. func ParseFlags(args []string) {
  81. var flags = flag.NewFlagSet("main", flag.ExitOnError)
  82. var printHelp = false
  83. // Declare flags
  84. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  85. flags.String("listen_addr", App.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)")
  86. flags.String("seed_node", App.GetString("SeedNode"), "Address of seed node")
  87. flags.String("rpc_http_listen_addr", App.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required")
  88. flags.Parse(args)
  89. if printHelp {
  90. flags.PrintDefaults()
  91. os.Exit(0)
  92. }
  93. // Merge parsed flag values onto App.
  94. App.BindPFlag("ListenAddr", flags.Lookup("listen_addr"))
  95. App.BindPFlag("SeedNode", flags.Lookup("seed_node"))
  96. App.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr"))
  97. // Confused?
  98. //App.Debug()
  99. }