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.

130 lines
2.6 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. package util
  2. // Copyright 2011 Numerotron Inc.
  3. // Use of this source code is governed by an MIT-style license
  4. // that can be found in the LICENSE file.
  5. //
  6. // Developed at www.stathat.com by Patrick Crosby
  7. // Contact us on twitter with any questions: twitter.com/stat_hat
  8. // The jconfig package provides a simple, basic configuration file parser using JSON.
  9. import (
  10. "bytes"
  11. "encoding/json"
  12. "os"
  13. "github.com/chrislusf/weed-fs/go/glog"
  14. )
  15. type Config struct {
  16. data map[string]interface{}
  17. filename string
  18. }
  19. func newConfig() *Config {
  20. result := new(Config)
  21. result.data = make(map[string]interface{})
  22. return result
  23. }
  24. // Loads config information from a JSON file
  25. func LoadConfig(filename string) *Config {
  26. result := newConfig()
  27. result.filename = filename
  28. err := result.parse()
  29. if err != nil {
  30. glog.Fatalf("error loading config file %s: %s", filename, err)
  31. }
  32. return result
  33. }
  34. // Loads config information from a JSON string
  35. func LoadConfigString(s string) *Config {
  36. result := newConfig()
  37. err := json.Unmarshal([]byte(s), &result.data)
  38. if err != nil {
  39. glog.Fatalf("error parsing config string %s: %s", s, err)
  40. }
  41. return result
  42. }
  43. func (c *Config) StringMerge(s string) {
  44. next := LoadConfigString(s)
  45. c.merge(next.data)
  46. }
  47. func (c *Config) LoadMerge(filename string) {
  48. next := LoadConfig(filename)
  49. c.merge(next.data)
  50. }
  51. func (c *Config) merge(ndata map[string]interface{}) {
  52. for k, v := range ndata {
  53. c.data[k] = v
  54. }
  55. }
  56. func (c *Config) parse() error {
  57. f, err := os.Open(c.filename)
  58. if err != nil {
  59. return err
  60. }
  61. defer f.Close()
  62. b := new(bytes.Buffer)
  63. _, err = b.ReadFrom(f)
  64. if err != nil {
  65. return err
  66. }
  67. err = json.Unmarshal(b.Bytes(), &c.data)
  68. if err != nil {
  69. return err
  70. }
  71. return nil
  72. }
  73. // Returns a string for the config variable key
  74. func (c *Config) GetString(key string) string {
  75. result, present := c.data[key]
  76. if !present {
  77. return ""
  78. }
  79. return result.(string)
  80. }
  81. // Returns an int for the config variable key
  82. func (c *Config) GetInt(key string) int {
  83. x, ok := c.data[key]
  84. if !ok {
  85. return -1
  86. }
  87. return int(x.(float64))
  88. }
  89. // Returns a float for the config variable key
  90. func (c *Config) GetFloat(key string) float64 {
  91. x, ok := c.data[key]
  92. if !ok {
  93. return -1
  94. }
  95. return x.(float64)
  96. }
  97. // Returns a bool for the config variable key
  98. func (c *Config) GetBool(key string) bool {
  99. x, ok := c.data[key]
  100. if !ok {
  101. return false
  102. }
  103. return x.(bool)
  104. }
  105. // Returns an array for the config variable key
  106. func (c *Config) GetArray(key string) []interface{} {
  107. result, present := c.data[key]
  108. if !present {
  109. return []interface{}(nil)
  110. }
  111. return result.([]interface{})
  112. }