config

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package config provides configuration loading and management for markata-go.

Package config provides configuration loading and management for markata-go.

Configuration File Formats

The package supports three configuration formats:

  • TOML (primary, recommended) using github.com/BurntSushi/toml
  • YAML using gopkg.in/yaml.v3
  • JSON using the standard library

All formats use a top-level "markata-go" key to namespace configuration:

# TOML example
[markata-go]
output_dir = "public"
url = "https://example.com"

# YAML example
markata-go:
  output_dir: public
  url: https://example.com

# JSON example
{"markata-go": {"output_dir": "public", "url": "https://example.com"}}

Configuration Discovery

When no explicit config path is provided, the package searches for configuration files in the following order:

  1. ./markata-go.toml
  2. ./markata-go.yaml
  3. ./markata-go.yml
  4. ./markata-go.json
  5. ~/.config/markata-go/config.toml

If no configuration file is found, default values are used.

Environment Variable Overrides

Configuration values can be overridden using environment variables with the MARKATA_GO_ prefix. Environment variables take precedence over file configuration.

Simple fields:

MARKATA_GO_OUTPUT_DIR=public
MARKATA_GO_URL=https://example.com

Nested fields use underscores:

MARKATA_GO_GLOB_PATTERNS=posts/**/*.md,pages/*.md
MARKATA_GO_FEED_DEFAULTS_ITEMS_PER_PAGE=20

Boolean values accept: "true", "1", "yes" for true; "false", "0", "no" for false.

List values are comma-separated:

MARKATA_GO_HOOKS=markdown,template,sitemap

Usage

Basic usage:

// Load from default locations with env overrides
config, err := config.Load("")

// Load from specific file
config, err := config.Load("/path/to/config.toml")

// Load with defaults only
config, err := config.LoadWithDefaults()

// Load and validate
config, validationErrs, err := config.LoadAndValidate("/path/to/config.toml")

Merging Configurations

The MergeConfigs function performs a deep merge of two configurations:

base := config.DefaultConfig()
override := &models.Config{OutputDir: "custom"}
merged := config.MergeConfigs(base, override)

String fields are overridden if non-empty. Integer fields are overridden if non-zero. Slices are replaced if non-empty. Feed formats are replaced if any format is enabled.

Validation

Configurations can be validated to catch common errors and warnings:

errs := config.ValidateConfig(cfg)
if config.HasErrors(errs) {
    // Handle errors
}
if config.HasWarnings(errs) {
    // Handle warnings
}

Validation checks include:

  • URL format validation
  • Concurrency must be >= 0
  • Feed slugs are required
  • Warning on empty glob patterns
  • Warning on feeds with no output formats

Index

Constants

This section is empty.

Variables

View Source
var ErrConfigNotFound = errors.New("no configuration file found")

ErrConfigNotFound is returned when no config file is found.

Functions

func AppendDisabledHooks

func AppendDisabledHooks(config *models.Config, hooks ...string)

AppendDisabledHooks appends hooks to the configuration's DisabledHooks slice.

func AppendFeeds

func AppendFeeds(config *models.Config, feeds ...models.FeedConfig)

AppendFeeds appends feeds to the configuration's Feeds slice.

func AppendGlobPatterns

func AppendGlobPatterns(config *models.Config, patterns ...string)

AppendGlobPatterns appends patterns to the configuration's GlobConfig.Patterns slice.

func AppendHooks

func AppendHooks(config *models.Config, hooks ...string)

AppendHooks appends hooks to the configuration's Hooks slice.

func ApplyEnvOverrides

func ApplyEnvOverrides(config *models.Config) error

ApplyEnvOverrides applies environment variable overrides to a config. Environment variables are expected to follow the format MARKATA_GO_*. Nested keys use underscores: MARKATA_GO_FEEDS_DEFAULTS_ITEMS_PER_PAGE Boolean values: "true", "1", "yes" -> true; "false", "0", "no" -> false List values: comma-separated strings

func DefaultConfig

func DefaultConfig() *models.Config

DefaultConfig returns a Config with sensible default values.

func Discover

func Discover() (string, error)

Discover searches for a configuration file in the standard locations. It returns the path to the first config file found, or ErrConfigNotFound.

Discovery order:

  1. ./markata-go.toml
  2. ./markata-go.yaml (or .yml)
  3. ./markata-go.json
  4. ~/.config/markata-go/config.toml

func FromEnv

func FromEnv() *models.Config

FromEnv creates a Config entirely from environment variables. This is useful when no config file is available.

func GetEnvValue

func GetEnvValue(key string) (string, bool)

GetEnvValue returns the value of an environment variable with the MARKATA_GO_ prefix.

func HasErrors

func HasErrors(errs []error) bool

HasErrors returns true if the error slice contains any actual errors (not warnings).

func HasWarnings

func HasWarnings(errs []error) bool

HasWarnings returns true if the error slice contains any warnings.

func Load

func Load(configPath string) (*models.Config, error)

Load loads configuration from the specified file path. If configPath is empty, it will attempt to discover a config file. Environment variable overrides are applied after loading the file.

func LoadAndValidate

func LoadAndValidate(configPath string) (*models.Config, []error, error)

LoadAndValidate loads and validates configuration. Returns the config and any validation errors/warnings.

func LoadFromString

func LoadFromString(data string, format Format) (*models.Config, error)

LoadFromString parses configuration from a string with the specified format.

func LoadWithDefaults

func LoadWithDefaults() (*models.Config, error)

LoadWithDefaults returns a configuration with default values and environment variable overrides applied.

func MergeConfigs

func MergeConfigs(base, override *models.Config) *models.Config

MergeConfigs merges override configuration into base configuration. The override values take precedence over base values. For nested objects, a deep merge is performed. Arrays replace by default (use *_append fields for appending).

func MergeSlice

func MergeSlice[T any](base, override []T, appendMode bool) []T

MergeSlice merges two slices with optional append behavior. If append is true, the override slice is appended to the base slice. Otherwise, the override slice replaces the base slice.

func MustLoad

func MustLoad(configPath string) *models.Config

MustLoad loads configuration and panics on error. This is useful for initialization code where config loading failure should be fatal.

func ParseJSON

func ParseJSON(data []byte) (*models.Config, error)

ParseJSON parses JSON configuration data into a Config struct. The JSON data is expected to have a top-level "markata-go" key.

func ParseTOML

func ParseTOML(data []byte) (*models.Config, error)

ParseTOML parses TOML configuration data into a Config struct. The TOML data is expected to have a top-level [markata-go] section.

func ParseYAML

func ParseYAML(data []byte) (*models.Config, error)

ParseYAML parses YAML configuration data into a Config struct. The YAML data is expected to have a top-level markata-go key.

func SetEnvValue

func SetEnvValue(key, value string) error

SetEnvValue sets an environment variable with the MARKATA_GO_ prefix. This is primarily useful for testing.

func SplitErrorsAndWarnings

func SplitErrorsAndWarnings(errs []error) (errsOut, warnsOut []error)

SplitErrorsAndWarnings separates errors and warnings into separate slices.

func StructToEnvKeys

func StructToEnvKeys(prefix string, v interface{}) map[string]string

StructToEnvKeys returns a map of environment variable keys for a struct. This is useful for documentation and debugging.

func UnsetEnvValue

func UnsetEnvValue(key string) error

UnsetEnvValue unsets an environment variable with the MARKATA_GO_ prefix. This is primarily useful for testing.

func ValidateAndWarn

func ValidateAndWarn(config *models.Config, warnFunc func(error)) []error

ValidateAndWarn validates config and logs warnings but only returns actual errors. This is useful when you want to proceed with warnings but stop on errors.

func ValidateConfig

func ValidateConfig(config *models.Config) []error

ValidateConfig validates a configuration and returns any errors or warnings. Errors are returned first, followed by warnings. The returned slice will be nil if no issues are found.

Types

type Format

type Format string

Format represents a configuration file format.

const (
	FormatTOML Format = "toml"
	FormatYAML Format = "yaml"
	FormatJSON Format = "json"
)

type Path

type Path struct {
	Path   string // Full path to the config file
	Format Format // Format of the config file
	Source string // Where it was found: "cli", "cwd", "user"
}

Path holds the result of config file discovery with additional metadata.

func DiscoverAll

func DiscoverAll() []Path

DiscoverAll finds all config files in the standard locations. This is useful for debugging or showing available configs.

type ValidationError

type ValidationError struct {
	Field   string
	Message string
	IsWarn  bool // If true, this is a warning rather than an error
}

ValidationError represents a configuration validation error.

func (ValidationError) Error

func (e ValidationError) Error() string

Jump to

Keyboard shortcuts

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