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.

138 lines
2.8 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. fmt.Println("----------------------------------")
  53. flag.PrintDefaults()
  54. fmt.Println("----------------------------------")
  55. os.Exit(0)
  56. }
  57. }
  58. /* Default configuration */
  59. var defaultConfig = Config_{
  60. Network: "tendermint_testnet0",
  61. LAddr: "0.0.0.0:0",
  62. SeedNode: "",
  63. Db: DbConfig{
  64. Type: "level",
  65. Dir: RootDir + "/data",
  66. },
  67. Twilio: TwilioConfig{},
  68. }
  69. /* Configuration types */
  70. type Config_ struct {
  71. Network string
  72. LAddr string
  73. SeedNode string
  74. Db DbConfig
  75. Twilio TwilioConfig
  76. }
  77. type TwilioConfig struct {
  78. Sid string
  79. Token string
  80. From string
  81. To string
  82. MinInterval int
  83. }
  84. type DbConfig struct {
  85. Type string
  86. Dir string
  87. }
  88. func (cfg *Config_) validate() error {
  89. if cfg.Network == "" {
  90. cfg.Network = defaultConfig.Network
  91. }
  92. if cfg.LAddr == "" {
  93. cfg.LAddr = defaultConfig.LAddr
  94. }
  95. if cfg.SeedNode == "" {
  96. cfg.SeedNode = defaultConfig.SeedNode
  97. }
  98. if cfg.Db.Type == "" {
  99. return errors.New("Db.Type must be set")
  100. }
  101. return nil
  102. }
  103. func (cfg *Config_) bytes() []byte {
  104. configBytes, err := json.MarshalIndent(cfg, "", "\t")
  105. if err != nil {
  106. panic(err)
  107. }
  108. return configBytes
  109. }
  110. func (cfg *Config_) write(configFile string) {
  111. if strings.Index(configFile, "/") != -1 {
  112. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  113. if err != nil {
  114. panic(err)
  115. }
  116. }
  117. err := ioutil.WriteFile(configFile, cfg.bytes(), 0600)
  118. if err != nil {
  119. panic(err)
  120. }
  121. }