Documentation
¶
Overview ¶
Package vcfg provides a flexible configuration management system. This file implements the Builder pattern for constructing ConfigManager instances with various configuration sources and options.
Package vcfg provides configuration management with comprehensive error handling. This file defines error types and structures for detailed error reporting across different configuration operations.
Package vcfg provides configuration management capabilities. This file implements the core ConfigManager that handles configuration loading, validation, watching, and plugin management with thread-safe operations.
Package vcfg provides a comprehensive configuration management system with support for multiple configuration sources, automatic type conversion, validation, and plugins. It offers both simple and advanced configuration loading patterns for Go applications.
Index ¶
- type Builder
- func (b *Builder[T]) AddCliFlags(cmd *cli.Command, delim string) *Builder[T]
- func (b *Builder[T]) AddEnv(prefix string) *Builder[T]
- func (b *Builder[T]) AddFile(path string) *Builder[T]
- func (b *Builder[T]) AddProvider(provider koanf.Provider) *Builder[T]
- func (b *Builder[T]) Build(ctx context.Context) (*ConfigManager[T], error)
- func (b *Builder[T]) MustBuild() *ConfigManager[T]
- func (b *Builder[T]) WithPlugin() *Builder[T]
- func (b *Builder[T]) WithWatch() *Builder[T]
- type ConfigError
- type ConfigManager
- func (cm *ConfigManager[T]) Close() error
- func (cm *ConfigManager[T]) CloseWithContext(ctx context.Context) error
- func (cm *ConfigManager[T]) DisableWatch()
- func (cm *ConfigManager[T]) EnablePlugins() error
- func (cm *ConfigManager[T]) EnableWatch() *ConfigManager[T]
- func (cm *ConfigManager[T]) Get() *T
- func (cm *ConfigManager[T]) MustEnableAndStartPlugins()
- func (cm *ConfigManager[T]) StartPlugins(ctx context.Context) error
- func (cm *ConfigManager[T]) StopPlugins(ctx context.Context) error
- type ErrorType
- type Unwatcher
- type Watcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder[T any] struct { // contains filtered or unexported fields }
Builder provides a fluent interface for constructing ConfigManager instances. It allows step-by-step configuration of various sources, plugins, and options before building the final ConfigManager.
func NewBuilder ¶
NewBuilder creates a new Builder instance for configuration type T. The builder is initialized with empty sources and plugins, ready for configuration.
func (*Builder[T]) AddCliFlags ¶
AddCliFlags adds CLI flags as a configuration source using the urfave/cli library. CLI flags are typically added last to ensure they override other configuration sources. The flags are processed through a wrapper that handles key name mapping and flattening.
func (*Builder[T]) AddEnv ¶
AddEnv adds environment variables as a configuration source. Environment variables with the specified prefix will be included, with the prefix stripped and keys converted using dot notation.
func (*Builder[T]) AddFile ¶
AddFile adds a file path as a configuration source. The file format will be automatically detected based on the file extension. Supported formats include JSON, YAML, TOML, and others supported by koanf.
func (*Builder[T]) AddProvider ¶
AddProvider adds a custom koanf.Provider as a configuration source. This allows integration with any provider that implements the koanf.Provider interface.
func (*Builder[T]) Build ¶
func (b *Builder[T]) Build(ctx context.Context) (*ConfigManager[T], error)
Build constructs and returns a ConfigManager instance based on the builder's configuration. It loads the initial configuration, initializes plugins if enabled, and sets up file watching if enabled.
Parameters:
- ctx: Context for plugin initialization and other operations
Returns a fully configured ConfigManager or an error if building fails.
func (*Builder[T]) MustBuild ¶
func (b *Builder[T]) MustBuild() *ConfigManager[T]
MustBuild 构建配置管理器,失败时panic
func (*Builder[T]) WithPlugin ¶
WithPlugin enables plugin discovery and initialization. When enabled, the ConfigManager will automatically discover plugin configurations in the loaded config and initialize the corresponding plugin instances.
type ConfigError ¶
type ConfigError struct {
// Type categorizes the kind of error that occurred
Type ErrorType
// Source identifies where the error originated (file path, provider name, etc.)
Source string
// Message provides a human-readable description of the error
Message string
// Cause holds the underlying error that triggered this configuration error
Cause error
}
ConfigError represents a structured configuration error with detailed context. It provides information about the error type, source, descriptive message, and the underlying cause for comprehensive error reporting.
func NewConfigError ¶
func NewConfigError(errType ErrorType, source, message string, cause error) *ConfigError
NewConfigError 创建新的配置错误
func NewParseError ¶
func NewParseError(source, message string, cause error) *ConfigError
NewParseError 创建解析错误
func NewValidationError ¶
func NewValidationError(source, message string, cause error) *ConfigError
NewValidationError 创建验证错误
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
Error implements the error interface by returning a formatted error message. The message includes the error type, source, and descriptive text for comprehensive error reporting.
type ConfigManager ¶
type ConfigManager[T any] struct { // contains filtered or unexported fields }
ConfigManager is the core configuration manager that handles loading, validation, watching, and plugin management. It supports generic configuration types through the type parameter T and provides thread-safe access to configuration values.
The manager coordinates multiple configuration sources, automatically detects file formats, validates configurations, and manages plugin lifecycles.
func MustBuild ¶
func MustBuild[T any](filePaths ...string) *ConfigManager[T]
MustBuild is a convenience function that creates a simple configuration manager using only file sources. It's a shorthand for common use cases where only file-based configuration is needed.
Type parameter:
- T: The configuration struct type to unmarshal into
Parameters:
- filePaths: Variable number of file paths to load configuration from
Returns a ConfigManager configured with the specified files. Panics if building fails - use Builder for error handling.
func MustLoad ¶
func MustLoad[T any](sources ...any) *ConfigManager[T]
MustLoad is a convenience function that initializes a new ConfigManager with the provided sources and loads the initial configuration. It accepts both file paths (strings) and koanf.Provider instances.
Type parameter:
- T: The configuration struct type to unmarshal into
Parameters:
- sources: Variable number of configuration sources (file paths or koanf.Provider instances)
Returns a fully initialized ConfigManager with the configuration loaded. Panics if initialization or loading fails - use Builder for error handling.
func (*ConfigManager[T]) Close ¶
func (cm *ConfigManager[T]) Close() error
Close closes the configuration manager with default context, including all plugins and watchers
func (*ConfigManager[T]) CloseWithContext ¶
func (cm *ConfigManager[T]) CloseWithContext(ctx context.Context) error
CloseWithContext closes the configuration manager with context, including all plugins and watchers
func (*ConfigManager[T]) DisableWatch ¶
func (cm *ConfigManager[T]) DisableWatch()
DisableWatch stops monitoring changes of all configuration providers.
func (*ConfigManager[T]) EnablePlugins ¶
func (cm *ConfigManager[T]) EnablePlugins() error
EnablePlugins automatically discovers and registers plugin instances based on current configuration This method uses the global plugin type registry to automatically instantiate and register plugins for any configuration field that matches a registered plugin type
func (*ConfigManager[T]) EnableWatch ¶
func (cm *ConfigManager[T]) EnableWatch() *ConfigManager[T]
EnableWatch enables watching for configuration changes. It sets up file watchers for providers that implement the Watcher interface. When a configuration change is detected, it reloads the configuration and triggers plugin reloads for affected plugins. This method is thread-safe and can be called multiple times safely.
func (*ConfigManager[T]) Get ¶
func (cm *ConfigManager[T]) Get() *T
Get returns the current configuration value. It retrieves the stored configuration from atomic.Value and returns it as type T. Returns nil if configuration is not initialized.
func (*ConfigManager[T]) MustEnableAndStartPlugins ¶
func (cm *ConfigManager[T]) MustEnableAndStartPlugins()
MustEnableAndStartPlugins enables and starts all plugins, panics on error This is a convenience method that combines EnablePlugins and StartPlugins
func (*ConfigManager[T]) StartPlugins ¶
func (cm *ConfigManager[T]) StartPlugins(ctx context.Context) error
StartPlugins starts all registered plugins This method should be called after EnablePlugins to start the plugin instances
func (*ConfigManager[T]) StopPlugins ¶
func (cm *ConfigManager[T]) StopPlugins(ctx context.Context) error
StopPlugins stops all running plugins This method gracefully stops all plugin instances
type ErrorType ¶
type ErrorType int
ErrorType represents the category of configuration errors. It provides a way to classify different types of failures that can occur during configuration loading, parsing, validation, and management.
const ( // ErrorTypeUnknown represents an unclassified error ErrorTypeUnknown ErrorType = iota // ErrorTypeFileNotFound indicates a configuration file could not be found ErrorTypeFileNotFound // ErrorTypeParseFailure indicates failure to parse configuration data ErrorTypeParseFailure // ErrorTypeValidationFailure indicates configuration validation failed ErrorTypeValidationFailure // ErrorTypeWatchFailure indicates failure in file watching functionality ErrorTypeWatchFailure // ErrorTypePluginFailure indicates failure in plugin operations ErrorTypePluginFailure // ErrorTypeMergeFailure indicates failure to merge configuration sources ErrorTypeMergeFailure )
type Unwatcher ¶
type Unwatcher interface {
// Unwatch stops watching for configuration changes
Unwatch()
}
Unwatcher interface defines the contract for providers that support stopping the watch operation and cleaning up resources.
type Watcher ¶
type Watcher interface {
// Watch starts watching for configuration changes and calls the callback
// when changes occur or errors happen
Watch(cb func(event any, err error)) error
}
Watcher interface defines the contract for providers that support watching configuration changes and notifying about updates.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package defaults provides functionality for setting default values on struct fields using struct tags.
|
Package defaults provides functionality for setting default values on struct fields using struct tags. |
|
Package plugins provides the core plugin system for vcfg configuration management.
|
Package plugins provides the core plugin system for vcfg configuration management. |
|
builtins
Package builtins provides built-in plugin implementations for the vcfg configuration system.
|
Package builtins provides built-in plugin implementations for the vcfg configuration system. |
|
Package providers contains custom provider implementations and wrappers for the koanf configuration library.
|
Package providers contains custom provider implementations and wrappers for the koanf configuration library. |
|
Package validator provides configuration validation functionality using the go-playground/validator library.
|
Package validator provides configuration validation functionality using the go-playground/validator library. |