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
+114
@@ -0,0 +1,114 @@
|
||||
package paths
|
||||
|
||||
import "yetano.dev/yetano/core"
|
||||
|
||||
// layer is one directory in a search/priority hierarchy (see
|
||||
// AGENTS.md-style docs in paths.go: "canonical path vs. search paths").
|
||||
// Search order is descending priority; merge order (used by core/config)
|
||||
// is the reverse.
|
||||
type layer struct {
|
||||
path string
|
||||
source Source
|
||||
priority int
|
||||
canonical bool
|
||||
}
|
||||
|
||||
// Priority constants shared with core/config, which adds the working
|
||||
// directory layer (always PriorityWorkingDirectory) on top of whatever
|
||||
// paths.Resolver reports for the user/system layers.
|
||||
const (
|
||||
PriorityWorkingDirectory = 100
|
||||
PriorityUserLocal = 90 // Windows-only machine-local override layer
|
||||
PriorityUser = 80
|
||||
PrioritySystem = 10
|
||||
)
|
||||
|
||||
// composeSearch applies the one Scope rule used across every search-dir
|
||||
// category (Config, Data, Executable): SystemScope resolves only the
|
||||
// system tier (a system-scope process, e.g. a system service, has no
|
||||
// meaningful per-user home directory); UserScope resolves the user
|
||||
// tier(s) plus the system tier as a lower-priority fallback, so a
|
||||
// system-wide default can still be found -- and, per spec section 36,
|
||||
// a user installation overrides a system one.
|
||||
func composeSearch(user, system []layer, scope core.Scope) []layer {
|
||||
if scope == core.SystemScope {
|
||||
return system
|
||||
}
|
||||
out := make([]layer, 0, len(user)+len(system))
|
||||
out = append(out, user...)
|
||||
out = append(out, system...)
|
||||
return out
|
||||
}
|
||||
|
||||
// envSuffix maps each category to the "_HOME" suffix used to build its
|
||||
// YETANO_*/<PREFIX>_* environment variable names (e.g. ConfigPath ->
|
||||
// "YETANO_CONFIG_HOME" / "YET_CONFIG_HOME").
|
||||
var envSuffix = map[PathKind]string{
|
||||
ConfigPath: "CONFIG_HOME",
|
||||
DataPath: "DATA_HOME",
|
||||
StatePath: "STATE_HOME",
|
||||
CachePath: "CACHE_HOME",
|
||||
RuntimePath: "RUNTIME_HOME",
|
||||
LogPath: "LOG_HOME",
|
||||
BinPath: "BIN_HOME",
|
||||
}
|
||||
|
||||
// appendsAppName reports whether the YetanoEnvironment/platform-native/
|
||||
// PlatformDefault tiers for kind are treated as a *base* directory that
|
||||
// the application name is joined onto (true, e.g. XDG_CONFIG_HOME) or as
|
||||
// an already-final, shared directory used as-is (false; only BinPath: a
|
||||
// bin directory is conventionally shared across every application on the
|
||||
// machine, like ~/.local/bin, not namespaced per app).
|
||||
//
|
||||
// APIOverride and AppEnvironment are always used as-is for every
|
||||
// category, regardless of appendsAppName -- an explicit or app-specific
|
||||
// override already names an exact directory; it would be surprising for
|
||||
// "YET_CONFIG_HOME=/foo" to actually mean "/foo/yet".
|
||||
func appendsAppName(kind PathKind) bool {
|
||||
return kind != BinPath
|
||||
}
|
||||
|
||||
// overrideValue resolves tiers 1-3 of the override chain (API override,
|
||||
// app-specific environment, shared YETANO_* environment) for kind. ok is
|
||||
// false if none of these tiers is set, meaning the caller must fall back
|
||||
// to its own platform-native tier 4 / default tier 5.
|
||||
func (r *Resolver) overrideValue(kind PathKind) (v value, ok bool) {
|
||||
if explicit, set := r.overrides[kind]; set {
|
||||
return value{path: explicit, source: APIOverride}, true
|
||||
}
|
||||
suffix := envSuffix[kind]
|
||||
if r.envPrefix != "" {
|
||||
if v, set := lookup(r.env, r.envPrefix+"_"+suffix); set {
|
||||
return value{path: v, source: AppEnvironment}, true
|
||||
}
|
||||
}
|
||||
if v, set := lookup(r.env, "YETANO_"+suffix); set {
|
||||
base := v
|
||||
if appendsAppName(kind) {
|
||||
base = r.join(v, r.app.Name)
|
||||
}
|
||||
return value{path: base, source: YetanoEnvironment}, true
|
||||
}
|
||||
return value{}, false
|
||||
}
|
||||
|
||||
// join joins path elements using the address family of r.platform
|
||||
// (windows: backslash; everything else: forward slash) -- see join.go.
|
||||
func (r *Resolver) join(elems ...string) string {
|
||||
if r.isWindows() {
|
||||
return windowsJoin(elems...)
|
||||
}
|
||||
return unixJoin(elems...)
|
||||
}
|
||||
|
||||
func (r *Resolver) isWindows() bool { return r.platform == "windows" }
|
||||
func (r *Resolver) isDarwin() bool { return r.platform == "darwin" }
|
||||
|
||||
// applyRoot rebases an absolute path under the portable root (WithRoot),
|
||||
// if one is set, using the same address family as r.platform.
|
||||
func (r *Resolver) applyRoot(p string) string {
|
||||
if r.isWindows() {
|
||||
return applyRootWindows(r.root, p)
|
||||
}
|
||||
return applyRootUnix(r.root, p)
|
||||
}
|
||||
Reference in new issue
Block a user