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:
terminar committed 2026-09-19 16:26:01 +02:00
1 parent 027b327c3d
commit 13801bec11
41 files changed
+4834 -2

No files matched your search

+163
View File
@@ -0,0 +1,163 @@
package paths
import "yetano.dev/yetano/core"
// macOS is deliberately split by ApplicationKind (spec section 12): CLI
// and Service applications use the same XDG-flavored layout as Linux
// (unix.go, via flavor()'s default case) for consistent cross-platform
// administration; only Desktop (native GUI) applications use these
// ~/Library-based conventions.
//
// Config and Data intentionally share one canonical directory (a native
// macOS app's Application Support directory commonly holds both its
// preferences-adjacent files and its bulk data side by side); State gets
// its own subdirectory so it doesn't collide with either.
func darwinAppSupportBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Application Support"
}
return unixJoin(home, "Library", "Application Support")
}
func darwinCachesBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Caches"
}
return unixJoin(home, "Library", "Caches")
}
func darwinLogsBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Logs"
}
return unixJoin(home, "Library", "Logs")
}
// darwinHomeIfNeeded returns "" (unused) for SystemScope, else the real
// home directory -- avoiding a spurious ErrNoHomeDirectory when
// SystemScope is resolved without HOME set.
func (r *Resolver) darwinHomeIfNeeded() (string, error) {
if r.app.Scope == core.SystemScope {
return "", nil
}
return r.homeDir()
}
func (r *Resolver) darwinDesktopAppDir() (value, error) {
if v, ok := r.overrideValue(ConfigPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinAppSupportBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
func (r *Resolver) darwinDesktopConfigDir() (value, error) {
return r.darwinDesktopAppDir()
}
func (r *Resolver) darwinDesktopDataDir() (value, error) {
// Config overrides (WithConfigDir) must not leak into DataDir, so
// DataDir consults its own override tier first.
if v, ok := r.overrideValue(DataPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
return r.darwinDesktopAppDir()
}
func (r *Resolver) darwinDesktopStateDir() (value, error) {
if v, ok := r.overrideValue(StatePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
app, err := r.darwinDesktopAppDir()
if err != nil {
return value{}, err
}
return value{path: unixJoin(app.path, "State"), source: app.source}, nil
}
func (r *Resolver) darwinDesktopCacheDir() (value, error) {
if v, ok := r.overrideValue(CachePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinCachesBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
func (r *Resolver) darwinDesktopLogDir() (value, error) {
if v, ok := r.overrideValue(LogPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinLogsBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
func (r *Resolver) darwinDesktopRuntimeDir() (value, error) {
if v, ok := r.overrideValue(RuntimePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
app, err := r.darwinDesktopAppDir()
if err != nil {
return value{}, err
}
return value{path: unixJoin(app.path, "Runtime"), source: app.source}, nil
}
// darwinDesktopBinDir has no native GUI-app convention; it reuses the
// shared Unix bin directory so a Desktop app that also ships a CLI shim
// still has a sensible, documented answer.
func (r *Resolver) darwinDesktopBinDir() (value, error) {
return r.binDirUnix()
}
func (r *Resolver) darwinDesktopConfigLayers() ([]layer, error) {
return r.darwinDesktopLayers(ConfigPath, darwinAppSupportBase)
}
func (r *Resolver) darwinDesktopDataLayers() ([]layer, error) {
return r.darwinDesktopLayers(DataPath, darwinAppSupportBase)
}
func (r *Resolver) darwinDesktopLayers(kind PathKind, base func(core.Scope, string) string) ([]layer, error) {
if v, ok := r.overrideValue(kind); ok {
return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil
}
home, err := r.homeDir()
if err != nil {
return nil, err
}
user := []layer{{
path: r.applyRoot(unixJoin(base(core.UserScope, home), r.app.Name)),
source: MacOSNative,
priority: PriorityUser,
canonical: true,
}}
system := []layer{{
path: r.applyRoot(unixJoin(base(core.SystemScope, ""), r.app.Name)),
source: SystemDirectory,
priority: PrioritySystem,
canonical: r.app.Scope == core.SystemScope,
}}
return composeSearch(user, system, r.app.Scope), nil
}