configuration

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 5 Imported by: 0

README

configuration

Go Reference
A small Go package for loading and saving application configuration as YAML using the XDG Base Directory specification.

Configuration is stored per executable under:

$XDG_CONFIG_HOME//config

The package provides transparent in-memory caching, and enforces strict type consistency across reads.

Features

  • YAML-based configuration files using go.yaml.in/yaml/v4 lib
  • XDG-compliant configuration directory resolution
  • Generic API (Get[T], Save[T])
  • Automatic in-memory caching
  • Strict type safety across calls
  • Secure file permissions (0600)

Installation

go get github.com/devilcove/configuration

Usage

Define a Configuration Struct

type Config struct {
    Host string `yaml:"host"`
    Port int    `yaml:"port"`
}

Load Configuration

// default file - config
var cfg Config
if err := configuration.Get(&cfg); err != nil {
    log.Fatal(err)
}

// use alternate filename
var cfg Config
if err := configuration.Get(&cfg, "alternate"); err != nil {
    log.Fatal(err)
}
  • On first call, the configuration is read from disk and cached.
  • Subsequent calls return the cached value.
  • The supplied type must match the type used on the first call.

Save Configuration

cfg := Config{
    Host: "localhost",
    Port: 8080,
}

// default filename
if err := configuration.Save(&cfg); err != nil {
    log.Fatal(err)
}
// alternate filename
if err := configuration.Save(&cfg, "alternate"); err != nil {
    log.Fatal(err)
}
  • The configuration is serialized to YAML.
  • The file is written with permissions 0600.
  • The saved configuration replaces the in-memory cache.

Configuration File Location

The configuration file is always stored in:

$XDG_CONFIG_HOME/<executable-name>/

Where:

  • XDG_CONFIG_HOME is resolved using os.UserConfigDir()
  • <executable-name> is derived from os.Args[0]

the default filename is config; an alternate filename can be passed as additional argument to Get or Save functions

Examples:
OS Path Example
Linux ~/.config/myapp/config
macOS ~/Library/Application Support/myapp/config
Windows %AppData%\myapp\config

Caching Behavior

  • Configuration is loaded once per process.
  • All subsequent Get calls return the cached value.
  • Mixing different configuration types in the same process is not allowed.

Example of invalid usage:

var a ConfigA
var b ConfigB

configuration.Get(&a)
configuration.Get(&b) // returns ErrInterfaceConversion

Error Handling

The package exposes the following sentinel errors:

Name Error
ErrInterfaceConverion the cached type does not match
ErrYAMLMarshal unable to marshal data

Errors are returned when:

  • No configuration directory can be resolved
  • The config file cannot be read or written
  • The YAML cannot be unmarshaled into the target type

Licence

MIT Licence. See LICENCE for details.

Documentation

Overview

Package configuration provides functions to save and read configuration structures.

Configuration files are read from/save to XDG_CONFIG_HOME/<executable name>/config.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInterfaceConversion indicates that supplied T is different from cached type.
	ErrInterfaceConversion = errors.New("interface conversion")
	// ErrYAMLMarshal indicates error marshalling supplied data to YAML.
	ErrYAMLMarshal = errors.New("unable to (un)marshal data to/from yaml")
)

Functions

func Get

func Get[T any](config *T, names ...string) error

Get returns the configuration data for the supplied configuration struct type T, caching it after first retrieval. Error will be returned if:

- both XDG_CONFIG_HOME and HOME env vars not set.

- user lacks permission to read from XDG_CONFIG_HOME/<executable name>/config.

- on subsequent calls, supplied T must be same as original T.

- config file cannot be converted to supplied T.

func Save

func Save[T any](config *T, names ...string) (err error)

Save saves the provided struct as a yaml config file in $XDG_CONFIG_HOME/executable name/ and updates the cache. Config dir and file willl be created if it dosen't exist Error will be returned if:

- both XDG_CONFIG_HOME and HOME env vars not set.

- user lacks permission to write to XDG_CONFIG_HOME/<executable name>/.

- supplied strut T cannot be marshalled to yaml.

Types

This section is empty.

Jump to

Keyboard shortcuts

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