Documentation
¶
Index ¶
- func DumpEnv(env Env) string
- func ExpandEnv(env Env, s string) string
- func GetDefault(env Env, key, other string) string
- type Env
- type EnvCloner
- type OsEnv
- func (o *OsEnv) CloneEnv() Env
- func (o *OsEnv) Environ() []string
- func (o *OsEnv) ExpandPath(p string) string
- func (o *OsEnv) Get(key string) string
- func (o *OsEnv) GetHome() (string, error)
- func (o *OsEnv) GetJail() string
- func (o *OsEnv) GetTempDir() string
- func (o *OsEnv) GetUser() (string, error)
- func (o *OsEnv) Getwd() (string, error)
- func (o *OsEnv) Has(key string) bool
- func (o *OsEnv) Name() string
- func (o *OsEnv) Set(key string, value string) error
- func (o *OsEnv) SetHome(home string) error
- func (o *OsEnv) SetJail(jail string) error
- func (o *OsEnv) SetUser(username string) error
- func (o *OsEnv) Setwd(dir string) error
- func (o *OsEnv) Unset(key string)
- type TestEnv
- func (env *TestEnv) Clone() *TestEnv
- func (env *TestEnv) CloneEnv() Env
- func (env *TestEnv) Environ() []string
- func (env *TestEnv) ExpandPath(p string) string
- func (env TestEnv) Get(key string) string
- func (env *TestEnv) GetHome() (string, error)
- func (env *TestEnv) GetJail() string
- func (env *TestEnv) GetTempDir() string
- func (env *TestEnv) GetUser() (string, error)
- func (env *TestEnv) Getwd() (string, error)
- func (env *TestEnv) Has(key string) bool
- func (env *TestEnv) Name() string
- func (env *TestEnv) ResolvePath(rel string, follow bool) (string, error)
- func (env *TestEnv) Set(key string, value string) error
- func (env *TestEnv) SetHome(rel string) error
- func (env *TestEnv) SetJail(jailPath string) error
- func (env *TestEnv) SetUser(username string) error
- func (env *TestEnv) Setwd(dir string) error
- func (env *TestEnv) Unset(key string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DumpEnv ¶
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 ¶
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 ¶
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) ExpandPath ¶
func (*OsEnv) GetHome ¶
GetHome returns the home directory reported by the OS. It delegates to os.UserHomeDir.
func (*OsEnv) GetTempDir ¶
GetTempDir returns the OS temporary directory.
func (*OsEnv) GetUser ¶
GetUser returns the current OS user username. If the Username field is empty it falls back to the user's Name field.
func (*OsEnv) SetHome ¶
SetHome sets environment values that represent the user's home directory.
On Windows it also sets USERPROFILE to satisfy common callers.
func (*OsEnv) SetUser ¶
SetUser sets environment values that represent the current user.
On Windows it also sets USERNAME in addition to USER.
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 ¶
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 ¶
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) Environ ¶
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 ¶
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 ¶
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 ¶
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) GetTempDir ¶
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) ResolvePath ¶
func (*TestEnv) Set ¶
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 ¶
SetHome sets the TestEnv home directory and updates the "HOME" key in the underlying map for callers that read via Get.
func (*TestEnv) SetUser ¶
SetUser sets the TestEnv current user and updates the "USER" key in the underlying map for callers that use Get.