config

package
v2.6.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 19 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close closes the global configurator and stops watching config changes.

func Get

func Get(pattern string, def ...any) value.Value

Get returns the config value under pattern, falling back to def when the config is missing.

func Has

func Has(pattern string) bool

Has reports whether a config matching pattern exists.

func Set

func Set(pattern string, value any) error

Set sets the config value under pattern.

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

func Store(ctx context.Context, source string, file string, content any, override ...bool) error

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

func Load(ctx context.Context, source string, file ...string) ([]*Configuration, error)

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 Decoder added in v2.1.0

type Decoder func(format string, content []byte) (any, error)

Decoder decodes content in the given format.

type Encoder added in v2.1.0

type Encoder func(format string, content any) ([]byte, error)

Encoder encodes content in the given format into bytes.

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.

func Match

func Match(patterns ...string) Matcher

Match returns a Matcher over the given patterns.

type Mode added in v2.1.0

type Mode string

Mode is the read-write mode of a Source.

const (
	ReadOnly  Mode = "read-only"  // ReadOnly: the config source can only be read
	WriteOnly Mode = "write-only" // WriteOnly: the config source can only be written
	ReadWrite Mode = "read-write" // ReadWrite: the config source can be read and written
)

The read-write modes of a Source.

type Option added in v2.1.0

type Option func(o *options)

Option configures a configurator.

func WithContext added in v2.1.0

func WithContext(ctx context.Context) Option

WithContext sets the base context of the configurator.

func WithDecoder added in v2.1.0

func WithDecoder(decoder Decoder) Option

WithDecoder sets the decoder used when loading config content.

func WithEncoder added in v2.1.0

func WithEncoder(encoder Encoder) Option

WithEncoder sets the encoder used when storing config content.

func WithSources added in v2.1.0

func WithSources(sources ...Source) Option

WithSources sets the config sources.

type Scanner added in v2.1.0

type Scanner func(format string, content []byte, dest any) error

Scanner scans content in the given format into dest.

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.

type Watcher added in v2.1.0

type Watcher interface {
	// Next blocks until a config changes and returns the changed configurations.
	Next() ([]*Configuration, error)
	// Stop stops watching.
	Stop() error
}

Watcher watches the config changes of a Source.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL