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.

115 lines
2.9 KiB

13 years ago
4 years ago
4 years ago
  1. package util
  2. import (
  3. "strings"
  4. "sync"
  5. "github.com/spf13/viper"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. )
  8. var (
  9. ConfigurationFileDirectory DirectoryValueType
  10. )
  11. type DirectoryValueType string
  12. func (s *DirectoryValueType) Set(value string) error {
  13. *s = DirectoryValueType(value)
  14. return nil
  15. }
  16. func (s *DirectoryValueType) String() string {
  17. return string(*s)
  18. }
  19. type Configuration interface {
  20. GetString(key string) string
  21. GetBool(key string) bool
  22. GetInt(key string) int
  23. GetStringSlice(key string) []string
  24. SetDefault(key string, value interface{})
  25. }
  26. func LoadConfiguration(configFileName string, required bool) (loaded bool) {
  27. // find a filer store
  28. viper.SetConfigName(configFileName) // name of config file (without extension)
  29. viper.AddConfigPath(ResolvePath(ConfigurationFileDirectory.String())) // path to look for the config file in
  30. viper.AddConfigPath(".") // optionally look for config in the working directory
  31. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  32. viper.AddConfigPath("/usr/local/etc/seaweedfs/") // search path for bsd-style config directory in
  33. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  34. if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
  35. if strings.Contains(err.Error(), "Not Found") {
  36. glog.V(1).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
  37. } else {
  38. glog.Fatalf("Reading %s: %v", viper.ConfigFileUsed(), err)
  39. }
  40. if required {
  41. glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
  42. "\n\nPlease use this command to generate the default %s.toml file\n"+
  43. " weed scaffold -config=%s -output=.\n\n\n",
  44. configFileName, configFileName, configFileName)
  45. } else {
  46. return false
  47. }
  48. }
  49. glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
  50. return true
  51. }
  52. type ViperProxy struct {
  53. *viper.Viper
  54. sync.Mutex
  55. }
  56. var (
  57. vp = &ViperProxy{}
  58. )
  59. func (vp *ViperProxy) SetDefault(key string, value interface{}) {
  60. vp.Lock()
  61. defer vp.Unlock()
  62. vp.Viper.SetDefault(key, value)
  63. }
  64. func (vp *ViperProxy) GetString(key string) string {
  65. vp.Lock()
  66. defer vp.Unlock()
  67. return vp.Viper.GetString(key)
  68. }
  69. func (vp *ViperProxy) GetBool(key string) bool {
  70. vp.Lock()
  71. defer vp.Unlock()
  72. return vp.Viper.GetBool(key)
  73. }
  74. func (vp *ViperProxy) GetInt(key string) int {
  75. vp.Lock()
  76. defer vp.Unlock()
  77. return vp.Viper.GetInt(key)
  78. }
  79. func (vp *ViperProxy) GetStringSlice(key string) []string {
  80. vp.Lock()
  81. defer vp.Unlock()
  82. return vp.Viper.GetStringSlice(key)
  83. }
  84. func GetViper() *ViperProxy {
  85. vp.Lock()
  86. defer vp.Unlock()
  87. if vp.Viper == nil {
  88. vp.Viper = viper.GetViper()
  89. vp.AutomaticEnv()
  90. vp.SetEnvPrefix("weed")
  91. vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  92. }
  93. return vp
  94. }