1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-01-05 10:20:53 +02:00
go-micro/config/config.go

102 lines
2.0 KiB
Go
Raw Normal View History

2019-05-31 00:11:13 +02:00
// Package config is an interface for dynamic configuration.
package config
import (
"context"
2024-06-04 22:40:43 +02:00
"go-micro.dev/v5/config/loader"
"go-micro.dev/v5/config/reader"
"go-micro.dev/v5/config/source"
"go-micro.dev/v5/config/source/file"
2019-05-31 00:11:13 +02:00
)
2022-09-30 16:27:07 +02:00
// Config is an interface abstraction for dynamic configuration.
2019-05-31 00:11:13 +02:00
type Config interface {
// provide the reader.Values interface
reader.Values
// Init the config
Init(opts ...Option) error
2020-03-31 18:13:21 +02:00
// Options in the config
Options() Options
2019-05-31 00:11:13 +02: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)
}
2022-09-30 16:27:07 +02:00
// Watcher is the config watcher.
2019-05-31 00:11:13 +02:00
type Watcher interface {
Next() (reader.Value, error)
Stop() error
}
type Options struct {
Loader loader.Loader
Reader reader.Reader
// for alternative data
Context context.Context
2023-04-26 02:16:34 +02:00
Source []source.Source
WithWatcherDisabled bool
2019-05-31 00:11:13 +02:00
}
type Option func(o *Options)
var (
2022-09-30 16:27:07 +02:00
// Default Config Manager.
2020-01-19 10:31:02 +02:00
DefaultConfig, _ = NewConfig()
2019-05-31 00:11:13 +02:00
)
2022-09-30 16:27:07 +02:00
// NewConfig returns new config.
2020-01-19 10:31:02 +02:00
func NewConfig(opts ...Option) (Config, error) {
2019-05-31 00:11:13 +02:00
return newConfig(opts...)
}
2022-09-30 16:27:07 +02:00
// Return config as raw json.
2019-05-31 00:11:13 +02:00
func Bytes() []byte {
return DefaultConfig.Bytes()
}
2022-09-30 16:27:07 +02:00
// Return config as a map.
2019-05-31 00:11:13 +02:00
func Map() map[string]interface{} {
return DefaultConfig.Map()
}
2022-09-30 16:27:07 +02:00
// Scan values to a go type.
2019-05-31 00:11:13 +02:00
func Scan(v interface{}) error {
return DefaultConfig.Scan(v)
}
2022-09-30 16:27:07 +02:00
// Force a source changeset sync.
2019-05-31 00:11:13 +02:00
func Sync() error {
return DefaultConfig.Sync()
}
2022-09-30 16:27:07 +02:00
// Get a value from the config.
2019-05-31 00:11:13 +02:00
func Get(path ...string) reader.Value {
return DefaultConfig.Get(path...)
}
2022-09-30 16:27:07 +02:00
// Load config sources.
2019-05-31 00:11:13 +02:00
func Load(source ...source.Source) error {
return DefaultConfig.Load(source...)
}
2022-09-30 16:27:07 +02:00
// Watch a value for changes.
2019-05-31 00:11:13 +02:00
func Watch(path ...string) (Watcher, error) {
return DefaultConfig.Watch(path...)
}
2022-09-30 16:27:07 +02:00
// LoadFile is short hand for creating a file source and loading it.
2019-05-31 00:11:13 +02:00
func LoadFile(path string) error {
return Load(file.NewSource(
file.WithPath(path),
))
}