micro/config/config.go

97 lines
2.0 KiB
Go
Raw Normal View History

2019-05-31 01:11:13 +03:00
// Package config is an interface for dynamic configuration.
package config
import (
"context"
"github.com/micro/go-micro/v2/config/loader"
"github.com/micro/go-micro/v2/config/reader"
"github.com/micro/go-micro/v2/config/source"
"github.com/micro/go-micro/v2/config/source/file"
2019-05-31 01:11:13 +03:00
)
// Config is an interface abstraction for dynamic configuration
type Config interface {
// provide the reader.Values interface
reader.Values
// Init the config
Init(opts ...Option) error
2019-05-31 01:11:13 +03:00
// Stop the config loader/watcher
Close() error
// Load config sources
Load(source ...source.Source) error
// Force a source changeset sync
Sync() error
// Watch a value for changes
Watch(path ...string) (Watcher, error)
}
// Watcher is the config watcher
type Watcher interface {
Next() (reader.Value, error)
Stop() error
}
type Options struct {
Loader loader.Loader
Reader reader.Reader
Source []source.Source
// for alternative data
Context context.Context
}
type Option func(o *Options)
var (
// Default Config Manager
2020-01-19 11:31:02 +03:00
DefaultConfig, _ = NewConfig()
2019-05-31 01:11:13 +03:00
)
// NewConfig returns new config
2020-01-19 11:31:02 +03:00
func NewConfig(opts ...Option) (Config, error) {
2019-05-31 01:11:13 +03:00
return newConfig(opts...)
}
// Return config as raw json
func Bytes() []byte {
return DefaultConfig.Bytes()
}
// Return config as a map
func Map() map[string]interface{} {
return DefaultConfig.Map()
}
// Scan values to a go type
func Scan(v interface{}) error {
return DefaultConfig.Scan(v)
}
// Force a source changeset sync
func Sync() error {
return DefaultConfig.Sync()
}
// Get a value from the config
func Get(path ...string) reader.Value {
return DefaultConfig.Get(path...)
}
// Load config sources
func Load(source ...source.Source) error {
return DefaultConfig.Load(source...)
}
// Watch a value for changes
func Watch(path ...string) (Watcher, error) {
return DefaultConfig.Watch(path...)
}
// LoadFile is short hand for creating a file source and loading it
func LoadFile(path string) error {
return Load(file.NewSource(
file.WithPath(path),
))
}