envvar

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package envvar loads environment-backed configuration through explicit adapter helpers.

Safe startup paths ------------------

The adapter keeps legacy panic-oriented methods (Must*) and adds Try* variants that return parse errors instead of panicking:

token, err := adapter.TryGet("TOKEN")
if err != nil { ... }

// or
if err := adapter.TryLoadEnvFiles([]string{"./.env"}); err != nil { ... }

This allows callers to keep startup failure strictness where desired while preserving a non-panicking path for validation and diagnostics.

Bind helpers ------------

Use TryBind/TryBindWithPrefix when loading config structs if you want to handle parse errors explicitly:

var cfg Config
if err := adapter.TryBindWithPrefix(&cfg, "APP_"); err != nil { ... }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() ports.EnvVar

New creates a new envvar adapter that satisfies ports.EnvVar.

Types

type Adapter

type Adapter struct{}

Adapter provides environment variable access using the envvar library.

func (*Adapter) Bind

func (a *Adapter) Bind(dst any) error

Bind populates a struct from environment variables. This is a simplified implementation that doesn't use struct tags.

func (*Adapter) BindWithPrefix

func (a *Adapter) BindWithPrefix(dst any, prefix string) error

BindWithPrefix binds with a prefix.

func (*Adapter) DumpRedacted

func (a *Adapter) DumpRedacted() map[string]string

DumpRedacted returns environment with secrets redacted.

func (*Adapter) Get

func (a *Adapter) Get(key string) (string, bool)

Get returns the raw value and presence indicator.

func (*Adapter) GetBoolOr

func (a *Adapter) GetBoolOr(key string, def bool) bool

GetBoolOr returns the value as boolean or default if not present.

func (*Adapter) GetDurationOr

func (a *Adapter) GetDurationOr(key string, def time.Duration) time.Duration

GetDurationOr returns the value as duration or default if not present.

func (*Adapter) GetFloat64Or

func (a *Adapter) GetFloat64Or(key string, def float64) float64

GetFloat64Or returns the value as float64 or default if not present.

func (*Adapter) GetInt64Or

func (a *Adapter) GetInt64Or(key string, def int64) int64

GetInt64Or returns the value as int64 or default if not present.

func (*Adapter) GetIntOr

func (a *Adapter) GetIntOr(key string, def int) int

GetIntOr returns the value as integer or default if not present.

func (*Adapter) GetOr

func (a *Adapter) GetOr(key, def string) string

GetOr returns the value or default if not present.

func (*Adapter) GetUint64Or

func (a *Adapter) GetUint64Or(key string, def uint64) uint64

GetUint64Or returns the value as uint64 or default if not present.

func (*Adapter) GetUintOr

func (a *Adapter) GetUintOr(key string, def uint) uint

GetUintOr returns the value as uint or default if not present.

func (*Adapter) LoadEnvFiles

func (a *Adapter) LoadEnvFiles(paths []string) error

LoadEnvFiles loads environment variables from files. Tries .env then /env/.env by default.

func (*Adapter) MustBind

func (a *Adapter) MustBind(dst any)

MustBind panics on binding errors.

func (*Adapter) MustBindWithPrefix

func (a *Adapter) MustBindWithPrefix(dst any, prefix string)

MustBindWithPrefix panics on binding errors with prefix.

func (*Adapter) MustGet

func (a *Adapter) MustGet(key string) string

MustGet returns the value or panics if not present.

func (*Adapter) MustGetBool

func (a *Adapter) MustGetBool(key string) bool

MustGetBool returns the value as boolean or panics if not present.

func (*Adapter) MustGetDuration

func (a *Adapter) MustGetDuration(key string) time.Duration

MustGetDuration returns the value as duration or panics if not present.

func (*Adapter) MustGetFloat64

func (a *Adapter) MustGetFloat64(key string) float64

MustGetFloat64 returns the value as float64 or panics if not present.

func (*Adapter) MustGetInt

func (a *Adapter) MustGetInt(key string) int

MustGetInt returns the value as integer or panics if not present.

func (*Adapter) MustGetInt64

func (a *Adapter) MustGetInt64(key string) int64

MustGetInt64 returns the value as int64 or panics if not present.

func (*Adapter) MustGetUint

func (a *Adapter) MustGetUint(key string) uint

MustGetUint returns the value as uint or panics if not present.

func (*Adapter) MustGetUint64

func (a *Adapter) MustGetUint64(key string) uint64

MustGetUint64 returns the value as uint64 or panics if not present.

func (*Adapter) MustLoadEnvFiles

func (a *Adapter) MustLoadEnvFiles(paths []string)

MustLoadEnvFiles panics on errors when loading environment files.

func (*Adapter) TryBind added in v2.1.0

func (a *Adapter) TryBind(dst any) error

TryBind populates a struct from environment variables.

func (*Adapter) TryBindWithPrefix added in v2.1.0

func (a *Adapter) TryBindWithPrefix(dst any, prefix string) error

TryBindWithPrefix binds with a prefix and returns errors.

func (*Adapter) TryGet added in v2.1.0

func (a *Adapter) TryGet(key string) (string, error)

TryGet returns the value and an error when missing.

func (*Adapter) TryGetBool added in v2.1.0

func (a *Adapter) TryGetBool(key string) (bool, error)

TryGetBool returns the value as boolean or an error.

func (*Adapter) TryGetDuration added in v2.1.0

func (a *Adapter) TryGetDuration(key string) (time.Duration, error)

TryGetDuration returns the value as duration or an error.

func (*Adapter) TryGetFloat64 added in v2.1.0

func (a *Adapter) TryGetFloat64(key string) (float64, error)

TryGetFloat64 returns the value as float64 or an error.

func (*Adapter) TryGetInt added in v2.1.0

func (a *Adapter) TryGetInt(key string) (int, error)

TryGetInt returns the value as integer or an error.

func (*Adapter) TryGetInt64 added in v2.1.0

func (a *Adapter) TryGetInt64(key string) (int64, error)

TryGetInt64 returns the value as int64 or an error.

func (*Adapter) TryGetUint added in v2.1.0

func (a *Adapter) TryGetUint(key string) (uint, error)

TryGetUint returns the value as uint or an error.

func (*Adapter) TryGetUint64 added in v2.1.0

func (a *Adapter) TryGetUint64(key string) (uint64, error)

TryGetUint64 returns the value as uint64 or an error.

func (*Adapter) TryLoadEnvFiles added in v2.1.0

func (a *Adapter) TryLoadEnvFiles(paths []string) error

TryLoadEnvFiles loads environment variable files and returns an error.

Jump to

Keyboard shortcuts

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