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.

95 lines
2.5 KiB

10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
  1. package config
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "sync"
  8. "time"
  9. )
  10. func Prompt(prompt string, defaultValue string) string {
  11. fmt.Print(prompt)
  12. reader := bufio.NewReader(os.Stdin)
  13. line, err := reader.ReadString('\n')
  14. if err != nil {
  15. log.Warn("Error reading stdin", "err", err)
  16. return defaultValue
  17. } else {
  18. line = strings.TrimSpace(line)
  19. if line == "" {
  20. return defaultValue
  21. }
  22. return line
  23. }
  24. }
  25. type Config interface {
  26. Get(key string) interface{}
  27. GetBool(key string) bool
  28. GetFloat64(key string) float64
  29. GetInt(key string) int
  30. GetString(key string) string
  31. GetStringMap(key string) map[string]interface{}
  32. GetStringMapString(key string) map[string]string
  33. GetStringSlice(key string) []string
  34. GetTime(key string) time.Time
  35. IsSet(key string) bool
  36. Set(key string, value interface{})
  37. }
  38. type MapConfig map[string]interface{}
  39. func (cfg MapConfig) Get(key string) interface{} { return cfg[key] }
  40. func (cfg MapConfig) GetBool(key string) bool { return cfg[key].(bool) }
  41. func (cfg MapConfig) GetFloat64(key string) float64 { return cfg[key].(float64) }
  42. func (cfg MapConfig) GetInt(key string) int { return cfg[key].(int) }
  43. func (cfg MapConfig) GetString(key string) string { return cfg[key].(string) }
  44. func (cfg MapConfig) GetStringMap(key string) map[string]interface{} {
  45. return cfg[key].(map[string]interface{})
  46. }
  47. func (cfg MapConfig) GetStringMapString(key string) map[string]string {
  48. return cfg[key].(map[string]string)
  49. }
  50. func (cfg MapConfig) GetStringSlice(key string) []string { return cfg[key].([]string) }
  51. func (cfg MapConfig) GetTime(key string) time.Time { return cfg[key].(time.Time) }
  52. func (cfg MapConfig) IsSet(key string) bool { _, ok := cfg[key]; return ok }
  53. func (cfg MapConfig) Set(key string, value interface{}) { cfg[key] = value }
  54. func (cfg MapConfig) SetDefault(key string, value interface{}) {
  55. if cfg.IsSet(key) {
  56. return
  57. }
  58. cfg[key] = value
  59. }
  60. //--------------------------------------------------------------------------------
  61. // A little convenient hack to notify listeners upon config changes.
  62. type Configurable func(Config)
  63. var mtx sync.Mutex
  64. var globalConfig Config
  65. var confs []Configurable
  66. func OnConfig(conf func(Config)) {
  67. mtx.Lock()
  68. defer mtx.Unlock()
  69. confs = append(confs, conf)
  70. if globalConfig != nil {
  71. conf(globalConfig)
  72. }
  73. }
  74. func ApplyConfig(config Config) {
  75. mtx.Lock()
  76. globalConfig = config
  77. confsCopy := make([]Configurable, len(confs))
  78. copy(confsCopy, confs)
  79. mtx.Unlock()
  80. for _, conf := range confsCopy {
  81. conf(config)
  82. }
  83. }