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.

126 lines
3.3 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 = "23.239.22.253:8080"
  20. [DB]
  21. # The only other available backend is "memdb"
  22. Backend = "leveldb"
  23. # Dir = "~/.tendermint/data"
  24. [Log.Stdout]
  25. Level = "info"
  26. [Log.File]
  27. Level = "debug"
  28. # Dir = "~/.tendermint/log"
  29. [RPC.HTTP]
  30. # For the RPC API HTTP server. Port required.
  31. ListenAddr = "127.0.0.1:8081"
  32. [Alert]
  33. # TODO: Document options
  34. [SMTP]
  35. # TODO: Document options
  36. `
  37. // NOTE: If you change this, maybe also change defaultConfig
  38. func initDefaults() {
  39. App.SetDefault("Network", "tendermint_testnet0")
  40. App.SetDefault("ListenAddr", "0.0.0.0:8080")
  41. App.SetDefault("DB.Backend", "leveldb")
  42. App.SetDefault("DB.Dir", rootDir+"/data")
  43. App.SetDefault("Log.Stdout.Level", "info")
  44. App.SetDefault("Log.File.Dir", rootDir+"/log")
  45. App.SetDefault("Log.File.Level", "debug")
  46. App.SetDefault("RPC.HTTP.ListenAddr", "127.0.0.1:8081")
  47. App.SetDefault("GenesisFile", rootDir+"/genesis.json")
  48. App.SetDefault("AddrBookFile", rootDir+"/addrbook.json")
  49. App.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json")
  50. }
  51. func init() {
  52. // Get RootDir
  53. rootDir = os.Getenv("TMROOT")
  54. if rootDir == "" {
  55. rootDir = os.Getenv("HOME") + "/.tendermint"
  56. }
  57. configFile := rootDir + "/config.toml"
  58. // Write default config file if missing.
  59. if _, err := os.Stat(configFile); os.IsNotExist(err) {
  60. if strings.Index(configFile, "/") != -1 {
  61. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  62. if err != nil {
  63. fmt.Printf("Could not create directory: %v", err)
  64. os.Exit(1)
  65. }
  66. }
  67. err := ioutil.WriteFile(configFile, []byte(defaultConfig), 0600)
  68. if err != nil {
  69. fmt.Printf("Could not write config file: %v", err)
  70. os.Exit(1)
  71. }
  72. fmt.Printf("Config file written to %v. Please edit & run again\n", configFile)
  73. os.Exit(1)
  74. }
  75. // Initialize Config
  76. App = confer.NewConfig()
  77. initDefaults()
  78. paths := []string{configFile}
  79. if err := App.ReadPaths(paths...); err != nil {
  80. log.Warn("Error reading configuration", "paths", paths, "error", err)
  81. }
  82. // Confused?
  83. // App.Debug()
  84. }
  85. func ParseFlags(args []string) {
  86. var flags = flag.NewFlagSet("main", flag.ExitOnError)
  87. var printHelp = false
  88. // Declare flags
  89. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  90. flags.String("listen_addr", App.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)")
  91. flags.String("seed_node", App.GetString("SeedNode"), "Address of seed node")
  92. flags.String("rpc_http_listen_addr", App.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required")
  93. flags.Parse(args)
  94. if printHelp {
  95. flags.PrintDefaults()
  96. os.Exit(0)
  97. }
  98. // Merge parsed flag values onto App.
  99. App.BindPFlag("ListenAddr", flags.Lookup("listen_addr"))
  100. App.BindPFlag("SeedNode", flags.Lookup("seed_node"))
  101. App.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr"))
  102. // Confused?
  103. //App.Debug()
  104. }