github.com/micro/go-micro/v2@v2.9.1/config/config.go (about)

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