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.

136 lines
2.7 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package config
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "flag"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. //"crypto/rand"
  13. //"encoding/hex"
  14. )
  15. /* Global & initialization */
  16. var RootDir string
  17. var Config Config_
  18. func setFlags(printHelp *bool) {
  19. flag.BoolVar(printHelp, "help", false, "Print this help message.")
  20. flag.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)")
  21. flag.StringVar(&Config.SeedNode, "seed", Config.SeedNode, "Address of seed node")
  22. }
  23. func ParseFlags() {
  24. RootDir = os.Getenv("TMROOT")
  25. if RootDir == "" {
  26. RootDir = os.Getenv("HOME") + "/.tendermint"
  27. }
  28. configFile := RootDir + "/config.json"
  29. // try to read configuration. if missing, write default
  30. configBytes, err := ioutil.ReadFile(configFile)
  31. if err != nil {
  32. defaultConfig.write(configFile)
  33. fmt.Println("Config file written to config.json. Please edit & run again")
  34. os.Exit(1)
  35. return
  36. }
  37. // try to parse configuration. on error, die
  38. Config = Config_{}
  39. err = json.Unmarshal(configBytes, &Config)
  40. if err != nil {
  41. log.Panicf("Invalid configuration file %s: %v", configFile, err)
  42. }
  43. err = Config.validate()
  44. if err != nil {
  45. log.Panicf("Invalid configuration file %s: %v", configFile, err)
  46. }
  47. // try to parse arg flags, which can override file configuration.
  48. var printHelp bool
  49. setFlags(&printHelp)
  50. flag.Parse()
  51. if printHelp {
  52. flag.PrintDefaults()
  53. os.Exit(0)
  54. }
  55. }
  56. /* Default configuration */
  57. var defaultConfig = Config_{
  58. Network: "tendermint_testnet0",
  59. LAddr: "0.0.0.0:0",
  60. SeedNode: "",
  61. Db: DbConfig{
  62. Type: "level",
  63. Dir: RootDir + "/data",
  64. },
  65. Twilio: TwilioConfig{},
  66. }
  67. /* Configuration types */
  68. type Config_ struct {
  69. Network string
  70. LAddr string
  71. SeedNode string
  72. Db DbConfig
  73. Twilio TwilioConfig
  74. }
  75. type TwilioConfig struct {
  76. Sid string
  77. Token string
  78. From string
  79. To string
  80. MinInterval int
  81. }
  82. type DbConfig struct {
  83. Type string
  84. Dir string
  85. }
  86. func (cfg *Config_) validate() error {
  87. if cfg.Network == "" {
  88. cfg.Network = defaultConfig.Network
  89. }
  90. if cfg.LAddr == "" {
  91. cfg.LAddr = defaultConfig.LAddr
  92. }
  93. if cfg.SeedNode == "" {
  94. cfg.SeedNode = defaultConfig.SeedNode
  95. }
  96. if cfg.Db.Type == "" {
  97. return errors.New("Db.Type must be set")
  98. }
  99. return nil
  100. }
  101. func (cfg *Config_) bytes() []byte {
  102. configBytes, err := json.MarshalIndent(cfg, "", "\t")
  103. if err != nil {
  104. panic(err)
  105. }
  106. return configBytes
  107. }
  108. func (cfg *Config_) write(configFile string) {
  109. if strings.Index(configFile, "/") != -1 {
  110. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  111. if err != nil {
  112. panic(err)
  113. }
  114. }
  115. err := ioutil.WriteFile(configFile, cfg.bytes(), 0600)
  116. if err != nil {
  117. panic(err)
  118. }
  119. }