43 lines
987 B
Go
43 lines
987 B
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func Load(cfgFile string) (*Config, error) {
|
|
if cfgFile != "" {
|
|
viper.SetConfigFile(cfgFile)
|
|
} else {
|
|
viper.SetConfigName("clonepack")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath(".")
|
|
viper.AddConfigPath("$HOME/.clonepack")
|
|
viper.AddConfigPath("/etc/clonepack")
|
|
}
|
|
|
|
viper.SetEnvPrefix("CLONEPACK")
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
viper.AutomaticEnv()
|
|
|
|
viper.SetDefault("server.host", "0.0.0.0")
|
|
viper.SetDefault("server.port", 8080)
|
|
viper.SetDefault("db.path", "./clonepack.db")
|
|
viper.SetDefault("data_dir", "./data")
|
|
viper.SetDefault("sync.interval", "1h")
|
|
viper.SetDefault("log.level", "info")
|
|
viper.SetDefault("log.format", "text")
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
var cfg Config
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
return &cfg, nil
|
|
}
|