gitee.com/sasukebo/go-micro/v4@v4.7.1/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/sasukebo/go-micro/v4/config/loader"
     8  	"gitee.com/sasukebo/go-micro/v4/config/reader"
     9  	"gitee.com/sasukebo/go-micro/v4/config/source"
    10  	"gitee.com/sasukebo/go-micro/v4/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  	// Init the config
    18  	Init(opts ...Option) error
    19  	// Options in the config
    20  	Options() Options
    21  	// Stop the config loader/watcher
    22  	Close() error
    23  	// Load config sources
    24  	Load(source ...source.Source) error
    25  	// Force a source changeset sync
    26  	Sync() error
    27  	// Watch a value for changes
    28  	Watch(path ...string) (Watcher, error)
    29  }
    30  
    31  // Watcher is the config watcher
    32  type Watcher interface {
    33  	Next() (reader.Value, error)
    34  	Stop() error
    35  }
    36  
    37  type Options struct {
    38  	Loader loader.Loader
    39  	Reader reader.Reader
    40  	Source []source.Source
    41  
    42  	// for alternative data
    43  	Context context.Context
    44  
    45  	WithWatcherDisabled bool
    46  }
    47  
    48  type Option func(o *Options)
    49  
    50  var (
    51  	// Default Config Manager
    52  	DefaultConfig, _ = NewConfig()
    53  )
    54  
    55  // NewConfig returns new config
    56  func NewConfig(opts ...Option) (Config, error) {
    57  	return newConfig(opts...)
    58  }
    59  
    60  // Return config as raw json
    61  func Bytes() []byte {
    62  	return DefaultConfig.Bytes()
    63  }
    64  
    65  // Return config as a map
    66  func Map() map[string]interface{} {
    67  	return DefaultConfig.Map()
    68  }
    69  
    70  // Scan values to a go type
    71  func Scan(v interface{}) error {
    72  	return DefaultConfig.Scan(v)
    73  }
    74  
    75  // Force a source changeset sync
    76  func Sync() error {
    77  	return DefaultConfig.Sync()
    78  }
    79  
    80  // Get a value from the config
    81  func Get(path ...string) reader.Value {
    82  	return DefaultConfig.Get(path...)
    83  }
    84  
    85  // Load config sources
    86  func Load(source ...source.Source) error {
    87  	return DefaultConfig.Load(source...)
    88  }
    89  
    90  // Watch a value for changes
    91  func Watch(path ...string) (Watcher, error) {
    92  	return DefaultConfig.Watch(path...)
    93  }
    94  
    95  // LoadFile is short hand for creating a file source and loading it
    96  func LoadFile(path string) error {
    97  	return Load(file.NewSource(
    98  		file.WithPath(path),
    99  	))
   100  }