config

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package config provides a streamlined, extensible configuration management system. It supports multiple configuration sources with priority-based merging and validation.

The default configuration sources include environment variables and configuration files in the following formats:

  • TOML
  • JSON
  • YAML

Configuration is loaded from multiple providers, merged based on priority, and validated before use.

The package offers sensible defaults but allows extensive customization through interfaces for providers, validators, and mergers.

Custom configuration providers, validators, and mergers can be easily integrated. The package ensures thread-safe operations and is designed for high performance in concurrent environments.

Note: initialization of the package-global loader is performed in `init()` within `config.go`. For explicit/custom loading use a `NewLoader()` instance.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.0.0

type Config struct {
	// Log holds the logging configuration.
	Log LogConfig `toml:"log" yaml:"log" json:"log"`

	// Find holds the 'find' command configuration.
	Find FindConfig `toml:"find" yaml:"find" json:"find"`

	// Preset holds the 'preset' command configuration.
	Preset PresetConfig `toml:"preset" yaml:"preset" json:"preset"`
}

Config represents the application configuration structure.

func DefaultConfig added in v1.0.0

func DefaultConfig() *Config

DefaultConfig returns the default configuration.

func Load added in v1.0.0

func Load() (*Config, error)

Load returns the current global configuration.

type EnvProvider added in v1.0.0

type EnvProvider struct {
	// contains filtered or unexported fields
}

EnvProvider provides configuration from environment variables.

func NewEnvProvider added in v1.0.0

func NewEnvProvider(prefix string, priority int) *EnvProvider

NewEnvProvider creates a new environment provider.

func (*EnvProvider) Load added in v1.0.0

func (p *EnvProvider) Load(ctx context.Context) (*Config, error)

Load loads configuration from the environment.

func (*EnvProvider) Name added in v1.0.0

func (p *EnvProvider) Name() string

Name returns the provider name.

func (*EnvProvider) Priority added in v1.0.0

func (p *EnvProvider) Priority() int

Priority returns the provider priority.

type FileProvider added in v1.0.0

type FileProvider struct {
	// contains filtered or unexported fields
}

FileProvider provides configuration from files.

func NewFileProvider added in v1.0.0

func NewFileProvider(path string, priority int) *FileProvider

NewFileProvider creates a new file provider. The path is expected to be sanitized. The file format is inferred from the file extension, or assumed to be TOML if not available.

func (*FileProvider) Load added in v1.0.0

func (p *FileProvider) Load(ctx context.Context) (*Config, error)

Load loads configuration from the file.

func (*FileProvider) Name added in v1.0.0

func (p *FileProvider) Name() string

Name returns the provider name.

func (*FileProvider) Priority added in v1.0.0

func (p *FileProvider) Priority() int

Priority returns the provider priority.

type FindConfig added in v1.0.0

type FindConfig struct {
	// Workers sets the number of concurrent workers for file processing.
	// Default is the number of CPU cores.
	Workers int `toml:"workers" yaml:"workers" json:"workers"`

	// Verbose enables verbose output.
	Verbose bool `toml:"verbose" yaml:"verbose" json:"verbose"`

	// ExcludeDirs holds the glob patterns to exclude directories from searching.
	// This is a comma-separated list of patterns, which should be escaped as needed.
	ExcludeDirs string `toml:"exclude_dirs" yaml:"exclude_dirs" json:"exclude_dirs"`

	// ExcludeFiles holds the glob patterns to exclude files from searching.
	// This is a comma-separated list of patterns, which should be escaped as needed.
	ExcludeFiles string `toml:"exclude_files" yaml:"exclude_files" json:"exclude_files"`

	// ExcludeDirRegex holds regex patterns to exclude directories.
	// This is a comma-separated list of patterns, which should be escaped as needed.
	ExcludeDirRegex string `toml:"exclude_dir_regex" yaml:"exclude_dir_regex" json:"exclude_dir_regex"`

	// ExcludeFileRegex holds regex patterns to exclude files.
	// This is a comma-separated list of patterns, which should be escaped as needed.
	ExcludeFileRegex string `toml:"exclude_file_regex" yaml:"exclude_file_regex" json:"exclude_file_regex"`

	// MinSize sets the minimum file size to consider (e.g., "10KB", "5MB").
	MinSize string `toml:"min_size" yaml:"min_size" json:"min_size"`

	// MaxSize sets the maximum file size to consider (e.g., "100MB", "1GB").
	MaxSize string `toml:"max_size" yaml:"max_size" json:"max_size"`

	// ShowFilters enables displaying the active filters.
	ShowFilters bool `toml:"show_filters" yaml:"show_filters" json:"show_filters"`

	// OutputFormat sets the output format (e.g., "pretty", "json", "yaml").
	OutputFormat string `toml:"output_format" yaml:"output_format" json:"output_format"`

	// OutputFile sets the file to write output to (default is stdout).
	OutputFile string `toml:"output_file" yaml:"output_file" json:"output_file"`
}

FindConfig holds configuration for the 'find' command.

type Loader added in v1.0.0

type Loader struct {
	// contains filtered or unexported fields
}

Loader manages configuration loading from multiple providers.

func NewLoader added in v1.0.0

func NewLoader(opts ...LoaderOption) *Loader

NewLoader creates a new configuration loader.

func (*Loader) AddProvider added in v1.0.0

func (l *Loader) AddProvider(provider Provider)

AddProvider adds a configuration provider.

func (*Loader) Load added in v1.0.0

func (l *Loader) Load(ctx context.Context) (*Config, error)

Load loads configuration from all providers.

type LoaderOption added in v1.0.0

type LoaderOption func(*LoaderOptions)

LoaderOption is a functional option for configuring the loader.

func WithMerger added in v1.0.0

func WithMerger(m Merger) LoaderOption

WithMerger sets a custom merger.

func WithTimeout added in v1.0.0

func WithTimeout(timeout time.Duration) LoaderOption

WithTimeout sets the operation timeout.

func WithValidator added in v1.0.0

func WithValidator(v Validator) LoaderOption

WithValidator sets a custom validator.

type LoaderOptions added in v1.0.0

type LoaderOptions struct {
	// Validator for configuration validation.
	Validator Validator
	// Merger for custom merging logic.
	Merger Merger
	// Timeout for provider operations.
	Timeout time.Duration
}

LoaderOptions configure the configuration loader.

type LogConfig added in v1.0.0

type LogConfig struct {
	// Level sets the logging level (e.g., "debug", "info", "warn", "error").
	Level string `toml:"level" yaml:"level" json:"level"`

	// Format sets the logging format (e.g., "text", "json", "pretty", "discard").
	Format string `toml:"format" yaml:"format" json:"format"`

	// Output sets the logging output (e.g., "stdout", "stderr", "null", or file path).
	Output string `toml:"output" yaml:"output" json:"output"`
}

LogConfig holds the logging configuration.

type Merger added in v1.0.0

type Merger interface {
	Merge(base, override *Config) *Config
}

Merger defines the interface for custom configuration merging.

type PresetConfig added in v1.0.0

type PresetConfig struct {
	// Workers sets the number of concurrent workers for file processing.
	// Default is the number of CPU cores.
	Workers int `toml:"workers" yaml:"workers" json:"workers"`

	// Verbose enables verbose output.
	Verbose bool `toml:"verbose" yaml:"verbose" json:"verbose"`

	// ShowFilters enables displaying the active filters.
	ShowFilters bool `toml:"show_filters" yaml:"show_filters" json:"show_filters"`

	// OutputFormat sets the output format (e.g., "pretty", "json", "yaml").
	OutputFormat string `toml:"output_format" yaml:"output_format" json:"output_format"`

	// OutputFile sets the file to write output to (default is stdout).
	OutputFile string `toml:"output_file" yaml:"output_file" json:"output_file"`
}

PresetConfig holds configuration for the 'preset' command.

type Provider added in v1.0.0

type Provider interface {
	// Name returns the provider name for identification.
	Name() string

	// Priority returns the provider priority (higher numbers = higher priority).
	Priority() int

	// Load loads configuration from the provider.
	Load(ctx context.Context) (*Config, error)
}

Provider defines the interface for configuration providers.

type Validator added in v1.0.0

type Validator interface {
	Validate(config *Config) error
}

Validator defines the interface for configuration validation.

Jump to

Keyboard shortcuts

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