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.

56 lines
1.7 KiB

13 years ago
  1. package util
  2. import (
  3. "strings"
  4. "github.com/spf13/viper"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. )
  7. type Configuration interface {
  8. GetString(key string) string
  9. GetBool(key string) bool
  10. GetInt(key string) int
  11. GetStringSlice(key string) []string
  12. SetDefault(key string, value interface{})
  13. }
  14. func LoadConfiguration(configFileName string, required bool) (loaded bool) {
  15. // find a filer store
  16. viper.SetConfigName(configFileName) // name of config file (without extension)
  17. viper.AddConfigPath(".") // optionally look for config in the working directory
  18. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  19. viper.AddConfigPath("/usr/local/etc/seaweedfs/") // search path for bsd-style config directory in
  20. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  21. glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
  22. if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
  23. logLevel := glog.Level(0)
  24. if strings.Contains(err.Error(), "Not Found") {
  25. logLevel = 1
  26. }
  27. glog.V(logLevel).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
  28. if required {
  29. glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
  30. "\n\nPlease use this command to generate the default %s.toml file\n"+
  31. " weed scaffold -config=%s -output=.\n\n\n",
  32. configFileName, configFileName, configFileName)
  33. } else {
  34. return false
  35. }
  36. }
  37. return true
  38. }
  39. func GetViper() *viper.Viper {
  40. v := &viper.Viper{}
  41. *v = *viper.GetViper()
  42. v.AutomaticEnv()
  43. v.SetEnvPrefix("weed")
  44. v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  45. return v
  46. }