go-micro.dev/v5@v5.12.0/config/config.go (about)

     1  // Package config is an interface for dynamic configuration.
     2  package config
     3  
     4  import (
     5  	"context"
     6  
     7  	"go-micro.dev/v5/config/loader"
     8  	"go-micro.dev/v5/config/reader"
     9  	"go-micro.dev/v5/config/source"
    10  	"go-micro.dev/v5/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  
    41  	// for alternative data
    42  	Context context.Context
    43  
    44  	Source []source.Source
    45  
    46  	WithWatcherDisabled bool
    47  }
    48  
    49  type Option func(o *Options)
    50  
    51  var (
    52  	// Default Config Manager.
    53  	DefaultConfig, _ = NewConfig()
    54  )
    55  
    56  // NewConfig returns new config.
    57  func NewConfig(opts ...Option) (Config, error) {
    58  	return newConfig(opts...)
    59  }
    60  
    61  // Return config as raw json.
    62  func Bytes() []byte {
    63  	return DefaultConfig.Bytes()
    64  }
    65  
    66  // Return config as a map.
    67  func Map() map[string]interface{} {
    68  	return DefaultConfig.Map()
    69  }
    70  
    71  // Scan values to a go type.
    72  func Scan(v interface{}) error {
    73  	return DefaultConfig.Scan(v)
    74  }
    75  
    76  // Force a source changeset sync.
    77  func Sync() error {
    78  	return DefaultConfig.Sync()
    79  }
    80  
    81  // Get a value from the config.
    82  func Get(path ...string) (reader.Value, error) {
    83  	return DefaultConfig.Get(path...)
    84  }
    85  
    86  // Load config sources.
    87  func Load(source ...source.Source) error {
    88  	return DefaultConfig.Load(source...)
    89  }
    90  
    91  // Watch a value for changes.
    92  func Watch(path ...string) (Watcher, error) {
    93  	return DefaultConfig.Watch(path...)
    94  }
    95  
    96  // LoadFile is short hand for creating a file source and loading it.
    97  func LoadFile(path string) error {
    98  	return Load(file.NewSource(
    99  		file.WithPath(path),
   100  	))
   101  }