51 lines
1.8 KiB
Go
51 lines
1.8 KiB
Go
package paths
|
|||
|
|
|
||
|
|
import "os"
|
||
|
|
|
||
|
|
// Environment provides environment-variable lookups to a Resolver. The
|
||
|
|
// default (osEnvironment) reads the real process environment; tests (and
|
||
|
|
// callers simulating another platform) should inject a MapEnvironment
|
||
|
|
// instead via WithEnvironment, per AGENTS.md's "no unchecked os.Getenv"
|
||
|
|
// rule for anything that needs deterministic tests.
|
||
|
|
type Environment interface {
|
||
|
|
// Lookup returns the environment variable's value and whether it was
|
||
|
|
// set at all (mirrors os.LookupEnv: unset and set-to-empty are
|
||
|
|
// distinguishable).
|
||
|
|
Lookup(key string) (value string, ok bool)
|
||
|
|
}
|
||
|
|
|
||
|
|
// MapEnvironment is an Environment backed by a fixed map, for tests and
|
||
|
|
// for simulating a platform's environment (e.g. Windows variables while
|
||
|
|
// running on Linux/macOS).
|
||
|
|
type MapEnvironment map[string]string
|
||
|
|
|
||
|
|
// Lookup implements Environment.
|
||
|
|
func (m MapEnvironment) Lookup(key string) (string, bool) {
|
||
|
|
v, ok := m[key]
|
||
|
|
return v, ok
|
||
|
|
}
|
||
|
|
|
||
|
|
// osEnvironment is the default Environment: the real process environment.
|
||
|
|
type osEnvironment struct{}
|
||
|
|
|
||
|
|
func (osEnvironment) Lookup(key string) (string, bool) {
|
||
|
|
return os.LookupEnv(key)
|
||
|
|
}
|
||
|
|
|
||
|
|
// OSEnvironment is the real process environment, exported so other
|
||
|
|
// yetano.dev/yetano/core packages (e.g. core/config) can share the exact
|
||
|
|
// same default Environment paths.New uses, without reaching for
|
||
|
|
// os.Getenv themselves.
|
||
|
|
var OSEnvironment Environment = osEnvironment{}
|
||
|
|
|
||
|
|
// lookup is a small convenience over Environment.Lookup that treats a
|
||
|
|
// set-but-empty variable as "not set", matching how shells and most CLI
|
||
|
|
// tools treat empty environment overrides (an empty YET_CONFIG_HOME should
|
||
|
|
// not silently resolve to the filesystem root/current directory).
|
||
|
|
func lookup(env Environment, key string) (string, bool) {
|
||
|
|
v, ok := env.Lookup(key)
|
||
|
|
if !ok || v == "" {
|
||
|
|
return "", false
|
||
|
|
}
|
||
|
|
return v, true
|
||
|
|
}
|