This commit is contained in:
2025-03-28 17:51:34 +08:00
parent da612380e0
commit 5c8802d2f0
68 changed files with 3422 additions and 630 deletions

View File

@@ -25,6 +25,40 @@ type Config struct {
Smb Smb `mapstructure:"smb" json:"smb" yaml:"smb"`
}
func New(path string) (*Config, error) {
fp := "."
fn := ConfigDefaultFile
if len(path) > 0 {
fp, fn = filepath.Split(path)
if len(fp) == 0 {
fp = "."
}
}
v := viper.New()
v.AddConfigPath(fp)
v.SetConfigName(fn)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
return nil, err
}
v.WatchConfig()
var conf *Config
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err := v.Unmarshal(&conf); err != nil {
fmt.Println(err)
}
})
if err := v.Unmarshal(&conf); err != nil {
return nil, err
}
return conf, nil
}
func Init(path string) error {
fp := "."
fn := ConfigDefaultFile