2015-03-10 15:20:31 +08:00
|
|
|
package util
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
import (
|
2020-01-30 01:09:55 +08:00
|
|
|
"strings"
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
"github.com/spf13/viper"
|
2020-01-30 01:09:55 +08:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2019-06-05 16:30:24 +08:00
|
|
|
)
|
|
|
|
|
2018-08-20 06:17:55 +08:00
|
|
|
type Configuration interface {
|
|
|
|
GetString(key string) string
|
|
|
|
GetBool(key string) bool
|
|
|
|
GetInt(key string) int
|
|
|
|
GetStringSlice(key string) []string
|
2019-12-21 12:56:14 +08:00
|
|
|
SetDefault(key string, value interface{})
|
2012-08-24 11:56:09 +08:00
|
|
|
}
|
2019-06-05 16:30:24 +08:00
|
|
|
|
|
|
|
func LoadConfiguration(configFileName string, required bool) (loaded bool) {
|
|
|
|
|
|
|
|
// find a filer store
|
2020-12-22 18:34:08 +08:00
|
|
|
viper.SetConfigName(configFileName) // name of config file (without extension)
|
|
|
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
|
|
|
viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
|
|
|
|
viper.AddConfigPath("/usr/local/etc/seaweedfs/") // search path for bsd-style config directory in
|
|
|
|
viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
|
2019-06-05 16:30:24 +08:00
|
|
|
|
|
|
|
glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
|
|
|
|
|
|
|
|
if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
|
2020-10-24 14:58:54 +08:00
|
|
|
logLevel := glog.Level(0)
|
|
|
|
if strings.Contains(err.Error(), "Not Found") {
|
|
|
|
logLevel = 1
|
|
|
|
}
|
|
|
|
glog.V(logLevel).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
|
2019-06-05 16:30:24 +08:00
|
|
|
if required {
|
|
|
|
glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
|
2020-01-28 16:49:47 +08:00
|
|
|
"\n\nPlease use this command to generate the default %s.toml file\n"+
|
2019-06-05 16:30:24 +08:00
|
|
|
" weed scaffold -config=%s -output=.\n\n\n",
|
|
|
|
configFileName, configFileName, configFileName)
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2019-11-11 09:15:17 +08:00
|
|
|
}
|
2020-01-30 01:09:55 +08:00
|
|
|
|
|
|
|
func GetViper() *viper.Viper {
|
2020-04-14 10:22:40 +08:00
|
|
|
v := &viper.Viper{}
|
|
|
|
*v = *viper.GetViper()
|
2020-01-30 01:09:55 +08:00
|
|
|
v.AutomaticEnv()
|
|
|
|
v.SetEnvPrefix("weed")
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
return v
|
|
|
|
}
|