configenv

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 8 Imported by: 0

README

configenv

Go package for parsing env vars.

The main idea is to enforce (on the package API design level) the best practice of parsing env vars: prevent duplicate names, wrong types, typos, etc. See Writing safe-to-use Go libraries on the design principles behind my Go packages, including this one.

Also, try out cliff, a package for safely parsing CLI args.

Installation

go get github.com/orsinium-labs/configenv

Usage

Given the following parser:

type Config struct {
    Debug bool
    Env   string
}

config := Config{}
vars := configenv.Vars{
    "DEBUG": configenv.Required(configenv.Bool(&config.Debug)),
    "ENV":   configenv.String(&config.Env),
}
err := vars.Parse(configenv.Config{Prefix: "BE_"})

And the following env vars:

export BE_DEBUG=true
export BE_ENV=prod

You will get Config{Debug: true, Env: "prod"}

If you instead of BE_DEBUG=true provide DEBUG=true, BE_DEUG=true, or BE_DEBUG=maybe, the parser will detect it and return an error.

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/orsinium-labs/configenv"
)

type Config struct {
	Debug bool
	Env   string
}

func main() {
	env := []string{
		"BE_DEBUG=true",
		"BE_ENV=prod",
		"ENV=fake",
	}
	config := Config{}
	vars := configenv.Vars{
		"DEBUG": configenv.Required(configenv.Bool(&config.Debug)),
		"ENV":   configenv.String(&config.Env),
	}
	err := vars.Parse(configenv.Config{
		Environ: env,
		Prefix:  "BE_",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(config.Env)
}
Output:

prod

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	R = Required
	S = String[string]
	I = Int[int]
	U = Uint[uint]
	B = Bool[bool]
	F = Float64[float64]
)

One-letter aliases for people living on the edge.

Functions

func Bool

func Bool[T ~bool](target *T) parser

func Float32

func Float32[T ~float32](target *T) parser

func Float64

func Float64[T ~float64](target *T) parser

func Int

func Int[T ~int](target *T) parser

func Int8

func Int8[T ~int8](target *T) parser

func Int16

func Int16[T ~int16](target *T) parser

func Int32

func Int32[T ~int32](target *T) parser

func Int64

func Int64[T ~int64](target *T) parser

func JSON added in v1.1.0

func JSON[T any](target *T) parser

func Map

func Map(p parser, f func(string) string) parser

Apply the function to the env var value before parsing it.

func PrefixMap added in v1.1.0

func PrefixMap[M ~map[K]V, K ~string, V any](target *M, p Parser[V]) parser

func PrefixSlice added in v1.1.0

func PrefixSlice[A ~[]V, V any](target *A, p Parser[V]) parser

func Required

func Required(p parser) parser

func Slice added in v1.1.0

func Slice[A ~[]V, V any](target *A, sep string, p Parser[V]) parser

func String

func String[T ~string](target *T) parser

func Strings

func Strings[A ~[]V, V ~string](target *A, sep string) parser

func Uint

func Uint[T ~uint](target *T) parser

func Uint8

func Uint8[T ~uint8](target *T) parser

func Uint16

func Uint16[T ~uint16](target *T) parser

func Uint32

func Uint32[T ~uint32](target *T) parser

func Uint64

func Uint64[T ~uint64](target *T) parser

Types

type C

type C = Config

One-letter aliases for people living on the edge.

type Config

type Config struct {
	// Pairs of name=value env vars. If not provided, [os.Environ] will be used.
	Environ []string
	// The prefix to add to all env var names.
	//
	// The prefix ensures that env vars between services don't conflict
	Prefix string
	// If true, will not return an error for unknown env vars with the given prefix.
	//
	// By default, extra env vars are forbidden which prevents typos in env var names.
	// For example, if you write `BE_DEUG=false` instead of `BE_DEBUG=false`.
	AllowExtra bool
	// Require all env vars to be present and non-empty, even if not wrapped in [Required].
	//
	// It's a good idea to set it on the production to ensure no env var is forgotten.
	RequireAll bool
}

type Parser added in v1.1.0

type Parser[T any] func(*T) parser

type V

type V = Vars

One-letter aliases for people living on the edge.

type Vars

type Vars map[string]parser

func (Vars) Parse

func (vars Vars) Parse(cfg Config) error

Jump to

Keyboard shortcuts

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