tmpenv

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Envguard

type Envguard struct {
	// contains filtered or unexported fields
}

Envguard is a state of environment variables which will be restored at Restore() method call.

func All

func All() *Envguard

All creates a new Envguard instance with all existing environment variables. All environment variables will be restored by calling Restore().

func New

func New(keys ...string) *Envguard

New creates a new Envguard instance. If one ore more keys are given, corresponding environment variables will be restored at Restore() method call.

func Setenvs

func Setenvs(m map[string]string) (*Envguard, error)

Setenvs sets environment variables with given key-values. And creates a new Envguard instance to restore them when calling Restore().

func Unset

func Unset(keys ...string) (*Envguard, error)

Unset creates a new Envguard instance with unsetting all given anvironment variables. The unset variables will be restored by calling Restore() method. When underlying os.Unsetenv() failed, it returns an error.

func UnsetAll

func UnsetAll() (*Envguard, error)

UnsetAll creates a new Envguard instance with clearing all environment variables.

func (*Envguard) Add

func (guard *Envguard) Add(keys ...string)

Add adds an environment variable by key.

func (*Envguard) Remove

func (guard *Envguard) Remove(keys ...string) (deleted bool)

Remove removes given keys from stored environment variables state.

func (*Envguard) Restore

func (guard *Envguard) Restore() error

Restore restores stored environment variable values. This method is usually called with 'defer' to ensure the state to be restored. This function returns an error when underlying Setenv() calls returned an error except for on Windows. On Windows this function returns nil always since some environment variables are not set on Windows and it is intentional.

func (*Envguard) Setenv

func (guard *Envguard) Setenv(key, val string) error

Setenv sets the environment variable to the given value. The set variable will be restored when calling Restore().

func (*Envguard) Unsetenv

func (guard *Envguard) Unsetenv(key string) error

Unsetenv unsets the environment variable. When Restore() method is called, the unset variable will be restored. When underlying os.Unsetenv() call failed, it returns an error

Jump to

Keyboard shortcuts

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