Files
core/config/explain.go
T
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

75 lines
1.9 KiB
Go

package config
import (
"fmt"
"strings"
"yetano.dev/yetano/core"
"yetano.dev/yetano/core/paths"
)
// Explanation is the full, structured report for a doctor-style command
// (spec sections 42/44/45): every configuration layer this Resolver
// considered, plus the paths.Explanation for everything else (Data,
// State, Cache, Runtime, Log, Bin). Building it never mutates the
// filesystem.
type Explanation struct {
Application core.Application
Filename string
Layers []Layer
Selected string // "", if no layer's file exists
Paths paths.Explanation
}
// Explain resolves every configuration layer and every core/paths
// category and returns a combined, structured report.
func (r *Resolver) Explain() (Explanation, error) {
layers, err := r.layers()
if err != nil {
return Explanation{}, err
}
pathsExplanation, err := r.paths.Explain()
if err != nil {
return Explanation{}, err
}
ex := Explanation{
Application: r.app,
Filename: r.filename,
Layers: layers,
Paths: pathsExplanation,
}
for _, l := range layers {
if l.Selected {
ex.Selected = l.Path
}
}
return ex, nil
}
// String renders a human-readable report similar to spec section 44.
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\n", e.Application.Scope)
b.WriteString("Configuration layers:\n")
for _, l := range e.Layers {
found := "no"
if l.Exists {
found = "yes"
}
fmt.Fprintf(&b, " %s\n priority: %d\n source: %s\n found: %s\n", l.Path, l.Priority, l.Source, found)
}
b.WriteString("\n")
if e.Selected != "" {
fmt.Fprintf(&b, "Selected:\n %s\n\n", e.Selected)
} else {
b.WriteString("Selected:\n (none found)\n\n")
}
b.WriteString(e.Paths.String())
return b.String()
}