config

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package config provides a unified configuration loader for OpenChoreo components.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader

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

Loader handles configuration loading from multiple sources.

func NewLoader

func NewLoader(envPrefix string, opts ...Option) *Loader

NewLoader creates a new configuration loader. envPrefix should be like "OC_API" (without trailing delimiter). Environment variables use double underscore (__) for nesting: OC_API__SERVER__PORT -> server.port

func (*Loader) LoadFlags

func (l *Loader) LoadFlags(flags *pflag.FlagSet, mappings map[string]string) error

LoadFlags applies CLI flag overrides using explicit mappings. Only flags that were explicitly set by the user are applied. Call this after LoadWithDefaults for highest priority overrides.

func (*Loader) LoadWithDefaults

func (l *Loader) LoadWithDefaults(defaults any, configPath string) error

LoadWithDefaults loads configuration with the following priority (highest to lowest):

  1. Environment variables (OC_API__SERVER__PORT -> server.port)
  2. Config file (YAML)
  3. Struct defaults

If configPath is specified but the file does not exist, an error is returned. If configPath is empty, only defaults and environment variables are used.

func (*Loader) Raw

func (l *Loader) Raw() map[string]any

Raw returns all loaded configuration as a nested map. Useful for config dumping (e.g., --dump-config).

func (*Loader) Set

func (l *Loader) Set(key string, value any) error

Set manually sets a configuration value.

func (*Loader) Unmarshal

func (l *Loader) Unmarshal(path string, out any) error

Unmarshal unmarshals the loaded configuration into the provided struct.

func (*Loader) UnmarshalAndValidate

func (l *Loader) UnmarshalAndValidate(path string, out any) error

UnmarshalAndValidate unmarshals the configuration and validates it. If out implements Validator, Validate() is called after unmarshaling.

type Option

type Option func(*Loader)

Option configures a Loader.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets a custom logger for debug output.

type Validator

type Validator interface {
	Validate() error
}

Validator can be implemented by config structs to enable validation.

Jump to

Keyboard shortcuts

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