2015-03-10 07:20:31 +00:00
|
|
|
package util
|
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
import (
|
2020-01-29 17:09:55 +00:00
|
|
|
"strings"
|
2021-01-12 10:28:13 +00:00
|
|
|
"sync"
|
2020-01-29 17:09:55 +00:00
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-01-29 17:09:55 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2019-06-05 08:30:24 +00:00
|
|
|
)
|
|
|
|
|
2022-03-19 07:22:47 +00:00
|
|
|
var (
|
|
|
|
ConfigurationFileDirectory DirectoryValueType
|
|
|
|
)
|
|
|
|
|
|
|
|
type DirectoryValueType string
|
|
|
|
|
|
|
|
func (s *DirectoryValueType) Set(value string) error {
|
|
|
|
*s = DirectoryValueType(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *DirectoryValueType) String() string {
|
|
|
|
return string(*s)
|
|
|
|
}
|
|
|
|
|
2018-08-19 22:17:55 +00:00
|
|
|
type Configuration interface {
|
|
|
|
GetString(key string) string
|
|
|
|
GetBool(key string) bool
|
|
|
|
GetInt(key string) int
|
|
|
|
GetStringSlice(key string) []string
|
2019-12-21 04:56:14 +00:00
|
|
|
SetDefault(key string, value interface{})
|
2012-08-24 03:56:09 +00:00
|
|
|
}
|
2019-06-05 08:30:24 +00:00
|
|
|
|
|
|
|
func LoadConfiguration(configFileName string, required bool) (loaded bool) {
|
|
|
|
|
|
|
|
// find a filer store
|
2022-03-19 07:22:47 +00:00
|
|
|
viper.SetConfigName(configFileName) // name of config file (without extension)
|
|
|
|
viper.AddConfigPath(ResolvePath(ConfigurationFileDirectory.String())) // path to look for the config file in
|
|
|
|
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 08:30:24 +00:00
|
|
|
|
|
|
|
if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
|
2020-10-24 06:58:54 +00:00
|
|
|
if strings.Contains(err.Error(), "Not Found") {
|
2021-01-20 01:21:50 +00:00
|
|
|
glog.V(1).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
|
|
|
|
} else {
|
|
|
|
glog.Fatalf("Reading %s: %v", viper.ConfigFileUsed(), err)
|
2020-10-24 06:58:54 +00:00
|
|
|
}
|
2019-06-05 08:30:24 +00:00
|
|
|
if required {
|
|
|
|
glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
|
2020-01-28 08:49:47 +00:00
|
|
|
"\n\nPlease use this command to generate the default %s.toml file\n"+
|
2019-06-05 08:30:24 +00:00
|
|
|
" weed scaffold -config=%s -output=.\n\n\n",
|
|
|
|
configFileName, configFileName, configFileName)
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-05-26 00:19:17 +00:00
|
|
|
glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
|
2019-06-05 08:30:24 +00:00
|
|
|
|
|
|
|
return true
|
2019-11-11 01:15:17 +00:00
|
|
|
}
|
2020-01-29 17:09:55 +00:00
|
|
|
|
2021-01-12 10:28:13 +00:00
|
|
|
type ViperProxy struct {
|
|
|
|
*viper.Viper
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-01-13 21:20:33 +00:00
|
|
|
var (
|
|
|
|
vp = &ViperProxy{}
|
|
|
|
)
|
|
|
|
|
2021-01-12 10:28:13 +00:00
|
|
|
func (vp *ViperProxy) SetDefault(key string, value interface{}) {
|
|
|
|
vp.Lock()
|
|
|
|
defer vp.Unlock()
|
|
|
|
vp.Viper.SetDefault(key, value)
|
|
|
|
}
|
|
|
|
|
2021-01-13 21:14:48 +00:00
|
|
|
func (vp *ViperProxy) GetString(key string) string {
|
|
|
|
vp.Lock()
|
|
|
|
defer vp.Unlock()
|
|
|
|
return vp.Viper.GetString(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vp *ViperProxy) GetBool(key string) bool {
|
|
|
|
vp.Lock()
|
|
|
|
defer vp.Unlock()
|
|
|
|
return vp.Viper.GetBool(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vp *ViperProxy) GetInt(key string) int {
|
|
|
|
vp.Lock()
|
|
|
|
defer vp.Unlock()
|
|
|
|
return vp.Viper.GetInt(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vp *ViperProxy) GetStringSlice(key string) []string {
|
|
|
|
vp.Lock()
|
|
|
|
defer vp.Unlock()
|
|
|
|
return vp.Viper.GetStringSlice(key)
|
|
|
|
}
|
|
|
|
|
2021-01-12 10:28:13 +00:00
|
|
|
func GetViper() *ViperProxy {
|
2021-01-13 21:20:33 +00:00
|
|
|
vp.Lock()
|
|
|
|
defer vp.Unlock()
|
|
|
|
|
|
|
|
if vp.Viper == nil {
|
|
|
|
vp.Viper = viper.GetViper()
|
|
|
|
vp.AutomaticEnv()
|
|
|
|
vp.SetEnvPrefix("weed")
|
|
|
|
vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return vp
|
2020-01-29 17:09:55 +00:00
|
|
|
}
|