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,136 @@
|
||||
package paths
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yetano.dev/yetano/core"
|
||||
)
|
||||
|
||||
// PathInfo is one resolved category in an Explanation (spec section 42).
|
||||
type PathInfo struct {
|
||||
Kind PathKind
|
||||
Path string
|
||||
Source Source
|
||||
Scope core.Scope
|
||||
Exists bool
|
||||
Writable bool
|
||||
}
|
||||
|
||||
// Explanation is the full, structured report produced by Explain: every
|
||||
// canonical category this Resolver computed, plus each category's search
|
||||
// directories. Resolving it never mutates the filesystem (spec section
|
||||
// 47) beyond the read-only os.Stat calls needed to fill Exists/Writable.
|
||||
type Explanation struct {
|
||||
Application core.Application
|
||||
Platform string
|
||||
Paths []PathInfo
|
||||
Search map[PathKind][]SearchResult
|
||||
}
|
||||
|
||||
// writable reports whether dir is writable by attempting to stat it and
|
||||
// checking a conservative permission bit; a missing directory is
|
||||
// reported as not writable rather than attempting to create one
|
||||
// (Explain must never mutate the filesystem).
|
||||
func writable(dir string) bool {
|
||||
info, err := statInfo(dir)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return info.writable
|
||||
}
|
||||
|
||||
// Explain resolves every category and returns a structured report
|
||||
// suitable for a `doctor`-style command. It never creates directories.
|
||||
func (r *Resolver) Explain() (Explanation, error) {
|
||||
ex := Explanation{
|
||||
Application: r.app,
|
||||
Platform: r.platform,
|
||||
Search: map[PathKind][]SearchResult{},
|
||||
}
|
||||
|
||||
kinds := []struct {
|
||||
kind PathKind
|
||||
fn func() (value, error)
|
||||
}{
|
||||
{ConfigPath, r.configDirValue},
|
||||
{DataPath, r.dataDirValue},
|
||||
{StatePath, r.stateDirValue},
|
||||
{CachePath, r.cacheDirValue},
|
||||
{RuntimePath, r.runtimeDirValue},
|
||||
{LogPath, r.logDirValue},
|
||||
{BinPath, r.binDirValue},
|
||||
}
|
||||
for _, k := range kinds {
|
||||
v, err := k.fn()
|
||||
if err != nil {
|
||||
if k.kind == RuntimePath && err == ErrRuntimeDirUnavailable {
|
||||
ex.Paths = append(ex.Paths, PathInfo{Kind: k.kind, Scope: r.app.Scope})
|
||||
continue
|
||||
}
|
||||
return Explanation{}, fmt.Errorf("explain %s: %w", k.kind, err)
|
||||
}
|
||||
ex.Paths = append(ex.Paths, PathInfo{
|
||||
Kind: k.kind,
|
||||
Path: v.path,
|
||||
Source: v.source,
|
||||
Scope: r.app.Scope,
|
||||
Exists: statExists(v.path),
|
||||
Writable: writable(v.path),
|
||||
})
|
||||
}
|
||||
|
||||
searchKinds := []struct {
|
||||
kind PathKind
|
||||
fn func() ([]SearchResult, error)
|
||||
}{
|
||||
{ConfigPath, r.ConfigSearchDirs},
|
||||
{DataPath, r.DataSearchDirs},
|
||||
{BinPath, r.ExecutableSearchDirs},
|
||||
}
|
||||
for _, k := range searchKinds {
|
||||
results, err := k.fn()
|
||||
if err != nil {
|
||||
return Explanation{}, fmt.Errorf("explain %s search dirs: %w", k.kind, err)
|
||||
}
|
||||
ex.Search[k.kind] = results
|
||||
}
|
||||
|
||||
return ex, nil
|
||||
}
|
||||
|
||||
// String renders a human-readable report similar to spec sections 44/45.
|
||||
func (e Explanation) String() string {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "Application: %s\n", e.Application.Name)
|
||||
fmt.Fprintf(&b, "Kind: %s\n", e.Application.Kind)
|
||||
fmt.Fprintf(&b, "Scope: %s\n", e.Application.Scope)
|
||||
fmt.Fprintf(&b, "Platform: %s\n\n", e.Platform)
|
||||
|
||||
for _, p := range e.Paths {
|
||||
if p.Path == "" {
|
||||
fmt.Fprintf(&b, "%s:\n unavailable\n\n", p.Kind)
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(&b, "%s:\n %s\n source: %s\n exists: %t\n writable: %t\n\n",
|
||||
p.Kind, p.Path, p.Source, p.Exists, p.Writable)
|
||||
}
|
||||
|
||||
for _, kind := range []PathKind{ConfigPath, DataPath, BinPath} {
|
||||
results, ok := e.Search[kind]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(&b, "%s search:\n", kind)
|
||||
for _, res := range results {
|
||||
marker := " "
|
||||
if res.Selected {
|
||||
marker = "*"
|
||||
}
|
||||
fmt.Fprintf(&b, " %s %s (source: %s, exists: %t)\n", marker, res.Path, res.Source, res.Exists)
|
||||
}
|
||||
b.WriteString("\n")
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
Reference in new issue
Block a user