conf

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 5 Imported by: 0

README

conf: application configuration made simple

coverage Go Reference Go Report Card

conf makes application configuration simple by providing a small, opinionated API for building configuration structs using environment variables.

go get github.com/lucafmarques/conf

conf is a simple struct tag wrapper for env and as such, supports all types env does.

Example

package main

import "github.com/lucafmarques/conf"

type config struct {
	URIs       []string  `env:"CONFIG_URIS"`
	MaxSize    uint16    `env:"CONFIG_MAX_SIZE"`
	LogLevel   string    `env:"CONFIG_LOG_LEVEL"`
	TraceRatio float32   `env:"CONFIG_TRACE_RATIO"`
	CutoffDate time.Time `env:"CONFIG_CUTOFF_DATE"`
}

func main() {
  cfg := &config{}
  err := conf.Build(cfg)
  // ...
}

Opinions

conf was designed to be used as-is, without any knobs to tweak. Because of that design decision, conf assumes that:

  • A field of type []T must have its ENV value be a comma-separated list of items
  • A field of type struct must either:
    • have a struct tag itself and implement encoding.TextUnmarshaler
    • have a struct tag in its fields

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidType represents a type that can't be parsed by conf.
	ErrInvalidType = errors.New("invalid type")
	// ErrCantSetField represents a failure setting a field, likely because it isn't exported.
	ErrCantSetField = errors.New("struct field can't be set")
	// ErrNotStructReference represents calling conf.Build with a struct instead of a reference to a struct.
	ErrNotStructReference = errors.New("cfg argument must be a reference to a struct")
)

Functions

func Build

func Build(cfg any) error

Build attempts to build the cfg.

Types

This section is empty.

Jump to

Keyboard shortcuts

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