Add platform-independent path/config resolution library
Implements core/paths (Config/Data/State/Cache/Runtime/Log/Bin
directory resolution with canonical-vs-search-dir separation, XDG on
Unix, native APPDATA/LOCALAPPDATA/PROGRAMDATA layering on Windows,
~/Library on macOS Desktop, injectable environment/platform for
deterministic cross-platform tests) and core/config (file search,
first-match resolution, layered YAML merging, safe ${VAR} expansion,
Explain/doctor introspection) on top of the shared Application/
Kind/Scope model in the core package.
This commit is contained in:
1 parent
027b327c3d
commit
13801bec11
41 files changed
+4834
-2
No files matched your search
@@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user