configyaml

package module
v0.0.0-...-0d68cb6 Latest Latest
Warning

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

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

README

configyaml Package

github.com/OpenNSW/core/configyaml loads a YAML config file into a Go struct, resolving "{{env:NAME}}" / "{{file:/path}}" placeholders via github.com/OpenNSW/core/secret so secrets never need to live in the checked-in file.

Why

Several SDK config types now carry yaml struct tags for generic embedding (database.Config, temporal.Config, cors.Config, and more per-package going forward). This package is the loader that pairs with them: it decodes into whatever struct you pass — including one that embeds those tagged types — without that struct needing to know which of its own fields might be sourced from an env var or a mounted file. That's a property of how the YAML file was authored, not of the Go type reading it.

Usage

type Config struct {
    DB   database.Config `yaml:"db"`
    CORS cors.Config     `yaml:"cors"`
}

var cfg Config
if err := configyaml.LoadAndExpand("config.yaml", &cfg); err != nil {
    log.Fatal(err)
}
db:
  host: localhost
  port: 5432
  user: postgres
  password: "{{env:DB_PASSWORD}}" # resolved from the DB_PASSWORD env var
cors:
  allowedOrigins: ["https://portal.example.com"]

A placeholder must be the entire scalar value — "prefix-{{env:X}}-suffix" is left as a literal, not partially resolved. Resolution also isn't limited to string fields: a non-string field (e.g. a port number) templated the same way still decodes to the right type.

Failure mode

Like secret.SecretRef.Resolve, a placeholder never silently resolves to the empty string — an unset env var or missing file is a loud error, reported with the dotted/indexed YAML path to the offending field (e.g. db.password: environment variable "DB_PASSWORD" is not set or is empty).

Adding a placeholder scheme

configyaml itself doesn't know about schemes — every {{scheme:ref}} is resolved via secret.SecretRef(ref).Resolve(). To add a new scheme (e.g. vault:), register it in secret's secretSchemes map — see that package's README.

Documentation

Overview

Package configyaml loads a YAML config file into a Go struct, resolving any "{{env:NAME}}" (read from an env var) or "{{file:/path}}" (read from a mounted file) placeholder found in it via github.com/OpenNSW/core/secret.

Resolution runs generically over the parsed YAML tree before it is decoded into any typed struct, so the config types passed to LoadAndExpand never need to know which of their own fields might be sourced from an env var or a file — that's a property of how the YAML file was authored, not of the Go type reading it. This pairs with the `yaml` struct tags already carried by config types such as database.Config, temporal.Config, and cors.Config: embed those (or your own) in an application config struct and load it with LoadAndExpand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadAndExpand

func LoadAndExpand(path string, v any) error

LoadAndExpand reads the YAML file at path, resolves every "{{env:NAME}}" / "{{file:/path}}" placeholder in it, and decodes the result into v (a pointer, as for yaml.Unmarshal).

Types

This section is empty.

Jump to

Keyboard shortcuts

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