config.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package util
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/spf13/viper"
  5. )
  6. type Configuration interface {
  7. GetString(key string) string
  8. GetBool(key string) bool
  9. GetInt(key string) int
  10. GetInt64(key string) int64
  11. GetFloat64(key string) float64
  12. GetStringSlice(key string) []string
  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 follow this example and add a filer.toml file to "+
  26. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n"+
  27. " https://github.com/chrislusf/seaweedfs/blob/master/weed/%s.toml\n"+
  28. "\nOr use this command to generate the default toml file\n"+
  29. " weed scaffold -config=%s -output=.\n\n\n",
  30. configFileName, configFileName, configFileName)
  31. } else {
  32. return false
  33. }
  34. }
  35. return true
  36. }