Documentation
¶
Index ¶
- func FlagOrEnv(r Getter, flag *string, key string) string
- func GetOrDefault(r Getter, key, def string) string
- func Required(r Getter, key string) (string, error)
- func Revokable(r GetSetter, key, value string) (func() error, error)
- func RevokableMany(r GetSetter, envs map[string]string) (func() error, error)
- func Scoped(r GetSetter, key, value string, fn func()) (err error)
- func ScopedMany(r GetSetter, envs map[string]string, fn func()) (err error)
- type CommandLocator
- type GetSetter
- type Getter
- type Repository
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FlagOrEnv ¶
FlagOrEnv returns *flag when it points to a non-empty string, else r.Get(key). A nil pointer is treated as unset.
func GetOrDefault ¶
GetOrDefault returns r.Get(key) when non-empty, else def.
func Revokable ¶
Revokable sets key to value on r and returns a function that restores the previous value when invoked.
func RevokableMany ¶
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 ¶
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 ¶
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 ¶
CommandLocator ...
type GetSetter ¶
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 ¶
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.