Files
terminar 13801bec11 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.
2026-09-19 16:26:01 +02:00

154 lines
4.6 KiB
Go

// Package paths resolves the filesystem locations a Yetano application
// uses for configuration, data, state, cache, runtime, log, and
// executable files -- platform-independently, without parsing or knowing
// anything about configuration file contents (that is core/config's job).
//
// The central type is Resolver, obtained via For (or New):
//
// app := core.Application{Name: "yet", Kind: core.CLI}
// resolver := paths.For(app)
//
// dir, err := resolver.ConfigDir() // canonical write location
// dirs, err := resolver.ConfigSearchDirs() // all locations to search, in priority order
//
// # Canonical directory vs. search directories
//
// Every category (Config, Data, State, Cache, Runtime, Log, Bin) has two
// APIs:
//
// - <Category>Dir() returns the single, preferred location a new file
// in that category would be written to.
// - <Category>SearchDirs() returns every location that should be
// searched for an existing file, ordered from highest to lowest
// priority (a "first match wins" order).
//
// These are not always the same location. On Windows in particular, the
// canonical ConfigDir() is the roaming %APPDATA%\<app> directory, while
// ConfigSearchDirs() additionally includes %LOCALAPPDATA%\<app> (a
// machine-specific override layer, checked first) and %PROGRAMDATA%\<app>
// (a system-wide default layer, checked last). See the package README for
// the full priority tables.
//
// # No implicit filesystem mutation
//
// Resolving a path never creates a directory or file. To create one
// explicitly, use the Ensure* methods (e.g. EnsureConfigDir).
//
// # Environment variable priority
//
// Every canonical category is resolved through the same override chain,
// highest priority first:
//
// 1. an explicit option passed to New/For (e.g. WithConfigDir)
// 2. the application-specific environment variable (e.g. YET_CONFIG_HOME)
// 3. the shared YETANO_* environment variable (e.g. YETANO_CONFIG_HOME)
// 4. the platform-native mechanism (XDG_CONFIG_HOME, a Windows known
// folder, native macOS paths, ...)
// 5. the platform default
package paths
// PathKind identifies which directory category a PathInfo or SearchResult
// describes.
type PathKind int
const (
ConfigPath PathKind = iota
DataPath
StatePath
CachePath
RuntimePath
LogPath
BinPath
)
// String returns the lowercase category name used in Explain output.
func (k PathKind) String() string {
switch k {
case ConfigPath:
return "config"
case DataPath:
return "data"
case StatePath:
return "state"
case CachePath:
return "cache"
case RuntimePath:
return "runtime"
case LogPath:
return "log"
case BinPath:
return "bin"
default:
return "unknown"
}
}
// Source names where a resolved path's value came from, for
// Explain/doctor-style introspection.
type Source int
const (
// APIOverride: an explicit option (e.g. WithConfigDir) was passed to
// New/For.
APIOverride Source = iota
// AppEnvironment: the application-specific environment variable
// (e.g. YET_CONFIG_HOME) was set.
AppEnvironment
// YetanoEnvironment: the shared YETANO_* environment variable (e.g.
// YETANO_CONFIG_HOME) was set.
YetanoEnvironment
// XDGEnvironment: an XDG_* environment variable was set (Unix-family
// platforms only).
XDGEnvironment
// WindowsKnownFolder: resolved from a Windows known folder
// (%APPDATA%, %LOCALAPPDATA%, %PROGRAMDATA%, ...).
WindowsKnownFolder
// MacOSNative: resolved from a native macOS location (~/Library/...).
MacOSNative
// PlatformDefault: no override applied; the platform's built-in
// default was used (e.g. ~/.config on Linux with no XDG_CONFIG_HOME).
PlatformDefault
// WorkingDirectory: the process's current working directory.
WorkingDirectory
// SystemDirectory: a fixed system-wide location (e.g. /etc, /var/lib)
// used for SystemScope.
SystemDirectory
// PortableRoot: WithRoot redirected the resolved path under a
// portable/test root.
PortableRoot
)
// String returns the lowercase source name used in Explain output.
func (s Source) String() string {
switch s {
case APIOverride:
return "api-override"
case AppEnvironment:
return "app-environment"
case YetanoEnvironment:
return "yetano-environment"
case XDGEnvironment:
return "xdg-environment"
case WindowsKnownFolder:
return "windows-known-folder"
case MacOSNative:
return "macos-native"
case PlatformDefault:
return "platform-default"
case WorkingDirectory:
return "working-directory"
case SystemDirectory:
return "system-directory"
case PortableRoot:
return "portable-root"
default:
return "unknown"
}
}
// value is an internal helper: a resolved string plus where it came from.
type value struct {
path string
source Source
}