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.

59 lines
1.8 KiB

5 years ago
13 years ago
5 years ago
5 years ago
5 years ago
  1. package util
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/spf13/viper"
  6. )
  7. type Configuration interface {
  8. GetString(key string) string
  9. GetBool(key string) bool
  10. GetInt(key string) int
  11. GetInt64(key string) int64
  12. GetFloat64(key string) float64
  13. GetStringSlice(key string) []string
  14. }
  15. func LoadConfiguration(configFileName string, required bool) (loaded bool) {
  16. // find a filer store
  17. viper.SetConfigName(configFileName) // name of config file (without extension)
  18. viper.AddConfigPath(".") // optionally look for config in the working directory
  19. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  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. glog.V(0).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
  24. if required {
  25. glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
  26. "\n\nPlease follow this example and add a filer.toml file to "+
  27. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n"+
  28. " https://github.com/chrislusf/seaweedfs/blob/master/weed/%s.toml\n"+
  29. "\nOr use this command to generate the default toml file\n"+
  30. " weed scaffold -config=%s -output=.\n\n\n",
  31. configFileName, configFileName, configFileName)
  32. } else {
  33. return false
  34. }
  35. }
  36. return true
  37. }
  38. func Config() Configuration {
  39. return viper.GetViper()
  40. }
  41. func SubConfig(subKey string) (Configuration, error) {
  42. if subKey != "" {
  43. sub := viper.GetViper().Sub(subKey)
  44. if sub == nil {
  45. return nil, fmt.Errorf("sub config [%s] not exist", subKey)
  46. }
  47. return sub, nil
  48. }
  49. return viper.GetViper(), nil
  50. }