// Package config is an interface for dynamic configuration. package config import ( "context" "IotAdmin/core/config/loader" "IotAdmin/core/config/reader" "IotAdmin/core/config/source" "IotAdmin/core/config/source/file" ) // Config 是动态配置的抽象接口 type Config interface { // Values provide the reader.Values interface reader.Values // Init the config Init(opts ...Option) error // Options in the config Options() Options // Close Stop the config loader/watcher Close() error // Load config sources Load(source ...source.Source) error // Sync Force a source changeset sync Sync() error // Watch a value for changes Watch(path ...string) (Watcher, error) } // Watcher 配置观察程序 type Watcher interface { Next() (reader.Value, error) Stop() error } // Entity 配置实体 type Entity interface { OnChange() } // Options 配置的参数 type Options struct { Loader loader.Loader Reader reader.Reader Source []source.Source // for alternative data Context context.Context Entity Entity } // Option 调用类型 type Option func(o *Options) var ( // DefaultConfig 默认配置管理器 DefaultConfig Config ) // NewConfig 返回新配置 func NewConfig(opts ...Option) (Config, error) { return newConfig(opts...) } // Bytes 以原始json形式返回config func Bytes() []byte { return DefaultConfig.Bytes() } // Map 将config作为map返回 func Map() map[string]interface{} { return DefaultConfig.Map() } // Scan 扫描赋值 func Scan(v interface{}) error { return DefaultConfig.Scan(v) } // Sync 强制同步变更集 func Sync() error { return DefaultConfig.Sync() } // Get 获取配置 func Get(path ...string) reader.Value { return DefaultConfig.Get(path...) } // Load 加载配置源 func Load(source ...source.Source) error { return DefaultConfig.Load(source...) } // Watch 监听文件 func Watch(path ...string) (Watcher, error) { return DefaultConfig.Watch(path...) } // LoadFile 加载配置文件 func LoadFile(path string) error { return Load(file.NewSource( file.WithPath(path), )) }