Documentation
¶
Index ¶
- func Close()
- func Get(pattern string, def ...any) value.Value
- func Has(pattern string) bool
- func Set(pattern string, value any) error
- func SetConfigurator(configurator Configurator)
- func SetConfiguratorWithSources(sources ...Source)
- func Store(ctx context.Context, source string, file string, content any, override ...bool) error
- func Watch(cb WatchCallbackFunc, names ...string)
- type Configuration
- type Configurator
- type Decoder
- type Encoder
- type Matcher
- type Mode
- type Option
- type Scanner
- type Source
- type WatchCallbackFunc
- type Watcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get returns the config value under pattern, falling back to def when the config is missing.
func SetConfigurator ¶
func SetConfigurator(configurator Configurator)
SetConfigurator sets the global configurator, closing the previous one when it is not nil.
func SetConfiguratorWithSources ¶ added in v2.1.0
func SetConfiguratorWithSources(sources ...Source)
SetConfiguratorWithSources creates a configurator from sources and installs it as the global configurator.
func Store ¶
Store saves content as file under the named source, merging it with the existing config unless override is true.
func Watch ¶
func Watch(cb WatchCallbackFunc, names ...string)
Watch registers cb to be called when one of the named configs changes; empty names means every config.
Types ¶
type Configuration ¶ added in v2.1.0
type Configuration struct {
Path string // Path of the config file relative to the source root
File string // File name including its extension
Name string // File name without its extension
Format string // File format
Content []byte // File content
FullPath string // Full path of the config file
// contains filtered or unexported fields
}
Configuration is a config item loaded from a config source.
func Load ¶
Load loads the configurations of the named source; empty file means every file of the source.
func (*Configuration) Decode ¶ added in v2.1.0
func (c *Configuration) Decode() (any, error)
Decode decodes the config content, reporting errors.ErrInvalidDecoder when no decoder is attached.
func (*Configuration) Scan ¶ added in v2.1.0
func (c *Configuration) Scan(dest any) error
Scan scans the config content into dest, reporting errors.ErrInvalidScanner when no scanner is attached.
type Configurator ¶ added in v2.1.0
type Configurator interface {
// Has reports whether a config matching pattern exists.
Has(pattern string) bool
// Get returns the config value under pattern, falling back to def when the config is missing.
Get(pattern string, def ...any) value.Value
// Set sets the config value under pattern.
Set(pattern string, value any) error
// Match returns a [Matcher] over the given patterns.
Match(patterns ...string) Matcher
// Watch registers cb to be called when one of the named configs changes; empty names means every
// config.
Watch(cb WatchCallbackFunc, names ...string)
// Load loads the configurations of the named source; empty file means every file of the source.
Load(ctx context.Context, source string, file ...string) ([]*Configuration, error)
// Store saves content as file under the named source, merging it with the existing config unless
// override is true.
Store(ctx context.Context, source string, file string, content any, override ...bool) error
// Close closes the configurator and its config sources.
Close()
}
Configurator manages a set of config sources and exposes the config loaded from them.
func GetConfigurator ¶
func GetConfigurator() Configurator
GetConfigurator returns the global configurator.
func NewConfigurator ¶
func NewConfigurator(opts ...Option) Configurator
NewConfigurator returns a new Configurator configured with opts and starts watching the config sources for changes.
type Matcher ¶ added in v2.1.0
type Matcher interface {
// Has reports whether a config exists for any of the patterns.
Has() bool
// Get returns the value of the first pattern that resolves, falling back to def when none does.
Get(def ...any) value.Value
// Scan scans the value of the first pattern that resolves into dest.
Scan(dest any) error
}
Matcher matches config values against an ordered list of patterns.
type Option ¶ added in v2.1.0
type Option func(o *options)
Option configures a configurator.
func WithContext ¶ added in v2.1.0
WithContext sets the base context of the configurator.
func WithDecoder ¶ added in v2.1.0
WithDecoder sets the decoder used when loading config content.
func WithEncoder ¶ added in v2.1.0
WithEncoder sets the encoder used when storing config content.
func WithSources ¶ added in v2.1.0
WithSources sets the config sources.
type Source ¶ added in v2.1.0
type Source interface {
// Name returns the name of the source.
Name() string
// Load loads the configurations of the given files, or every file of the source when file is
// empty.
Load(ctx context.Context, file ...string) ([]*Configuration, error)
// Store saves content as file.
Store(ctx context.Context, file string, content []byte) error
// Watch starts watching the source and returns a [Watcher].
Watch(ctx context.Context) (Watcher, error)
// Close closes the source.
Close() error
}
Source loads, stores and watches configurations.
type WatchCallbackFunc ¶ added in v2.1.0
type WatchCallbackFunc func(names ...string)
WatchCallbackFunc is called with the names of the configs that changed.