env

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpEnv

func DumpEnv(env Env) string

DumpEnv returns a sorted, newline separated representation of the environment visible via env. Each line is formatted as "KEY=VALUE".

For TestEnv and OsEnv the function enumerates the known keys. For other Env implementations the function attempts to use common helper methods (Environ or Keys) if available. If enumeration is not possible a short message is returned indicating that limitation.

func ExpandEnv

func ExpandEnv(env Env, s string) string

ExpandEnv expands $var or ${var} in s using the provided env. If env is nil, the real OS environment is used via OsEnv.

func GetDefault

func GetDefault(env Env, key, other string) string

GetDefault returns the value of key from env when present and non-empty. Otherwise it returns the provided fallback value. Use this helper when a preference for an environment value is desired while still allowing a concrete default.

Types

type Env

type Env interface {
	jail.Jailed

	// Name of the environment
	Name() string

	// Get returns the raw environment value for key. The return value may be
	// empty when the key is not present.
	Get(key string) string

	// Set assigns the environment key to value.
	Set(key, value string) error

	// Has reports whether the environment key is set.
	Has(key string) bool

	// Environ returns a copy of the environment as a slice of strings in the
	// form "KEY=VALUE".
	Environ() []string

	// Unset removes the environment key.
	Unset(key string)

	// GetHome returns the user's home directory. Implementations should return
	// an error if the value is not available.
	GetHome() (string, error)

	// SetHome sets the user's home directory in the environment.
	SetHome(home string) error

	// GetUser returns the current user's username. Implementations should
	// return an error if the value is not available.
	GetUser() (string, error)

	// SetUser sets the current user's username in the environment.
	SetUser(user string) error

	// Getwd returns the working directory as seen by this Env. For OsEnv this
	// is the process working directory; for TestEnv it is the stored PWD.
	Getwd() (string, error)

	// Setwd sets the working directory for this Env. For OsEnv this may change
	// the process working directory; for TestEnv it updates the stored PWD.
	Setwd(dir string) error

	// GetTempDir returns an appropriate temp directory for this Env. For OsEnv
	// this delegates to os.TempDir(); TestEnv provides testable fallbacks.
	GetTempDir() string
}

Env is a compact interface for reading and modifying environment values. Implementations may reflect the real process environment (OsEnv) or provide an in-memory view suitable for tests (TestEnv).

The interface mirrors common environment operations so callers can inject a test implementation for unit tests without touching the real process env.

type EnvCloner

type EnvCloner interface {
	CloneEnv() Env
}

EnvCloner is implemented by environments that can create a deep-copy clone suitable for isolated mutation in concurrent runs.

type OsEnv

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

OsEnv is an Env implementation that delegates to the real process environment. Use this in production code where access to the actual OS environment is required.

func (*OsEnv) CloneEnv

func (o *OsEnv) CloneEnv() Env

func (*OsEnv) Environ

func (o *OsEnv) Environ() []string

Environ returns a copy of the process environment in "key=value" form.

func (*OsEnv) ExpandPath

func (o *OsEnv) ExpandPath(p string) string

func (*OsEnv) Get

func (o *OsEnv) Get(key string) string

Get returns the environment variable for key.

func (*OsEnv) GetHome

func (o *OsEnv) GetHome() (string, error)

GetHome returns the home directory reported by the OS. It delegates to os.UserHomeDir.

func (*OsEnv) GetJail

func (o *OsEnv) GetJail() string

func (*OsEnv) GetTempDir

func (o *OsEnv) GetTempDir() string

GetTempDir returns the OS temporary directory.

func (*OsEnv) GetUser

func (o *OsEnv) GetUser() (string, error)

GetUser returns the current OS user username. If the Username field is empty it falls back to the user's Name field.

func (*OsEnv) Getwd

func (o *OsEnv) Getwd() (string, error)

Getwd returns the current process working directory.

func (*OsEnv) Has

func (o *OsEnv) Has(key string) bool

Has reports whether the given environment key is present.

func (*OsEnv) Name

func (o *OsEnv) Name() string

func (*OsEnv) Set

func (o *OsEnv) Set(key string, value string) error

Set sets the OS environment variable key to value.

func (*OsEnv) SetHome

func (o *OsEnv) SetHome(home string) error

SetHome sets environment values that represent the user's home directory.

On Windows it also sets USERPROFILE to satisfy common callers.

func (*OsEnv) SetJail

func (o *OsEnv) SetJail(jail string) error

func (*OsEnv) SetUser

func (o *OsEnv) SetUser(username string) error

SetUser sets environment values that represent the current user.

On Windows it also sets USERNAME in addition to USER.

func (*OsEnv) Setwd

func (o *OsEnv) Setwd(dir string) error

Setwd attempts to change the process working directory to dir.

It also attempts to update PWD.

func (*OsEnv) Unset

func (o *OsEnv) Unset(key string)

Unset removes the OS environment variable.

type TestEnv

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

TestEnv is an in-memory Env implementation useful for tests. It does not touch the real process environment and therefore makes tests hermetic.

The home and user fields satisfy GetHome and GetUser. The data map stores other keys. For convenience, setting or unsetting the keys "HOME" and "USER" updates the corresponding home and user fields.

func NewTestEnv

func NewTestEnv(jailPath, home, username string) *TestEnv

NewTestEnv constructs a TestEnv populated with sensible defaults for tests. It sets HOME and USER and also sets platform specific variables so functions that prefer XDG_* on Unix or APPDATA/LOCALAPPDATA on Windows will pick them up.

If home or username are empty, reasonable defaults are chosen:

  • home defaults to EnsureInJailFor(jail, "/home/<username>")
  • username defaults to "testuser"

The function does not create directories on disk. It only sets environment values in the returned TestEnv.

func (*TestEnv) Clone

func (env *TestEnv) Clone() *TestEnv

Clone returns a copy of the TestEnv so tests can modify the returned environment without mutating the original. It deep-copies the internal map.

func (*TestEnv) CloneEnv

func (env *TestEnv) CloneEnv() Env

func (*TestEnv) Environ

func (env *TestEnv) Environ() []string

Environ returns a slice of "KEY=VALUE" entries representing the environment stored in the TestEnv. It guarantees HOME and USER are present when set.

func (*TestEnv) ExpandPath

func (env *TestEnv) ExpandPath(p string) string

ExpandPath expands a leading tilde in the provided path to the TestEnv home. Supported forms:

"~"
"~/rest/of/path"
"~\\rest\\of\\path" (Windows)

If the path does not start with a tilde it is returned unchanged. This method uses the TestEnv GetHome value. If home is not set, expansion may produce an empty or unexpected result.

func (TestEnv) Get

func (env TestEnv) Get(key string) string

Get returns the stored value for key. Reading from a nil map returns the zero value, so this method is safe on a zero TestEnv. The special keys HOME and USER come from dedicated fields.

func (*TestEnv) GetHome

func (env *TestEnv) GetHome() (string, error)

GetHome returns the configured home directory or an error if it is not set.

For TestEnv the returned home is guaranteed to be located within the configured jail when possible. This helps keep tests hermetic by ensuring paths used for home are under the test temporary area.

func (*TestEnv) GetJail

func (env *TestEnv) GetJail() string

func (*TestEnv) GetTempDir

func (env *TestEnv) GetTempDir() string

GetTempDir returns a temp directory appropriate for the TestEnv. If the receiver is nil this falls back to os.TempDir to avoid panics.

The method prefers explicit TMPDIR/TEMP/TMP values stored in the TestEnv. On Windows it applies a series of fallbacks: LOCALAPPDATA, APPDATA, USERPROFILE, then a home-based default. On Unix-like systems it falls back to /tmp.

The returned path will be adjusted to reside inside the configured jail when possible to keep test artifacts contained.

func (*TestEnv) GetUser

func (env *TestEnv) GetUser() (string, error)

GetUser returns the configured username or an error if it is not set.

func (*TestEnv) Getwd

func (env *TestEnv) Getwd() (string, error)

Getwd returns the TestEnv's PWD value if set, otherwise an error.

func (*TestEnv) Has

func (env *TestEnv) Has(key string) bool

Has reports whether the given key is present in the TestEnv map.

func (*TestEnv) Name

func (env *TestEnv) Name() string

func (*TestEnv) ResolvePath

func (env *TestEnv) ResolvePath(rel string, follow bool) (string, error)

func (*TestEnv) Set

func (env *TestEnv) Set(key string, value string) error

Set stores a key/value pair in the TestEnv. If key is "HOME" or "USER" the corresponding dedicated field is updated. Calling Set on a nil receiver returns an error rather than panicking.

func (*TestEnv) SetHome

func (env *TestEnv) SetHome(rel string) error

SetHome sets the TestEnv home directory and updates the "HOME" key in the underlying map for callers that read via Get.

func (*TestEnv) SetJail

func (env *TestEnv) SetJail(jailPath string) error

func (*TestEnv) SetUser

func (env *TestEnv) SetUser(username string) error

SetUser sets the TestEnv current user and updates the "USER" key in the underlying map for callers that use Get.

func (*TestEnv) Setwd

func (env *TestEnv) Setwd(dir string) error

Setwd sets the TestEnv's PWD value to the provided directory.

func (*TestEnv) Unset

func (env *TestEnv) Unset(key string)

Unset removes a key from the TestEnv. If key is "HOME" or "USER" the corresponding field is cleared. Calling Unset on a nil receiver is a no-op.

Jump to

Keyboard shortcuts

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