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>
This commit is contained in:
1 parent
586e76889c
commit
2481a0dd27
48 files changed
+10304
No files matched your search
@@ -0,0 +1,190 @@
|
||||
// Package engine is the Solutions Engine: it evaluates a solution's
|
||||
// questions and rules against a user's answers, resolves packages,
|
||||
// modules, capabilities and providers, generates native configuration
|
||||
// with the origin of every value, and builds the architecture graph,
|
||||
// the plan, the lock file and the handbook.
|
||||
//
|
||||
// The engine is pure: it reads a loaded catalog and values, and returns
|
||||
// values. It never touches the network, never runs a command and writes
|
||||
// files only in Write, into the directory it is given.
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"yetano.dev/yetano/core/solutions/catalog"
|
||||
)
|
||||
|
||||
// Instance is a user's answers for one solution: a draft wizard session
|
||||
// (kind WizardSession) or a saved solution instance (SolutionInstance).
|
||||
// It never contains plain-text secrets (secret answers are references).
|
||||
type Instance struct {
|
||||
APIVersion string `yaml:"apiVersion" json:"apiVersion"`
|
||||
Kind string `yaml:"kind" json:"kind"`
|
||||
Metadata InstanceMeta `yaml:"metadata" json:"metadata"`
|
||||
Spec InstanceSpec `yaml:"spec" json:"spec"`
|
||||
}
|
||||
|
||||
// InstanceMeta names an instance.
|
||||
type InstanceMeta struct {
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Description string `yaml:"description,omitempty" json:"description,omitempty"`
|
||||
Created string `yaml:"created,omitempty" json:"created,omitempty"`
|
||||
Updated string `yaml:"updated,omitempty" json:"updated,omitempty"`
|
||||
Favorite bool `yaml:"favorite,omitempty" json:"favorite,omitempty"`
|
||||
Archived bool `yaml:"archived,omitempty" json:"archived,omitempty"`
|
||||
}
|
||||
|
||||
// InstanceSpec holds the decisions.
|
||||
type InstanceSpec struct {
|
||||
Solution SolutionPin `yaml:"solution" json:"solution"`
|
||||
Mode catalog.Level `yaml:"mode,omitempty" json:"mode,omitempty"`
|
||||
Preset string `yaml:"preset,omitempty" json:"preset,omitempty"`
|
||||
Locale string `yaml:"locale,omitempty" json:"locale,omitempty"`
|
||||
Platform string `yaml:"platform,omitempty" json:"platform,omitempty"`
|
||||
Target string `yaml:"target,omitempty" json:"target,omitempty"`
|
||||
Parameters map[string]any `yaml:"parameters" json:"parameters"`
|
||||
Overrides []Override `yaml:"overrides,omitempty" json:"overrides,omitempty"`
|
||||
// Cursor is the question a wizard session was at (sessions only).
|
||||
Cursor string `yaml:"cursor,omitempty" json:"cursor,omitempty"`
|
||||
}
|
||||
|
||||
// SolutionPin names the solution and the version the answers were made for.
|
||||
type SolutionPin struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Version string `yaml:"version,omitempty" json:"version,omitempty"`
|
||||
}
|
||||
|
||||
// Override sets one value in a generated file directly (layer 6 of
|
||||
// ARCHITECTURE.md): path is dot-separated map keys.
|
||||
type Override struct {
|
||||
File string `yaml:"file" json:"file"`
|
||||
Path string `yaml:"path" json:"path"`
|
||||
Value any `yaml:"value" json:"value"`
|
||||
}
|
||||
|
||||
// Preset is a reusable set of answers (PRE-01). Instance-specific and
|
||||
// secret questions are never taken from a preset.
|
||||
type Preset struct {
|
||||
APIVersion string `yaml:"apiVersion" json:"apiVersion"`
|
||||
Kind string `yaml:"kind" json:"kind"`
|
||||
Metadata InstanceMeta `yaml:"metadata" json:"metadata"`
|
||||
Spec PresetSpec `yaml:"spec" json:"spec"`
|
||||
}
|
||||
|
||||
// PresetSpec holds a preset's answers.
|
||||
type PresetSpec struct {
|
||||
Solution string `yaml:"solution,omitempty" json:"solution,omitempty"`
|
||||
Version string `yaml:"version,omitempty" json:"version,omitempty"`
|
||||
Mode catalog.Level `yaml:"mode,omitempty" json:"mode,omitempty"`
|
||||
Parameters map[string]any `yaml:"parameters" json:"parameters"`
|
||||
}
|
||||
|
||||
// NewInstance starts a session for a solution.
|
||||
func NewInstance(name string, s *catalog.Solution, mode catalog.Level) *Instance {
|
||||
if mode == "" {
|
||||
mode = catalog.LevelQuick
|
||||
}
|
||||
return &Instance{
|
||||
APIVersion: catalog.APIVersion, Kind: catalog.KindSession,
|
||||
Metadata: InstanceMeta{Name: name},
|
||||
Spec: InstanceSpec{
|
||||
Solution: SolutionPin{ID: s.Meta.ID, Version: s.Meta.Version},
|
||||
Mode: mode,
|
||||
Parameters: map[string]any{},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// MaxInstanceSize bounds instance and preset files.
|
||||
const MaxInstanceSize = 1 << 20
|
||||
|
||||
// ReadInstance decodes an instance or session (strict).
|
||||
func ReadInstance(r io.Reader) (*Instance, error) {
|
||||
var in Instance
|
||||
if err := strict(r, &in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if in.APIVersion != catalog.APIVersion {
|
||||
return nil, fmt.Errorf("apiVersion %q, want %q", in.APIVersion, catalog.APIVersion)
|
||||
}
|
||||
if in.Kind != catalog.KindInstance && in.Kind != catalog.KindSession {
|
||||
return nil, fmt.Errorf("kind %q, want %s or %s", in.Kind, catalog.KindInstance, catalog.KindSession)
|
||||
}
|
||||
if in.Spec.Parameters == nil {
|
||||
in.Spec.Parameters = map[string]any{}
|
||||
}
|
||||
return &in, nil
|
||||
}
|
||||
|
||||
// ReadPreset decodes a preset (strict).
|
||||
func ReadPreset(r io.Reader) (*Preset, error) {
|
||||
var p Preset
|
||||
if err := strict(r, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if p.APIVersion != catalog.APIVersion || p.Kind != catalog.KindPreset {
|
||||
return nil, fmt.Errorf("not a %s %s", catalog.APIVersion, catalog.KindPreset)
|
||||
}
|
||||
if p.Spec.Parameters == nil {
|
||||
p.Spec.Parameters = map[string]any{}
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
func strict(r io.Reader, v any) error {
|
||||
b, err := io.ReadAll(io.LimitReader(r, MaxInstanceSize+1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(b) > MaxInstanceSize {
|
||||
return fmt.Errorf("larger than %d bytes", MaxInstanceSize)
|
||||
}
|
||||
d := yaml.NewDecoder(bytes.NewReader(b))
|
||||
d.KnownFields(true)
|
||||
if err := d.Decode(v); err != nil && !errors.Is(err, io.EOF) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshal renders any of the user files as YAML with sorted parameters.
|
||||
func Marshal(v any) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
enc := yaml.NewEncoder(&buf)
|
||||
enc.SetIndent(2)
|
||||
if err := enc.Encode(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
enc.Close()
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// PresetFrom derives a preset from an instance: all answers except
|
||||
// instance-specific and secret ones (PRE-02, PRE-04).
|
||||
func PresetFrom(name string, in *Instance, s *catalog.Solution) *Preset {
|
||||
p := &Preset{
|
||||
APIVersion: catalog.APIVersion, Kind: catalog.KindPreset,
|
||||
Metadata: InstanceMeta{Name: name},
|
||||
Spec: PresetSpec{Solution: s.Meta.ID, Version: s.Meta.Version, Mode: in.Spec.Mode, Parameters: map[string]any{}},
|
||||
}
|
||||
keys := make([]string, 0, len(in.Spec.Parameters))
|
||||
for k := range in.Spec.Parameters {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
q, _ := s.Question(k)
|
||||
if q == nil || q.InstanceSpecific || q.Type == "secret" {
|
||||
continue
|
||||
}
|
||||
p.Spec.Parameters[k] = in.Spec.Parameters[k]
|
||||
}
|
||||
return p
|
||||
}
|
||||
Reference in new issue
Block a user