config

package
v0.0.4-alpha.21 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: EUPL-1.2 Imports: 9 Imported by: 0

Documentation

Overview

Package config provides layered configuration management for the Core framework.

Configuration values are resolved in priority order: defaults -> file -> env -> flags. Values are stored in a YAML file at ~/.core/config.yaml by default.

Keys use dot notation for nested access:

cfg.Set("dev.editor", "vim")
var editor string
cfg.Get("dev.editor", &editor)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(m io.Medium, path string) (map[string]any, error)

Load reads a YAML configuration file from the given medium and path. Returns the parsed data as a map, or an error if the file cannot be read or parsed.

func LoadEnv

func LoadEnv(prefix string) map[string]any

LoadEnv parses environment variables with the given prefix and returns them as a flat map with dot-notation keys.

For example, with prefix "CORE_CONFIG_":

CORE_CONFIG_FOO_BAR=baz  ->  {"foo.bar": "baz"}
CORE_CONFIG_EDITOR=vim   ->  {"editor": "vim"}

func NewConfigService

func NewConfigService(c *core.Core) (any, error)

NewConfigService creates a new config service factory for the Core framework. Register it with core.WithService(config.NewConfigService).

func Save

func Save(m io.Medium, path string, data map[string]any) error

Save writes configuration data to a YAML file at the given path. It ensures the parent directory exists before writing.

Types

type Config

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

Config implements the core.Config interface with layered resolution. Values are resolved in order: defaults -> file -> env -> flags.

func New

func New(opts ...Option) (*Config, error)

New creates a new Config instance with the given options. If no medium is provided, it defaults to io.Local. If no path is provided, it defaults to ~/.core/config.yaml.

func (*Config) All

func (c *Config) All() map[string]any

All returns a deep copy of all configuration values.

func (*Config) Get

func (c *Config) Get(key string, out any) error

Get retrieves a configuration value by dot-notation key and stores it in out. The out parameter must be a pointer to the target type. Returns an error if the key is not found.

func (*Config) Path

func (c *Config) Path() string

Path returns the path to the configuration file.

func (*Config) Set

func (c *Config) Set(key string, v any) error

Set stores a configuration value by dot-notation key and persists to disk.

type Option

type Option func(*Config)

Option is a functional option for configuring a Config instance.

func WithMedium

func WithMedium(m io.Medium) Option

WithMedium sets the storage medium for configuration file operations.

func WithPath

func WithPath(path string) Option

WithPath sets the path to the configuration file.

type Service

type Service struct {
	*core.ServiceRuntime[ServiceOptions]
	// contains filtered or unexported fields
}

Service wraps Config as a framework service with lifecycle support.

func (*Service) Get

func (s *Service) Get(key string, out any) error

Get retrieves a configuration value by key.

func (*Service) OnStartup

func (s *Service) OnStartup(_ context.Context) error

OnStartup loads the configuration file during application startup.

func (*Service) Set

func (s *Service) Set(key string, v any) error

Set stores a configuration value by key.

type ServiceOptions

type ServiceOptions struct {
	// Path overrides the default config file path.
	Path string
	// Medium overrides the default storage medium.
	Medium io.Medium
}

ServiceOptions holds configuration for the config service.

Jump to

Keyboard shortcuts

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