conf

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package conf loads the typed configuration behind chok.Load.

Sources, lowest to highest: `default` tags, the YAML file, environment variables named after the key path ({PREFIX}_HTTP_ADDR), then flags named after it (--http.addr). Decoding is strict: an unknown key or section is an error with a suggestion, a value of the wrong shape is an error naming its path. Validation runs the `validate` tags and then every Validate method. All problems are reported together.

Index

Constants

View Source
const Masked = "***"

Masked replaces a non-empty secret in Dump output.

Variables

This section is empty.

Functions

func Check

func Check(v any) error

Check runs the validate tags and Validate methods on the struct v points to, as Load does, for configuration built in code.

func Defaults

func Defaults(dst any) error

Defaults sets every field of the struct dst points to that has a `default` tag, at any depth; fields without one keep their value.

func Dump

func Dump(v any) ([]byte, error)

Dump renders v as YAML under its yaml key names, with every field tagged secret:"true" masked. It is what --check-config prints.

func EnvName

func EnvName(prefix, path string) string

EnvName is the environment variable that overrides a key path.

func Field

func Field(cfg any, path string) (reflect.Value, bool)

Field finds the field at a dotted key path in the struct cfg points to.

func Prefix

func Prefix(name string) string

Prefix is the environment variable prefix derived from an app name.

Types

type Error

type Error struct {
	File     string
	Problems []string
}

Error is a failed Load with every problem found.

func (*Error) Error

func (e *Error) Error() string

type Key

type Key struct {
	Path    string
	Type    string
	Default string
	Secret  bool
	// Rules is the field's validate tag.
	Rules string
	// Decl is the named struct type whose declaration holds the field and
	// Field the Go field names leading from it to the field: one name, or
	// more when the field sits in an anonymous struct, as the sections of
	// telemetry.Config and cache.Config do. chok docs gen reads the
	// field's comment through them.
	Decl  reflect.Type
	Field []string
}

Key describes one leaf of a configuration type, for documentation and for the configkeys budget. Map entries appear once with "<name>" for the key and list elements with "[]".

func Keys

func Keys(t reflect.Type) []Key

Keys lists the leaf keys of struct type t, sorted by path.

type Options

type Options struct {
	// Name is the application name: it derives the environment prefix
	// and the file candidates {name}.yaml and configs/{name}.yaml.
	Name string
	// EnvPrefix overrides the prefix derived from Name.
	EnvPrefix string
	// Override is the file named on the command line (--config); it
	// beats {PREFIX}_CONFIG and File. A missing one is an error.
	Override string
	// File is the file the code names (ConfigFile); a missing one is an
	// error.
	File string
	// Flags is a parsed flag set; changed flags named after a key path win.
	Flags *pflag.FlagSet
	// Environ is the environment, os.Environ() unless a test sets it.
	Environ []string
	// Only, when set, loads just these top-level sections of the file:
	// the others are skipped rather than reported as unknown, and only
	// environment variables inside them can warn. The chok command reads
	// the db section of an application's chok.yaml this way, with a type
	// that has nothing else.
	Only []string
}

Options controls one Load.

type Result

type Result struct {
	// File is the configuration file read, "" when none was found.
	File string
	// Warnings lists environment variables with the prefix that match no key.
	Warnings []string
}

Result reports what Load read.

func Load

func Load(dst any, o Options) (Result, error)

Load fills the struct dst points to.

type Validator

type Validator interface{ Validate() error }

Validator is implemented by configuration structs that check themselves beyond what validate tags express.

Jump to

Keyboard shortcuts

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