Files
core/solutions/engine/handbook.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

279 lines
8.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package engine
import (
"fmt"
"sort"
"strings"
"yetano.dev/yetano/core/solutions/catalog"
)
// HandbookPage is one Markdown page of a solution handbook (HB-01).
type HandbookPage struct {
Path string
Content []byte
}
// Handbook composes the handbook of a state from the solution, the
// decisions, the generated files and existing manual text (via mr). It
// covers only what was chosen (HB-02) and never prints secret values
// (HB-04): secret answers are references and are shown as such.
func (e *Engine) Handbook(st *State, mr ManualReader) []HandbookPage {
s := e.Cat.Lookup(st.Solution.ID)
link := func(ref string) string {
if mr != nil {
if l := mr.Link(ref); l != "" {
return l
}
}
return "`" + ref + "`"
}
excerpt := func(b *strings.Builder, ref string) {
if mr == nil {
fmt.Fprintf(b, "- %s\n", link(ref))
return
}
title, md, ok := mr.Excerpt(ref, 40)
if !ok {
fmt.Fprintf(b, "- %s\n", link(ref))
return
}
fmt.Fprintf(b, "> From the manual: **%s** (%s)\n>\n", title, link(ref))
for _, line := range strings.Split(strings.TrimRight(md, "\n"), "\n") {
fmt.Fprintf(b, "> %s\n", line)
}
b.WriteString("\n")
}
var pages []HandbookPage
page := func(name, title string, body func(b *strings.Builder)) {
var b strings.Builder
fmt.Fprintf(&b, "# %s\n\n", title)
body(&b)
pages = append(pages, HandbookPage{Path: "handbook/" + name, Content: []byte(b.String())})
}
selected := []PackageState{}
for _, p := range st.Packages {
if p.Selected {
selected = append(selected, p)
}
}
hasCap := func(prefix string) bool {
for _, c := range st.Capabilities {
if strings.HasPrefix(c.ID, prefix) && provided(c.Status) {
return true
}
}
return false
}
secretQ := map[string]bool{}
for _, q := range s.Spec.Questions {
if q.Type == "secret" {
secretQ[q.ID] = true
}
}
page("index.md", "Handbook: "+firstNonEmpty(st.Instance, st.Solution.Title), func(b *strings.Builder) {
fmt.Fprintf(b, "%s\n\n", st.Solution.Summary)
fmt.Fprintf(b, "Solution `%s` %s, status **%s**.\n\n", st.Solution.ID, st.Solution.Version, st.Solution.Status)
b.WriteString("1. [Your architecture](architecture.md)\n2. [Installation and configuration](installation.md)\n3. [Configuration reference](configuration.md)\n4. [Security](security.md)\n5. [Operations and maintenance](operations.md)\n")
if hasCap("backup.") {
b.WriteString("6. [Backup and restore](backup-restore.md)\n7. [Troubleshooting](troubleshooting.md)\n")
} else {
b.WriteString("6. [Troubleshooting](troubleshooting.md)\n")
}
b.WriteString("\n## Your decisions\n\n| Question | Answer | From |\n|---|---|---|\n")
for _, q := range st.Questions {
if !q.Visible || q.Value == nil {
continue
}
val := answerText(q)
if secretQ[q.ID] {
val = "(secret reference)"
}
fmt.Fprintf(b, "| %s | %s | %s |\n", q.Title, val, q.Origin)
}
if len(st.Messages) > 0 {
b.WriteString("\n## Notes and warnings\n\n")
for _, m := range st.Messages {
fmt.Fprintf(b, "- **%s**: %s\n", m.Severity, m.Text)
}
}
})
page("architecture.md", "Your architecture", func(b *strings.Builder) {
b.WriteString("```text\n")
b.WriteString(GraphText(st.Graph))
b.WriteString("```\n\n## Packages\n\n")
for _, p := range selected {
fmt.Fprintf(b, "### %s\n\n%s\n\n", p.ID, p.Summary)
if p.Profile != "" {
fmt.Fprintf(b, "Profile: `%s`. ", p.Profile)
}
fmt.Fprintf(b, "Why: %s.\n\n", strings.Join(p.Reasons, "; "))
var on []string
for _, m := range p.Modules {
if m.Enabled {
on = append(on, fmt.Sprintf("`%s`", firstNonEmpty(m.Program, m.ID)))
}
}
if len(on) > 0 {
fmt.Fprintf(b, "Programs used: %s.\n\n", strings.Join(on, ", "))
}
}
b.WriteString("## Capabilities\n\n| Capability | Status | Provided by |\n|---|---|---|\n")
for _, c := range st.Capabilities {
if c.Status == StExcluded {
continue
}
fmt.Fprintf(b, "| %s | %s | %s |\n", c.Title, c.Status, firstNonEmpty(c.Provider, "–"))
}
if st.Solution.BigPicture != "" {
fmt.Fprintf(b, "\nIn the Big Picture: %s\n", link("yetano:bigpicture/"+st.Solution.BigPicture))
}
})
page("installation.md", "Installation and configuration", func(b *strings.Builder) {
b.WriteString("The guide has not changed any system. These are the steps, in order;\n" +
"`deployment-plan.yaml` has the same list in machine-readable form.\n\n")
phase := ""
n := 0
for _, sp := range st.Steps {
if sp.Phase != phase {
phase = sp.Phase
fmt.Fprintf(b, "## %s\n\n", strings.ToUpper(phase[:1])+phase[1:])
}
n++
fmt.Fprintf(b, "%d. %s\n", n, sp.Title)
if sp.Run != "" {
fmt.Fprintf(b, "\n ```sh\n %s\n ```\n", sp.Run)
}
if sp.Note != "" {
fmt.Fprintf(b, "\n %s\n", sp.Note)
}
if sp.Reversible != nil && !*sp.Reversible {
b.WriteString("\n **Cannot be undone automatically.**\n")
}
b.WriteString("\n")
}
})
page("configuration.md", "Configuration reference", func(b *strings.Builder) {
for _, f := range st.Files {
fmt.Fprintf(b, "## `%s`\n\n```yaml\n%s```\n\n", f.Path, f.Content)
keys := make([]string, 0, len(f.Origins))
for k := range f.Origins {
keys = append(keys, k)
}
sort.Strings(keys)
b.WriteString("| Setting | Comes from | Uses answers |\n|---|---|---|\n")
for _, k := range keys {
o := f.Origins[k]
fmt.Fprintf(b, "| `%s` | %s | %s |\n", k, o.Source, strings.Join(o.Params, ", "))
}
b.WriteString("\n")
}
for _, p := range selected {
if cp := e.Cat.Packages[p.ID]; cp != nil {
for _, m := range cp.Spec.Modules {
if ms := findModule(p, m.ID); ms != nil && ms.Enabled && m.Manual != "" {
fmt.Fprintf(b, "- Full option list of %s: %s\n", firstNonEmpty(m.Program, m.ID), link("yetano:manual/"+m.Manual+"/configuration"))
}
}
}
}
})
page("security.md", "Security", func(b *strings.Builder) {
b.WriteString("Secrets are never written into these files: secret answers are\nreferences (`secret://…` resolved by yaseva, `env:`, `file:`).\n\n")
for _, c := range st.Checks {
if c.Status == CheckNotApplicable {
continue
}
if c.Kind == "tls" || c.Kind == "dns" || strings.Contains(c.ID, "tls") || strings.Contains(c.ID, "relay") {
fmt.Fprintf(b, "- **%s** (%s): %s", c.Title, c.Severity, c.Hint)
if c.Run != "" {
fmt.Fprintf(b, " Check: `%s`", c.Run)
}
b.WriteString("\n")
}
}
for _, r := range s.Spec.References.Manuals {
if strings.Contains(r, "dkim") || strings.Contains(r, "tls") || strings.Contains(r, "security") {
b.WriteString("\n")
excerpt(b, r)
}
}
})
page("operations.md", "Operations and maintenance", func(b *strings.Builder) {
for _, op := range s.Spec.Operations {
fmt.Fprintf(b, "- %s\n", op)
}
b.WriteString("\n## Health checks\n\n")
for _, p := range selected {
if cp := e.Cat.Packages[p.ID]; cp != nil {
for _, m := range cp.Spec.Modules {
if ms := findModule(p, m.ID); ms != nil && ms.Enabled {
for _, d := range m.Doctor {
fmt.Fprintf(b, "- `%s`\n", d)
}
}
}
}
}
if len(st.Related) > 0 {
b.WriteString("\n## Related tasks and solutions\n\n")
for _, r := range st.Related {
kind := "solution"
if e.Cat.Tasks[r] != nil {
kind = "task"
}
fmt.Fprintf(b, "- `yet %s show %s`\n", kind, r)
}
}
})
if hasCap("backup.") {
page("backup-restore.md", "Backup and restore", func(b *strings.Builder) {
for _, c := range st.Capabilities {
if strings.HasPrefix(c.ID, "backup.") && provided(c.Status) {
fmt.Fprintf(b, "- %s: %s\n", c.Title, c.Provider)
}
}
})
}
page("troubleshooting.md", "Troubleshooting", func(b *strings.Builder) {
b.WriteString("Run the health checks from [operations](operations.md) first: they name\nthe failing area and give a hint.\n\n## Manuals\n\n")
refs := append([]string{}, st.References...)
for _, r := range refs {
if strings.HasPrefix(r, "yetano:manual/") {
fmt.Fprintf(b, "- %s\n", link(r))
}
}
})
return pages
}
func findModule(p PackageState, id string) *ModuleState {
for i := range p.Modules {
if p.Modules[i].ID == id {
return &p.Modules[i]
}
}
return nil
}
func answerText(q QuestionState) string {
if l, ok := q.Value.([]any); ok {
var parts []string
for _, x := range l {
parts = append(parts, optTitle(q, catalog.Scalar(x)))
}
return strings.Join(parts, ", ")
}
return optTitle(q, catalog.Scalar(q.Value))
}
func optTitle(q QuestionState, v string) string {
for _, o := range q.Options {
if o.ID == v {
return o.Title
}
}
return v
}