1 Home
Virgil edited this page 2026-03-11 12:07:45 +00:00

go-config

Module: forge.lthn.ai/core/go-config

Layered configuration management for the Core framework. Values are resolved in priority order: defaults -> file -> environment variables -> flags. Backed by Viper with YAML persistence at ~/.core/config.yaml.

Architecture

File Purpose
config.go Config struct, New(), Get(), Set(), Commit(), LoadFile(), file helpers
env.go Environment variable handling
service.go Core DI service integration

Key Types

Config

  • Config — Implements core.Config interface. Uses dual Viper instances: one for full config (file + env), one for file-only (for safe persistence without env leakage).
  • Option — Functional options: WithMedium(), WithPath(), WithEnvPrefix()

Operations

Method Description
Get(key, out) Retrieve value by dot-notation key, unmarshal into out pointer. Empty key unmarshals entire config.
Set(key, v) Store value in memory (call Commit() to persist)
Commit() Persist changes to YAML file on disk
LoadFile(m, path) Merge a config file (YAML or .env) into current config
All() iter.Seq2[string, any] iterator over all settings
Path() Returns config file path

Package Functions

Function Description
Load(m, path) Read YAML file, return map[string]any (deprecated)
Save(m, path, data) Write map[string]any to YAML file

Usage

import "forge.lthn.ai/core/go-config"

// Create config (defaults to ~/.core/config.yaml)
cfg, _ := config.New()

// With options
cfg, _ := config.New(
    config.WithPath("/custom/config.yaml"),
    config.WithEnvPrefix("MYAPP"),
)

// Dot-notation access
cfg.Set("dev.editor", "vim")

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

// Persist to disk
cfg.Commit()

// Environment variables override: CORE_CONFIG_DEV_EDITOR=code
// (prefix CORE_CONFIG, dots replaced with underscores)

Dependencies

  • forge.lthn.ai/core/go-io — File I/O abstraction
  • forge.lthn.ai/core/go-log — Error handling
  • forge.lthn.ai/core/go/pkg/core — Implements core.Config interface
  • github.com/spf13/viper — Configuration engine
  • gopkg.in/yaml.v3 — YAML marshalling