Files
core/solutions/engine/plan.go
T
terminarandClaude Opus 5.5 2481a0dd27 solutions: Solutions Guide engine (catalog, rules, wizard state, native config, knowledge index, CLI)
New nested module yetano.dev/yetano/core/solutions: loads the catalog
in assets/solutions on top of the Big Picture, evaluates questions and
conditions, resolves packages/modules/capabilities, generates native
configuration from typed fragments with per-value origins, and builds
architecture graph, plan, lock file and handbook. Knowledge index with
yetano: references, search and lint (incl. manual sections). CLI with
yet's conventions (solution, task, knowledge, open), standalone as
yetano-solutions. No execution, no network.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 08:42:58 +01:00

139 lines
3.7 KiB
Go

package engine
import (
"fmt"
"sort"
"strings"
"yetano.dev/yetano/core/solutions/catalog"
)
// phases in plan order.
var phaseOrder = map[string]int{"install": 0, "configure": 1, "data": 2, "dns": 3, "start": 4, "verify": 5}
func (ev *evaluation) steps() {
vars := ev.vars()
for _, s := range ev.s.Spec.Steps {
if len(s.When) > 0 && !s.When.Holds(ev.env) {
continue
}
if s.Package != "" && !ev.pkgSelected(s.Package) {
continue
}
if s.Package != "" && s.Module != "" && s.ForEach != "$modules" && !ev.moduleEnabled(s.Package, s.Module) {
continue
}
items := []any{nil}
switch {
case s.ForEach == "$modules":
// every enabled module of the step's package that is a program
items = nil
if cp := ev.e.Cat.Packages[s.Package]; cp != nil {
for _, m := range cp.Spec.Modules {
if m.Program != "" && ev.moduleEnabled(s.Package, m.ID) && (s.Module == "" || m.Kind == s.Module) {
items = append(items, m.Program)
}
}
}
case s.ForEach != "":
items = listOf(ev.answers[s.ForEach])
}
for i, it := range items {
v := vars
if s.ForEach != "" {
v = map[string]any{}
for k, x := range vars {
v[k] = x
}
v["item"] = it
}
run, err := expand(s.Run, v)
if err != nil {
ev.msg("error", "step", fmt.Sprintf("step %s: %v", s.ID, err), "step:"+s.ID)
continue
}
title, _ := expand(s.Title, v)
note, _ := expand(s.Note, v)
id := s.ID
if s.ForEach != "" {
id = fmt.Sprintf("%s-%d", s.ID, i+1)
}
phase := s.Phase
if phase == "" {
phase = "configure"
}
ev.st.Steps = append(ev.st.Steps, PlannedStep{
ID: id, Title: title, Phase: phase, Package: s.Package, Module: s.Module,
Run: run, Note: note, Mutating: s.Mutating, Reversible: s.Reversible, References: s.References,
})
}
}
sort.SliceStable(ev.st.Steps, func(i, j int) bool {
return phaseOrder[ev.st.Steps[i].Phase] < phaseOrder[ev.st.Steps[j].Phase]
})
}
// expand fills placeholders into a text (steps, notes). Values are
// inserted as text; steps are shown to the operator, never run.
func expand(s string, vars map[string]any) (string, error) {
if !strings.Contains(s, "${") {
return s, nil
}
var firstErr error
used := map[string]bool{}
out := placeholderRE.ReplaceAllStringFunc(s, func(m string) string {
if m == "$${" {
return "${"
}
if strings.HasPrefix(m, "${item") {
if it, ok := vars["item"]; ok {
return catalog.Scalar(it)
}
}
v, err := lookup(m, vars, used)
if err != nil && firstErr == nil {
firstErr = err
}
return shellWord(text(v))
})
return out, firstErr
}
// shellWord quotes a value for display in a command line if needed.
func shellWord(s string) string {
if s == "" {
return "''"
}
for _, r := range s {
if !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || strings.ContainsRune("@%+=:,./_-", r)) {
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
}
}
return s
}
func (ev *evaluation) checks() {
vars := ev.vars()
for _, c := range ev.s.Spec.Requirements {
r := CheckResult{ID: c.ID, Title: c.Title, Kind: c.Kind, Severity: firstNonEmpty(c.Severity, "error"),
Local: c.Local, Hint: c.Hint, References: c.References, Params: map[string]string{}}
if len(c.When) > 0 && !c.When.Holds(ev.env) {
r.Status = CheckNotApplicable
ev.st.Checks = append(ev.st.Checks, r)
continue
}
for k, v := range c.Params {
x, err := expand(v, vars)
if err != nil {
x = v
}
r.Params[k] = strings.Trim(x, "'")
}
r.Run, _ = expand(c.Run, vars)
// The engine itself never inspects a system: every check starts
// as NOT_CHECKED; a doctor runner fills in results (DR-03).
r.Status = CheckNotChecked
ev.st.Checks = append(ev.st.Checks, r)
}
}