config.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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("/etc/seaweedfs/") // path to look for the config file in
  20. glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
  21. if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
  22. glog.V(0).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
  23. if required {
  24. glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
  25. "\n\nPlease use this command to generate the default %s.toml file\n"+
  26. " weed scaffold -config=%s -output=.\n\n\n",
  27. configFileName, configFileName, configFileName)
  28. } else {
  29. return false
  30. }
  31. }
  32. return true
  33. }
  34. func GetViper() *viper.Viper {
  35. v := viper.GetViper()
  36. v.AutomaticEnv()
  37. v.SetEnvPrefix("weed")
  38. v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  39. return v
  40. }