gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/config/config.go (about) 1 // Package config is an interface for dynamic configuration. 2 package config 3 4 import ( 5 "context" 6 7 "gitee.com/liuxuezhan/go-micro-v1.18.0/config/loader" 8 "gitee.com/liuxuezhan/go-micro-v1.18.0/config/reader" 9 "gitee.com/liuxuezhan/go-micro-v1.18.0/config/source" 10 "gitee.com/liuxuezhan/go-micro-v1.18.0/config/source/file" 11 ) 12 13 // Config is an interface abstraction for dynamic configuration 14 type Config interface { 15 // provide the reader.Values interface 16 reader.Values 17 // Stop the config loader/watcher 18 Close() error 19 // Load config sources 20 Load(source ...source.Source) error 21 // Force a source changeset sync 22 Sync() error 23 // Watch a value for changes 24 Watch(path ...string) (Watcher, error) 25 } 26 27 // Watcher is the config watcher 28 type Watcher interface { 29 Next() (reader.Value, error) 30 Stop() error 31 } 32 33 type Options struct { 34 Loader loader.Loader 35 Reader reader.Reader 36 Source []source.Source 37 38 // for alternative data 39 Context context.Context 40 } 41 42 type Option func(o *Options) 43 44 var ( 45 // Default Config Manager 46 DefaultConfig = NewConfig() 47 ) 48 49 // NewConfig returns new config 50 func NewConfig(opts ...Option) Config { 51 return newConfig(opts...) 52 } 53 54 // Return config as raw json 55 func Bytes() []byte { 56 return DefaultConfig.Bytes() 57 } 58 59 // Return config as a map 60 func Map() map[string]interface{} { 61 return DefaultConfig.Map() 62 } 63 64 // Scan values to a go type 65 func Scan(v interface{}) error { 66 return DefaultConfig.Scan(v) 67 } 68 69 // Force a source changeset sync 70 func Sync() error { 71 return DefaultConfig.Sync() 72 } 73 74 // Get a value from the config 75 func Get(path ...string) reader.Value { 76 return DefaultConfig.Get(path...) 77 } 78 79 // Load config sources 80 func Load(source ...source.Source) error { 81 return DefaultConfig.Load(source...) 82 } 83 84 // Watch a value for changes 85 func Watch(path ...string) (Watcher, error) { 86 return DefaultConfig.Watch(path...) 87 } 88 89 // LoadFile is short hand for creating a file source and loading it 90 func LoadFile(path string) error { 91 return Load(file.NewSource( 92 file.WithPath(path), 93 )) 94 }