env

package
v2.0.0-alpha.39 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: MIT Imports: 4 Imported by: 129

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlagOrEnv

func FlagOrEnv(r Getter, flag *string, key string) string

FlagOrEnv returns *flag when it points to a non-empty string, else r.Get(key). A nil pointer is treated as unset.

func GetOrDefault

func GetOrDefault(r Getter, key, def string) string

GetOrDefault returns r.Get(key) when non-empty, else def.

func Required

func Required(r Getter, key string) (string, error)

Required returns r.Get(key) or an error when it is unset or empty.

func Revokable

func Revokable(r GetSetter, key, value string) (func() error, error)

Revokable sets key to value on r and returns a function that restores the previous value when invoked.

func RevokableMany

func RevokableMany(r GetSetter, envs map[string]string) (func() error, error)

RevokableMany sets every key in envs on r and returns a revoke function that restores the previous values. If any Set fails, every key already written is restored before returning on a best-effort basis; the returned error wraps both the Set failure and any restore failures, and the returned revoke is a no-op.

func Scoped

func Scoped(r GetSetter, key, value string, fn func()) (err error)

Scoped sets key to value on r, invokes fn, then restores the previous value. The restore runs even if fn panics, and even when the initial Set reports an error (a GetSetter may leave state written and still return an error).

func ScopedMany

func ScopedMany(r GetSetter, envs map[string]string, fn func()) (err error)

ScopedMany applies every entry in envs on r, invokes fn, then restores all previous values. Restore runs even if fn panics. On setup failure RevokableMany has already restored every key it touched and returns a no-op revoke, so the deferred call is a safe cleanup either way.

Types

type CommandLocator

type CommandLocator interface {
	LookPath(file string) (string, error)
}

CommandLocator ...

func NewCommandLocator

func NewCommandLocator() CommandLocator

NewCommandLocator ...

type GetSetter

type GetSetter interface {
	Get(key string) string
	Set(key, value string) error
}

GetSetter is the minimal interface required to read and write environment variables. Callers should prefer defining their own local interface at the use site; this type exists so the helpers in this package can stay small.

type Getter

type Getter interface {
	Get(key string) string
}

Getter is the minimal interface required to read an environment variable. Callers should prefer defining their own local interface at the use site; this type exists so the helpers in this package can stay small.

type Repository

type Repository interface {
	// List returns the current environment as "KEY=VALUE" entries, like
	// os.Environ().
	List() []string
	// Unset removes key from the environment.
	Unset(key string) error
	// Get returns the value of key, or "" when unset.
	Get(key string) string
	// Set assigns value to key.
	Set(key, value string) error
}

Repository abstracts read/write access to process environment variables. Implementations should be safe to replace in tests (e.g. with an in-memory fake) without touching the real process environment.

func NewRepository

func NewRepository() Repository

NewRepository ...

Jump to

Keyboard shortcuts

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