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,219 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// BigPicture is the read-only view of assets/bigpicture/verse that the
|
||||
// guide builds on (ADR-0002). Validation of these files belongs to bpgen;
|
||||
// here they are decoded leniently and only the fields the guide uses are
|
||||
// kept.
|
||||
type BigPicture struct {
|
||||
Dir string `json:"-"`
|
||||
Title string `json:"title"`
|
||||
WebPath string `json:"webPath"` // "map/"
|
||||
Topics map[string]*BPTopic `json:"topics"`
|
||||
Stories map[string]*BPStory `json:"stories"`
|
||||
Products map[string]*BPProduct `json:"products"`
|
||||
Connections []BPConnection `json:"connections"`
|
||||
Interfaces map[string]BPInterface `json:"interfaces"`
|
||||
Files []FileHash `json:"-"`
|
||||
}
|
||||
|
||||
// BPTopic is a Big Picture topic.
|
||||
type BPTopic struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Summary string `yaml:"summary" json:"summary"`
|
||||
EntryStory string `yaml:"entryStory" json:"entryStory,omitempty"`
|
||||
}
|
||||
|
||||
// BPStory is a Big Picture goal (user story).
|
||||
type BPStory struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Intent string `yaml:"intent" json:"intent"`
|
||||
Outcome string `yaml:"outcome" json:"outcome"`
|
||||
Explanation string `yaml:"explanation" json:"explanation,omitempty"`
|
||||
PrimaryTopic string `yaml:"primaryTopic" json:"primaryTopic"`
|
||||
RelatedTopics []string `yaml:"relatedTopics" json:"relatedTopics,omitempty"`
|
||||
Roles []struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Product string `yaml:"product" json:"product"`
|
||||
Contribution string `yaml:"contribution" json:"contribution"`
|
||||
Participation string `yaml:"participation" json:"participation"`
|
||||
} `yaml:"roles" json:"roles"`
|
||||
Next []struct {
|
||||
Story string `yaml:"story" json:"story"`
|
||||
Reason string `yaml:"reason" json:"reason"`
|
||||
} `yaml:"next" json:"next,omitempty"`
|
||||
}
|
||||
|
||||
// BPProduct is a Big Picture program.
|
||||
type BPProduct struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Summary string `yaml:"summary" json:"summary"`
|
||||
PrimaryTopic string `yaml:"primaryTopic" json:"primaryTopic"`
|
||||
Tasks []string `yaml:"tasks" json:"tasks,omitempty"`
|
||||
Links []struct {
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Href string `yaml:"href" json:"href"`
|
||||
} `yaml:"links" json:"links,omitempty"`
|
||||
}
|
||||
|
||||
// BPConnection is a technical relation between programs/interfaces.
|
||||
type BPConnection struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
From string `yaml:"from" json:"from"`
|
||||
To string `yaml:"to" json:"to"`
|
||||
Type string `yaml:"type" json:"type"`
|
||||
Label string `yaml:"label" json:"label"`
|
||||
Scope string `yaml:"scope" json:"scope"`
|
||||
Review string `yaml:"review" json:"review"`
|
||||
Stories []string `yaml:"stories" json:"stories,omitempty"`
|
||||
}
|
||||
|
||||
// BPInterface is a shared interface (OCI registry, ...).
|
||||
type BPInterface struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Summary string `yaml:"summary" json:"summary"`
|
||||
}
|
||||
|
||||
// LoadBigPicture reads dir/manifest.yaml and the files it names.
|
||||
func LoadBigPicture(dir string) (*BigPicture, error) {
|
||||
bp := &BigPicture{
|
||||
Dir: dir, WebPath: "map/",
|
||||
Topics: map[string]*BPTopic{}, Stories: map[string]*BPStory{},
|
||||
Products: map[string]*BPProduct{}, Interfaces: map[string]BPInterface{},
|
||||
}
|
||||
var manifest struct {
|
||||
Title string `yaml:"title"`
|
||||
Content struct {
|
||||
Topics string `yaml:"topics"`
|
||||
Products string `yaml:"products"`
|
||||
Stories string `yaml:"stories"`
|
||||
Connections string `yaml:"connections"`
|
||||
} `yaml:"content"`
|
||||
}
|
||||
if err := bp.read("manifest.yaml", &manifest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bp.Title = manifest.Title
|
||||
var topics struct {
|
||||
Topics []*BPTopic `yaml:"topics"`
|
||||
}
|
||||
if err := bp.read(manifest.Content.Topics, &topics); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, t := range topics.Topics {
|
||||
bp.Topics[t.ID] = t
|
||||
}
|
||||
if err := bp.readDir(manifest.Content.Products, func(name string) error {
|
||||
var p BPProduct
|
||||
if err := bp.read(name, &p); err != nil {
|
||||
return err
|
||||
}
|
||||
bp.Products[p.ID] = &p
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := bp.readDir(manifest.Content.Stories, func(name string) error {
|
||||
var s BPStory
|
||||
if err := bp.read(name, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
bp.Stories[s.ID] = &s
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var conns struct {
|
||||
Interfaces []BPInterface `yaml:"interfaces"`
|
||||
Connections []BPConnection `yaml:"connections"`
|
||||
}
|
||||
if manifest.Content.Connections != "" {
|
||||
if err := bp.read(manifest.Content.Connections, &conns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, i := range conns.Interfaces {
|
||||
bp.Interfaces[i.ID] = i
|
||||
}
|
||||
bp.Connections = conns.Connections
|
||||
sort.Slice(bp.Files, func(i, j int) bool { return bp.Files[i].Path < bp.Files[j].Path })
|
||||
return bp, nil
|
||||
}
|
||||
|
||||
func (bp *BigPicture) read(name string, v any) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("big picture: manifest names no file")
|
||||
}
|
||||
b, err := os.ReadFile(filepath.Join(bp.Dir, name))
|
||||
if err != nil {
|
||||
return fmt.Errorf("big picture: %w", err)
|
||||
}
|
||||
if len(b) > MaxFileSize {
|
||||
return fmt.Errorf("big picture: %s too large", name)
|
||||
}
|
||||
bp.Files = append(bp.Files, FileHash{Path: "bigpicture:" + filepath.ToSlash(name), Hash: sha(b)})
|
||||
if err := yaml.Unmarshal(b, v); err != nil {
|
||||
return fmt.Errorf("big picture: %s: %w", name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bp *BigPicture) readDir(sub string, fn func(string) error) error {
|
||||
ents, err := os.ReadDir(filepath.Join(bp.Dir, sub))
|
||||
if err != nil {
|
||||
return fmt.Errorf("big picture: %w", err)
|
||||
}
|
||||
for _, e := range ents {
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".yaml") {
|
||||
continue
|
||||
}
|
||||
if err := fn(filepath.Join(sub, e.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Has reports whether a Big Picture element ID exists.
|
||||
func (bp *BigPicture) Has(id string) bool {
|
||||
if bp == nil {
|
||||
return false
|
||||
}
|
||||
if _, ok := bp.Topics[id]; ok {
|
||||
return true
|
||||
}
|
||||
if _, ok := bp.Stories[id]; ok {
|
||||
return true
|
||||
}
|
||||
if _, ok := bp.Products[id]; ok {
|
||||
return true
|
||||
}
|
||||
_, ok := bp.Interfaces[id]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Fragment is the map URL fragment for an element (story.x -> story=x).
|
||||
func (bp *BigPicture) Fragment(id string) string {
|
||||
kind, slug, ok := strings.Cut(id, ".")
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
switch kind {
|
||||
case "topic", "story", "product", "interface":
|
||||
return kind + "=" + slug
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package catalog
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCond(t *testing.T) {
|
||||
env := Env{Answers: map[string]any{"a": "x", "n": 3, "l": []any{"p", "q"}, "b": true, "e": ""}, Mode: LevelGuided}
|
||||
cases := []struct {
|
||||
c Cond
|
||||
want bool
|
||||
}{
|
||||
{Cond{}, true},
|
||||
{Cond{"a": "x"}, true},
|
||||
{Cond{"a": "y"}, false},
|
||||
{Cond{"a": []any{"y", "x"}}, true},
|
||||
{Cond{"l": "q"}, true},
|
||||
{Cond{"l": "z"}, false},
|
||||
{Cond{"a": map[string]any{"not": "x"}}, false},
|
||||
{Cond{"n": map[string]any{"gte": 3, "lt": 4}}, true},
|
||||
{Cond{"l": map[string]any{"count_gte": 2}}, true},
|
||||
{Cond{"e": map[string]any{"set": false}}, true},
|
||||
{Cond{"missing": map[string]any{"set": true}}, false},
|
||||
{Cond{"b": true}, true},
|
||||
{Cond{"b": "true"}, true},
|
||||
{Cond{"any": []any{map[string]any{"a": "y"}, map[string]any{"n": 3}}}, true},
|
||||
{Cond{"all": []any{map[string]any{"a": "x"}, map[string]any{"n": 4}}}, false},
|
||||
{Cond{"not": map[string]any{"a": "x"}}, false},
|
||||
{Cond{"$mode": "guided"}, true},
|
||||
}
|
||||
for i, c := range cases {
|
||||
got, err := c.c.Eval(env)
|
||||
if err != nil || got != c.want {
|
||||
t.Errorf("%d %v: got %v %v", i, c.c, got, err)
|
||||
}
|
||||
}
|
||||
if _, err := (Cond{"a": map[string]any{"regex": "x"}}).Eval(env); err == nil {
|
||||
t.Error("unknown operator accepted")
|
||||
}
|
||||
deep := Cond{"a": "x"}
|
||||
for i := 0; i < MaxCondDepth+2; i++ {
|
||||
deep = Cond{"not": map[string]any(deep)}
|
||||
}
|
||||
if _, err := deep.Eval(env); err == nil {
|
||||
t.Error("unbounded nesting accepted")
|
||||
}
|
||||
if r := (Cond{"any": []any{map[string]any{"x": 1}}, "y": 2, "$mode": "q"}).Refs(); len(r) != 2 || r[0] != "x" || r[1] != "y" {
|
||||
t.Errorf("refs %v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeRelPath(t *testing.T) {
|
||||
for _, ok := range []string{"a/b.yaml", "yamase/yamase.yaml"} {
|
||||
if !SafeRelPath(ok) {
|
||||
t.Errorf("%s rejected", ok)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"", "/etc/passwd", "../x", "a/../../x", "a/./b", "a//b", ".hidden/x", `a\b`, "c:/x"} {
|
||||
if SafeRelPath(bad) {
|
||||
t.Errorf("%q accepted", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Cond is a condition over wizard answers (ADR-0003). It is plain data:
|
||||
//
|
||||
// {q: v} answer equals v (a multiselect answer: contains v)
|
||||
// {q: [a, b]} answer is one of a, b (multiselect: contains one)
|
||||
// {q: {in: [..]}} same as a list
|
||||
// {q: {not: v}} answer is not v (not one of a list)
|
||||
// {q: {set: true|false}} answer is (not) given and non-empty
|
||||
// {q: {gt|gte|lt|lte: n}} numeric comparison
|
||||
// {q: {count_gte|count_lte: n}} number of list items
|
||||
// {all: [c, ..]} {any: [c, ..]} {not: c}
|
||||
// {$mode: guided} wizard mode; {$platform: linux}
|
||||
//
|
||||
// Several keys in one map must all hold. An empty condition is true.
|
||||
type Cond map[string]any
|
||||
|
||||
// Env is what a condition is evaluated against.
|
||||
type Env struct {
|
||||
Answers map[string]any
|
||||
Mode Level
|
||||
Platform string
|
||||
}
|
||||
|
||||
// MaxCondDepth bounds nesting (RULE-08).
|
||||
const MaxCondDepth = 16
|
||||
|
||||
// Eval evaluates c. Errors are malformed conditions (the linter reports
|
||||
// them before a catalog is used).
|
||||
func (c Cond) Eval(env Env) (bool, error) { return evalMap(map[string]any(c), env, 0) }
|
||||
|
||||
// Holds is Eval for callers that validated the catalog: a malformed
|
||||
// condition counts as false.
|
||||
func (c Cond) Holds(env Env) bool {
|
||||
ok, err := c.Eval(env)
|
||||
return err == nil && ok
|
||||
}
|
||||
|
||||
func evalMap(m map[string]any, env Env, depth int) (bool, error) {
|
||||
if depth > MaxCondDepth {
|
||||
return false, fmt.Errorf("condition nested deeper than %d", MaxCondDepth)
|
||||
}
|
||||
keys := sortedKeys(m)
|
||||
for _, k := range keys {
|
||||
v := m[k]
|
||||
var ok bool
|
||||
var err error
|
||||
switch k {
|
||||
case "all", "any":
|
||||
list, isList := v.([]any)
|
||||
if !isList {
|
||||
return false, fmt.Errorf("%s: want a list of conditions", k)
|
||||
}
|
||||
ok = k == "all"
|
||||
for _, item := range list {
|
||||
sub, isMap := asMap(item)
|
||||
if !isMap {
|
||||
return false, fmt.Errorf("%s: want a list of conditions", k)
|
||||
}
|
||||
r, err := evalMap(sub, env, depth+1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if k == "all" && !r {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
if k == "any" && r {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
case "not":
|
||||
sub, isMap := asMap(v)
|
||||
if !isMap {
|
||||
return false, fmt.Errorf("not: want a condition")
|
||||
}
|
||||
r, err := evalMap(sub, env, depth+1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
ok = !r
|
||||
case "$mode":
|
||||
ok, err = match(string(env.Mode), v)
|
||||
case "$platform":
|
||||
ok, err = match(env.Platform, v)
|
||||
default:
|
||||
ok, err = match(env.Answers[k], v)
|
||||
}
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s: %w", k, err)
|
||||
}
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// match compares one answer with a condition value.
|
||||
func match(answer any, want any) (bool, error) {
|
||||
if ops, ok := asMap(want); ok {
|
||||
for _, op := range sortedKeys(ops) {
|
||||
arg := ops[op]
|
||||
var r bool
|
||||
switch op {
|
||||
case "in":
|
||||
r = matchAny(answer, arg)
|
||||
case "not":
|
||||
r = !matchAny(answer, arg)
|
||||
case "set":
|
||||
b, isBool := arg.(bool)
|
||||
if !isBool {
|
||||
return false, fmt.Errorf("set: want true or false")
|
||||
}
|
||||
r = IsSet(answer) == b
|
||||
case "gt", "gte", "lt", "lte":
|
||||
a, aok := toFloat(answer)
|
||||
n, nok := toFloat(arg)
|
||||
if !nok {
|
||||
return false, fmt.Errorf("%s: want a number", op)
|
||||
}
|
||||
if !aok {
|
||||
r = false
|
||||
break
|
||||
}
|
||||
switch op {
|
||||
case "gt":
|
||||
r = a > n
|
||||
case "gte":
|
||||
r = a >= n
|
||||
case "lt":
|
||||
r = a < n
|
||||
case "lte":
|
||||
r = a <= n
|
||||
}
|
||||
case "count_gte", "count_lte":
|
||||
n, nok := toFloat(arg)
|
||||
if !nok {
|
||||
return false, fmt.Errorf("%s: want a number", op)
|
||||
}
|
||||
cnt := float64(len(asList(answer)))
|
||||
if op == "count_gte" {
|
||||
r = cnt >= n
|
||||
} else {
|
||||
r = cnt <= n
|
||||
}
|
||||
default:
|
||||
return false, fmt.Errorf("unknown operator %q", op)
|
||||
}
|
||||
if !r {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
return matchAny(answer, want), nil
|
||||
}
|
||||
|
||||
// matchAny: want is a scalar or a list of scalars; answer a scalar or list.
|
||||
func matchAny(answer any, want any) bool {
|
||||
wants := asList(want)
|
||||
if want != nil && len(wants) == 0 {
|
||||
if _, isList := want.([]any); !isList {
|
||||
wants = []any{want}
|
||||
}
|
||||
}
|
||||
answers := asList(answer)
|
||||
if _, isList := answer.([]any); !isList {
|
||||
if _, isStrList := answer.([]string); !isStrList {
|
||||
answers = []any{answer}
|
||||
}
|
||||
}
|
||||
for _, a := range answers {
|
||||
for _, w := range wants {
|
||||
if Equal(a, w) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Equal compares two scalar answer values loosely: numbers by value,
|
||||
// everything else by its string form ("true" == true).
|
||||
func Equal(a, b any) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if fa, ok := toFloat(a); ok {
|
||||
if fb, ok := toFloat(b); ok {
|
||||
return fa == fb
|
||||
}
|
||||
}
|
||||
return Scalar(a) == Scalar(b)
|
||||
}
|
||||
|
||||
// Scalar renders a scalar answer as text.
|
||||
func Scalar(v any) string {
|
||||
switch x := v.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return x
|
||||
case bool:
|
||||
return strconv.FormatBool(x)
|
||||
case int:
|
||||
return strconv.Itoa(x)
|
||||
case int64:
|
||||
return strconv.FormatInt(x, 10)
|
||||
case float64:
|
||||
return strconv.FormatFloat(x, 'f', -1, 64)
|
||||
}
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
|
||||
// IsSet reports whether an answer is present and not empty.
|
||||
func IsSet(v any) bool {
|
||||
switch x := v.(type) {
|
||||
case nil:
|
||||
return false
|
||||
case string:
|
||||
return strings.TrimSpace(x) != ""
|
||||
case []any:
|
||||
return len(x) > 0
|
||||
case []string:
|
||||
return len(x) > 0
|
||||
case map[string]any:
|
||||
return len(x) > 0
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func toFloat(v any) (float64, bool) {
|
||||
switch x := v.(type) {
|
||||
case int:
|
||||
return float64(x), true
|
||||
case int64:
|
||||
return float64(x), true
|
||||
case float64:
|
||||
return x, true
|
||||
case uint64:
|
||||
return float64(x), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func asList(v any) []any {
|
||||
switch x := v.(type) {
|
||||
case []any:
|
||||
return x
|
||||
case []string:
|
||||
out := make([]any, len(x))
|
||||
for i, s := range x {
|
||||
out[i] = s
|
||||
}
|
||||
return out
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func asMap(v any) (map[string]any, bool) {
|
||||
switch x := v.(type) {
|
||||
case map[string]any:
|
||||
return x, true
|
||||
case Cond:
|
||||
return map[string]any(x), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func sortedKeys(m map[string]any) []string {
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
// Refs returns the question IDs a condition reads (for the linter).
|
||||
func (c Cond) Refs() []string {
|
||||
seen := map[string]bool{}
|
||||
var walk func(m map[string]any, depth int)
|
||||
walk = func(m map[string]any, depth int) {
|
||||
if depth > MaxCondDepth {
|
||||
return
|
||||
}
|
||||
for k, v := range m {
|
||||
switch k {
|
||||
case "all", "any":
|
||||
if list, ok := v.([]any); ok {
|
||||
for _, item := range list {
|
||||
if sub, ok := asMap(item); ok {
|
||||
walk(sub, depth+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
case "not":
|
||||
if sub, ok := asMap(v); ok {
|
||||
walk(sub, depth+1)
|
||||
}
|
||||
case "$mode", "$platform":
|
||||
default:
|
||||
seen[k] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(map[string]any(c), 0)
|
||||
out := make([]string, 0, len(seen))
|
||||
for k := range seen {
|
||||
out = append(out, k)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,503 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Limits protect against hostile catalogs (SEC-03).
|
||||
const (
|
||||
MaxFileSize = 4 << 20
|
||||
MaxFiles = 10000
|
||||
MaxQuestions = 500
|
||||
MaxRules = 1000
|
||||
)
|
||||
|
||||
// Entity fields shared by all loaded manifests.
|
||||
type Entity struct {
|
||||
Kind string `json:"kind"`
|
||||
Meta Meta `json:"metadata"`
|
||||
File string `json:"file"` // relative to the catalog root (or "bigpicture:...")
|
||||
Line int `json:"line"`
|
||||
Hash string `json:"hash"` // sha256 of the document's source
|
||||
}
|
||||
|
||||
// Package is a loaded Package manifest.
|
||||
type Package struct {
|
||||
Entity
|
||||
Spec PackageSpec `json:"spec"`
|
||||
}
|
||||
|
||||
// Module returns the module with that ID.
|
||||
func (p *Package) Module(id string) *Module {
|
||||
for i := range p.Spec.Modules {
|
||||
if p.Spec.Modules[i].ID == id {
|
||||
return &p.Spec.Modules[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Profile is a loaded Profile manifest.
|
||||
type Profile struct {
|
||||
Entity
|
||||
Spec ProfileSpec `json:"spec"`
|
||||
}
|
||||
|
||||
// Key is "<package>/<id>".
|
||||
func (p *Profile) Key() string { return p.Meta.Package + "/" + p.Meta.ID }
|
||||
|
||||
// Solution is a loaded Solution or Task manifest.
|
||||
type Solution struct {
|
||||
Entity
|
||||
Spec SolutionSpec `json:"spec"`
|
||||
}
|
||||
|
||||
// IsTask reports whether this is a Task.
|
||||
func (s *Solution) IsTask() bool { return s.Kind == KindTask }
|
||||
|
||||
// Question returns the question with that ID and its index.
|
||||
func (s *Solution) Question(id string) (*Question, int) {
|
||||
for i := range s.Spec.Questions {
|
||||
if s.Spec.Questions[i].ID == id {
|
||||
return &s.Spec.Questions[i], i
|
||||
}
|
||||
}
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
// Example is a loaded Example manifest.
|
||||
type Example struct {
|
||||
Entity
|
||||
Spec ExampleSpec `json:"spec"`
|
||||
}
|
||||
|
||||
// Key is "<solution>/<id>".
|
||||
func (e *Example) Key() string { return e.Spec.Solution + "/" + e.Meta.ID }
|
||||
|
||||
// KnowledgeSource is a loaded KnowledgeSource manifest.
|
||||
type KnowledgeSource struct {
|
||||
Entity
|
||||
Spec KnowledgeSourceSpec `json:"spec"`
|
||||
}
|
||||
|
||||
// Catalog is everything loaded from one catalog root.
|
||||
type Catalog struct {
|
||||
Root string `json:"-"`
|
||||
Meta Meta `json:"metadata"`
|
||||
Spec CatalogSpec `json:"spec"`
|
||||
Capabilities map[string]*Capability `json:"capabilities"`
|
||||
Packages map[string]*Package `json:"packages"`
|
||||
Profiles map[string]*Profile `json:"profiles"`
|
||||
Solutions map[string]*Solution `json:"solutions"`
|
||||
Tasks map[string]*Solution `json:"tasks"`
|
||||
Examples map[string]*Example `json:"examples"`
|
||||
Sources map[string]*KnowledgeSource `json:"sources"`
|
||||
BigPicture *BigPicture `json:"bigpicture"`
|
||||
// Files are all source files with their hashes, sorted (for locks).
|
||||
Files []FileHash `json:"files"`
|
||||
// Issues found while loading (fatal ones make Load fail).
|
||||
Issues []Issue `json:"-"`
|
||||
|
||||
capFile map[string]string
|
||||
}
|
||||
|
||||
// FileHash is one source file and its sha256.
|
||||
type FileHash struct {
|
||||
Path string `json:"path"`
|
||||
Hash string `json:"sha256"`
|
||||
}
|
||||
|
||||
// Issue is a problem in a catalog (load, validation or lint).
|
||||
type Issue struct {
|
||||
Severity string `json:"severity"` // error | warning
|
||||
File string `json:"file,omitempty"`
|
||||
Line int `json:"line,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (i Issue) String() string {
|
||||
loc := i.File
|
||||
if i.Line > 0 {
|
||||
loc = fmt.Sprintf("%s:%d", i.File, i.Line)
|
||||
}
|
||||
if loc != "" {
|
||||
loc += ": "
|
||||
}
|
||||
id := ""
|
||||
if i.ID != "" {
|
||||
id = i.ID + ": "
|
||||
}
|
||||
return fmt.Sprintf("%s%s%s (%s)", loc, id, i.Message, i.Code)
|
||||
}
|
||||
|
||||
// ErrInvalid wraps load errors.
|
||||
var ErrInvalid = errors.New("invalid catalog")
|
||||
|
||||
// Load reads a catalog root directory (the one with catalog.yaml).
|
||||
func Load(root string) (*Catalog, error) {
|
||||
return LoadFS(os.DirFS(root), root)
|
||||
}
|
||||
|
||||
// LoadFS reads a catalog from fsys (root is only used for messages and
|
||||
// to find the Big Picture relative to it, when fsys is a directory).
|
||||
func LoadFS(fsys fs.FS, root string) (*Catalog, error) {
|
||||
c := &Catalog{
|
||||
Root: root,
|
||||
Capabilities: map[string]*Capability{},
|
||||
Packages: map[string]*Package{},
|
||||
Profiles: map[string]*Profile{},
|
||||
Solutions: map[string]*Solution{},
|
||||
Tasks: map[string]*Solution{},
|
||||
Examples: map[string]*Example{},
|
||||
Sources: map[string]*KnowledgeSource{},
|
||||
capFile: map[string]string{},
|
||||
}
|
||||
var files []string
|
||||
err := fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := d.Name()
|
||||
if d.IsDir() {
|
||||
if p != "." && (strings.HasPrefix(name, ".") || name == "schema" || name == "testdata" || name == "golden") {
|
||||
return fs.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml") {
|
||||
files = append(files, p)
|
||||
if len(files) > MaxFiles {
|
||||
return fmt.Errorf("more than %d files", MaxFiles)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrInvalid, err)
|
||||
}
|
||||
sort.Strings(files)
|
||||
sawRoot := false
|
||||
for _, f := range files {
|
||||
b, err := readLimited(fsys, f)
|
||||
if err != nil {
|
||||
c.add("error", f, 0, "", "read", err.Error())
|
||||
continue
|
||||
}
|
||||
c.Files = append(c.Files, FileHash{Path: f, Hash: sha(b)})
|
||||
if c.loadFile(f, b) {
|
||||
sawRoot = true
|
||||
}
|
||||
}
|
||||
if !sawRoot {
|
||||
c.add("error", "catalog.yaml", 0, "", "no-root", "no Catalog manifest (catalog.yaml) found")
|
||||
}
|
||||
if c.Spec.BigPicture != "" {
|
||||
bpDir := c.Spec.BigPicture
|
||||
if !filepath.IsAbs(bpDir) {
|
||||
bpDir = filepath.Join(root, bpDir)
|
||||
}
|
||||
bp, err := LoadBigPicture(bpDir)
|
||||
if err != nil {
|
||||
c.add("error", "catalog.yaml", 0, "", "bigpicture", err.Error())
|
||||
} else {
|
||||
c.BigPicture = bp
|
||||
c.Files = append(c.Files, bp.Files...)
|
||||
}
|
||||
}
|
||||
sort.Slice(c.Files, func(i, j int) bool { return c.Files[i].Path < c.Files[j].Path })
|
||||
if errs := c.Errors(); len(errs) > 0 {
|
||||
return c, fmt.Errorf("%w: %d error(s), first: %s", ErrInvalid, len(errs), errs[0])
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func readLimited(fsys fs.FS, p string) ([]byte, error) {
|
||||
f, err := fsys.Open(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
b, err := io.ReadAll(io.LimitReader(f, MaxFileSize+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(b) > MaxFileSize {
|
||||
return nil, fmt.Errorf("larger than %d bytes", MaxFileSize)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func sha(b []byte) string {
|
||||
h := sha256.Sum256(b)
|
||||
return hex.EncodeToString(h[:])
|
||||
}
|
||||
|
||||
func (c *Catalog) add(sev, file string, line int, id, code, msg string) {
|
||||
c.Issues = append(c.Issues, Issue{Severity: sev, File: file, Line: line, ID: id, Code: code, Message: msg})
|
||||
}
|
||||
|
||||
// Errors returns the issues with severity error.
|
||||
func (c *Catalog) Errors() []Issue {
|
||||
var out []Issue
|
||||
for _, i := range c.Issues {
|
||||
if i.Severity == "error" {
|
||||
out = append(out, i)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// loadFile decodes all documents of one file; reports whether it held the root.
|
||||
func (c *Catalog) loadFile(file string, b []byte) bool {
|
||||
dec := yaml.NewDecoder(bytes.NewReader(b))
|
||||
root := false
|
||||
for {
|
||||
var doc yaml.Node
|
||||
err := dec.Decode(&doc)
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
c.add("error", file, 0, "", "yaml", err.Error())
|
||||
return root
|
||||
}
|
||||
if len(doc.Content) == 0 {
|
||||
continue
|
||||
}
|
||||
raw, _ := yaml.Marshal(&doc)
|
||||
var h Header
|
||||
if err := strictDecode(raw, &h); err != nil {
|
||||
c.add("error", file, doc.Line, "", "header", err.Error())
|
||||
continue
|
||||
}
|
||||
line := doc.Content[0].Line
|
||||
if h.APIVersion != APIVersion {
|
||||
c.add("error", file, line, h.Metadata.ID, "api-version", fmt.Sprintf("apiVersion %q, want %q", h.APIVersion, APIVersion))
|
||||
continue
|
||||
}
|
||||
ent := Entity{Kind: h.Kind, Meta: h.Metadata, File: file, Line: line, Hash: sha(raw)}
|
||||
specRaw, _ := yaml.Marshal(&h.Spec)
|
||||
if h.Spec.Kind == 0 {
|
||||
specRaw = []byte("{}")
|
||||
}
|
||||
if err := c.addEntity(ent, specRaw); err != nil {
|
||||
c.add("error", file, line, h.Metadata.ID, "spec", err.Error())
|
||||
continue
|
||||
}
|
||||
if h.Kind == KindCatalog {
|
||||
root = true
|
||||
}
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func strictDecode(b []byte, v any) error {
|
||||
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
|
||||
}
|
||||
|
||||
func (c *Catalog) addEntity(e Entity, spec []byte) error {
|
||||
dup := func(kind, id, other string) error {
|
||||
return fmt.Errorf("duplicate %s %q (also in %s)", kind, id, other)
|
||||
}
|
||||
switch e.Kind {
|
||||
case KindCatalog:
|
||||
if c.Meta.ID != "" {
|
||||
return fmt.Errorf("more than one Catalog manifest")
|
||||
}
|
||||
if err := strictDecode(spec, &c.Spec); err != nil {
|
||||
return err
|
||||
}
|
||||
c.Meta = e.Meta
|
||||
case KindCapabilities:
|
||||
var s CapabilitiesSpec
|
||||
if err := strictDecode(spec, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range s.Capabilities {
|
||||
cp := s.Capabilities[i]
|
||||
if other, ok := c.capFile[cp.ID]; ok {
|
||||
return dup("capability", cp.ID, other)
|
||||
}
|
||||
c.Capabilities[cp.ID] = &cp
|
||||
c.capFile[cp.ID] = e.File
|
||||
}
|
||||
case KindPackage:
|
||||
p := &Package{Entity: e}
|
||||
if err := strictDecode(spec, &p.Spec); err != nil {
|
||||
return err
|
||||
}
|
||||
if o, ok := c.Packages[e.Meta.ID]; ok {
|
||||
return dup("package", e.Meta.ID, o.File)
|
||||
}
|
||||
c.Packages[e.Meta.ID] = p
|
||||
case KindProfile:
|
||||
p := &Profile{Entity: e}
|
||||
if err := strictDecode(spec, &p.Spec); err != nil {
|
||||
return err
|
||||
}
|
||||
if e.Meta.Package == "" {
|
||||
return fmt.Errorf("profile %q: metadata.package is required", e.Meta.ID)
|
||||
}
|
||||
if o, ok := c.Profiles[p.Key()]; ok {
|
||||
return dup("profile", p.Key(), o.File)
|
||||
}
|
||||
c.Profiles[p.Key()] = p
|
||||
case KindSolution, KindTask:
|
||||
s := &Solution{Entity: e}
|
||||
if err := strictDecode(spec, &s.Spec); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(s.Spec.Questions) > MaxQuestions || len(s.Spec.Rules) > MaxRules {
|
||||
return fmt.Errorf("more than %d questions or %d rules", MaxQuestions, MaxRules)
|
||||
}
|
||||
m := c.Solutions
|
||||
if e.Kind == KindTask {
|
||||
m = c.Tasks
|
||||
}
|
||||
if o, ok := m[e.Meta.ID]; ok {
|
||||
return dup(strings.ToLower(e.Kind), e.Meta.ID, o.File)
|
||||
}
|
||||
m[e.Meta.ID] = s
|
||||
case KindExample:
|
||||
x := &Example{Entity: e}
|
||||
if err := strictDecode(spec, &x.Spec); err != nil {
|
||||
return err
|
||||
}
|
||||
if x.Spec.Solution == "" {
|
||||
// default: the solution directory the example lives in
|
||||
x.Spec.Solution = solutionDirOf(e.File)
|
||||
}
|
||||
if o, ok := c.Examples[x.Key()]; ok {
|
||||
return dup("example", x.Key(), o.File)
|
||||
}
|
||||
c.Examples[x.Key()] = x
|
||||
case KindKnowledgeSource:
|
||||
k := &KnowledgeSource{Entity: e}
|
||||
if err := strictDecode(spec, &k.Spec); err != nil {
|
||||
return err
|
||||
}
|
||||
if o, ok := c.Sources[e.Meta.ID]; ok {
|
||||
return dup("knowledge source", e.Meta.ID, o.File)
|
||||
}
|
||||
c.Sources[e.Meta.ID] = k
|
||||
default:
|
||||
return fmt.Errorf("unknown kind %q", e.Kind)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// solutionDirOf: solutions/<category>/<id>/examples/x.yaml -> <id>.
|
||||
func solutionDirOf(file string) string {
|
||||
dir := path.Dir(path.Dir(file))
|
||||
return path.Base(dir)
|
||||
}
|
||||
|
||||
// SortedIDs returns the keys of a map in order.
|
||||
func SortedIDs[V any](m map[string]V) []string {
|
||||
out := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
out = append(out, k)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// Lookup finds a solution or task by ID (solutions first).
|
||||
func (c *Catalog) Lookup(id string) *Solution {
|
||||
if s, ok := c.Solutions[id]; ok {
|
||||
return s
|
||||
}
|
||||
if s, ok := c.Tasks[id]; ok {
|
||||
return s
|
||||
}
|
||||
for _, m := range []map[string]*Solution{c.Solutions, c.Tasks} {
|
||||
for _, s := range m {
|
||||
for _, a := range s.Meta.Aliases {
|
||||
if a == id {
|
||||
return s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ProvidersOf lists package modules providing a capability, best first.
|
||||
func (c *Catalog) ProvidersOf(capID string) []ModuleRef {
|
||||
var out []ModuleRef
|
||||
for _, pid := range SortedIDs(c.Packages) {
|
||||
p := c.Packages[pid]
|
||||
for _, m := range p.Spec.Modules {
|
||||
for _, pr := range m.Provides {
|
||||
if pr == capID {
|
||||
out = append(out, ModuleRef{Package: pid, Module: m.ID, Priority: m.Priority, Support: m.Support})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.SliceStable(out, func(i, j int) bool { return out[i].Priority > out[j].Priority })
|
||||
return out
|
||||
}
|
||||
|
||||
// ModuleRef points at one module of one package.
|
||||
type ModuleRef struct {
|
||||
Package string `json:"package"`
|
||||
Module string `json:"module"`
|
||||
Priority int `json:"priority,omitempty"`
|
||||
Support Support `json:"support,omitempty"`
|
||||
}
|
||||
|
||||
func (m ModuleRef) String() string { return m.Package + "/" + m.Module }
|
||||
|
||||
// ProfileChain returns the profile and its ancestors, base first; it
|
||||
// fails on unknown profiles and cycles (PROF-03).
|
||||
func (c *Catalog) ProfileChain(pkg, id string) ([]*Profile, error) {
|
||||
var chain []*Profile
|
||||
seen := map[string]bool{}
|
||||
for cur := id; cur != ""; {
|
||||
key := pkg + "/" + cur
|
||||
if seen[key] {
|
||||
return nil, fmt.Errorf("profile %s: inheritance cycle", key)
|
||||
}
|
||||
seen[key] = true
|
||||
p, ok := c.Profiles[key]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown profile %s", key)
|
||||
}
|
||||
chain = append(chain, p)
|
||||
cur = p.Spec.Extends
|
||||
}
|
||||
for i, j := 0, len(chain)-1; i < j; i, j = i+1, j-1 {
|
||||
chain[i], chain[j] = chain[j], chain[i]
|
||||
}
|
||||
return chain, nil
|
||||
}
|
||||
|
||||
// Hash is a digest over all catalog files (catalog version for locks).
|
||||
func (c *Catalog) Hash() string {
|
||||
h := sha256.New()
|
||||
for _, f := range c.Files {
|
||||
fmt.Fprintf(h, "%s %s\n", f.Path, f.Hash)
|
||||
}
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
@@ -0,0 +1,509 @@
|
||||
// Package catalog is the data model of Yetano Knowledge & Solutions: the
|
||||
// manifests under assets/solutions (Capability, Package, Profile,
|
||||
// Solution, Task, Example, KnowledgeSource) and the read-only view of the
|
||||
// Big Picture sources (topics, goals, programs) they build on.
|
||||
//
|
||||
// Everything here is data. Nothing in a catalog is ever executed: there
|
||||
// are no template functions, no commands run by the engine and no network
|
||||
// access (see docs/knowledge-solutions/SECURITY.md in the meta-repo).
|
||||
package catalog
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
// APIVersion is the only manifest version this package reads.
|
||||
const APIVersion = "yetano.dev/v1"
|
||||
|
||||
// Manifest kinds.
|
||||
const (
|
||||
KindCatalog = "Catalog"
|
||||
KindCapabilities = "Capabilities"
|
||||
KindPackage = "Package"
|
||||
KindProfile = "Profile"
|
||||
KindSolution = "Solution"
|
||||
KindTask = "Task"
|
||||
KindExample = "Example"
|
||||
KindPreset = "Preset"
|
||||
KindInstance = "SolutionInstance"
|
||||
KindSession = "WizardSession"
|
||||
KindKnowledgeSource = "KnowledgeSource"
|
||||
)
|
||||
|
||||
// Support says how far an option, module or solution is backed by the
|
||||
// real programs (RULE-03). Only Supported, Experimental, Untested and
|
||||
// Discouraged can be selected; the others are shown with their reason.
|
||||
type Support string
|
||||
|
||||
const (
|
||||
Supported Support = "supported"
|
||||
Experimental Support = "experimental"
|
||||
Untested Support = "untested"
|
||||
Discouraged Support = "discouraged"
|
||||
NeedsPrereq Support = "needs-prerequisites"
|
||||
NotImplemented Support = "not-implemented"
|
||||
NotSupported Support = "not-supported"
|
||||
Impossible Support = "impossible"
|
||||
Insecure Support = "insecure"
|
||||
)
|
||||
|
||||
// Selectable reports whether an option with this support level may be chosen.
|
||||
func (s Support) Selectable() bool {
|
||||
switch s {
|
||||
case "", Supported, Experimental, Untested, Discouraged, NeedsPrereq:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Valid reports whether s is a known support level ("" means supported).
|
||||
func (s Support) Valid() bool {
|
||||
switch s {
|
||||
case "", Supported, Experimental, Untested, Discouraged, NeedsPrereq,
|
||||
NotImplemented, NotSupported, Impossible, Insecure:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Text is a translatable string set: the English base plus translations.
|
||||
type Translations map[string]map[string]string // lang -> field -> text
|
||||
|
||||
// Meta is the common metadata block.
|
||||
type Meta struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Name string `yaml:"name,omitempty" json:"name,omitempty"` // presets/instances
|
||||
Package string `yaml:"package,omitempty" json:"package,omitempty"`
|
||||
Version string `yaml:"version,omitempty" json:"version,omitempty"`
|
||||
Title string `yaml:"title,omitempty" json:"title,omitempty"`
|
||||
Summary string `yaml:"summary,omitempty" json:"summary,omitempty"`
|
||||
Description string `yaml:"description,omitempty" json:"description,omitempty"`
|
||||
Category string `yaml:"category,omitempty" json:"category,omitempty"`
|
||||
Tags []string `yaml:"tags,omitempty" json:"tags,omitempty"`
|
||||
Synonyms []string `yaml:"synonyms,omitempty" json:"synonyms,omitempty"`
|
||||
Difficulty string `yaml:"difficulty,omitempty" json:"difficulty,omitempty"`
|
||||
Status Support `yaml:"status,omitempty" json:"status,omitempty"`
|
||||
Audience []string `yaml:"audience,omitempty" json:"audience,omitempty"`
|
||||
Featured bool `yaml:"featured,omitempty" json:"featured,omitempty"`
|
||||
Origin string `yaml:"origin,omitempty" json:"origin,omitempty"` // official | local | org | community | ai
|
||||
Aliases []string `yaml:"aliases,omitempty" json:"aliases,omitempty"`
|
||||
Translations Translations `yaml:"translations,omitempty" json:"translations,omitempty"`
|
||||
}
|
||||
|
||||
// Header is the envelope every manifest file has.
|
||||
type Header struct {
|
||||
APIVersion string `yaml:"apiVersion"`
|
||||
Kind string `yaml:"kind"`
|
||||
Metadata Meta `yaml:"metadata"`
|
||||
Spec yaml.Node `yaml:"spec"`
|
||||
}
|
||||
|
||||
// ---- Catalog root -------------------------------------------------------
|
||||
|
||||
// CatalogSpec is catalog.yaml: where the other sources are.
|
||||
type CatalogSpec struct {
|
||||
// BigPicture is the directory with the Big Picture manifest.yaml,
|
||||
// relative to catalog.yaml.
|
||||
BigPicture string `yaml:"bigpicture"`
|
||||
// SourcesRoot is where repositories named in knowledge sources are
|
||||
// checked out, relative to catalog.yaml (optional; manuals are then
|
||||
// only referenced, not indexed).
|
||||
SourcesRoot string `yaml:"sourcesRoot,omitempty"`
|
||||
// WebBase resolves web references (https://yetano.app/).
|
||||
WebBase string `yaml:"webBase"`
|
||||
// Categories maps Big Picture topics to guide categories (order = display order).
|
||||
Categories []Category `yaml:"categories"`
|
||||
// Locales the catalog is (partly) translated into; English is the base.
|
||||
Locales []string `yaml:"locales,omitempty"`
|
||||
}
|
||||
|
||||
// Category is a guide category, normally one Big Picture topic.
|
||||
type Category struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Topic string `yaml:"topic,omitempty" json:"topic,omitempty"`
|
||||
Title string `yaml:"title,omitempty" json:"title,omitempty"`
|
||||
Summary string `yaml:"summary,omitempty" json:"summary,omitempty"`
|
||||
Translations Translations `yaml:"translations,omitempty" json:"translations,omitempty"`
|
||||
}
|
||||
|
||||
// ---- Capabilities ---------------------------------------------------------
|
||||
|
||||
// Capability is an abstract ability (mail.smtp.server); see CAPABILITIES.md.
|
||||
type Capability struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Description string `yaml:"description,omitempty" json:"description,omitempty"`
|
||||
Requires []string `yaml:"requires,omitempty" json:"requires,omitempty"`
|
||||
Conflicts []string `yaml:"conflicts,omitempty" json:"conflicts,omitempty"`
|
||||
Translations Translations `yaml:"translations,omitempty" json:"translations,omitempty"`
|
||||
// Providers outside Yetano (an existing LDAP, PostgreSQL, ...).
|
||||
External []ExternalProvider `yaml:"external,omitempty" json:"external,omitempty"`
|
||||
}
|
||||
|
||||
// ExternalProvider is a provider that is not a Yetano package.
|
||||
type ExternalProvider struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Description string `yaml:"description,omitempty" json:"description,omitempty"`
|
||||
Support Support `yaml:"support,omitempty" json:"support,omitempty"`
|
||||
}
|
||||
|
||||
// CapabilitiesSpec is a capabilities file.
|
||||
type CapabilitiesSpec struct {
|
||||
Capabilities []Capability `yaml:"capabilities"`
|
||||
}
|
||||
|
||||
// ---- Packages -------------------------------------------------------------
|
||||
|
||||
// PackageSpec describes a Yetano package for the guide. Its human summary
|
||||
// comes from the Big Picture program (BigPicture), never copied here.
|
||||
type PackageSpec struct {
|
||||
BigPicture string `yaml:"bigpicture,omitempty" json:"bigpicture,omitempty"` // product.<name>
|
||||
Registry string `yaml:"registry,omitempty" json:"registry,omitempty"` // yetano.app/yetano/<pkg>
|
||||
Microsite string `yaml:"microsite,omitempty" json:"microsite,omitempty"` // path below webBase
|
||||
Platforms []string `yaml:"platforms,omitempty" json:"platforms,omitempty"`
|
||||
Modules []Module `yaml:"modules" json:"modules"`
|
||||
// Sources back every claim about what the package can do (ANA-02).
|
||||
Sources []string `yaml:"sources" json:"sources"`
|
||||
}
|
||||
|
||||
// Module is one functional part of a package (a yamase-* program, ...).
|
||||
type Module struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Description string `yaml:"description,omitempty" json:"description,omitempty"`
|
||||
Program string `yaml:"program,omitempty" json:"program,omitempty"` // binary name
|
||||
Kind string `yaml:"kind,omitempty" json:"kind,omitempty"` // daemon | cli | plugin | library
|
||||
Provides []string `yaml:"provides,omitempty" json:"provides,omitempty"`
|
||||
Requires []string `yaml:"requires,omitempty" json:"requires,omitempty"` // capabilities
|
||||
Support Support `yaml:"support,omitempty" json:"support,omitempty"`
|
||||
// Priority orders providers of the same capability (higher first).
|
||||
Priority int `yaml:"priority,omitempty" json:"priority,omitempty"`
|
||||
// Manual is the knowledge source with this module's manual, if any.
|
||||
Manual string `yaml:"manual,omitempty" json:"manual,omitempty"`
|
||||
Doctor []string `yaml:"doctor,omitempty" json:"doctor,omitempty"` // commands that check it
|
||||
Sources []string `yaml:"sources,omitempty" json:"sources,omitempty"`
|
||||
Translations Translations `yaml:"translations,omitempty" json:"translations,omitempty"`
|
||||
}
|
||||
|
||||
// ---- Profiles -------------------------------------------------------------
|
||||
|
||||
// ProfileSpec is a recommended way to run one package.
|
||||
type ProfileSpec struct {
|
||||
Extends string `yaml:"extends,omitempty" json:"extends,omitempty"`
|
||||
Modules map[string]string `yaml:"modules,omitempty" json:"modules,omitempty"` // module -> enabled|disabled
|
||||
Parameters []string `yaml:"parameters,omitempty" json:"parameters,omitempty"`
|
||||
Config []Fragment `yaml:"config,omitempty" json:"config,omitempty"`
|
||||
Recommendations []Recommendation `yaml:"recommendations,omitempty" json:"recommendations,omitempty"`
|
||||
Platforms []string `yaml:"platforms,omitempty" json:"platforms,omitempty"`
|
||||
References []string `yaml:"references,omitempty" json:"references,omitempty"`
|
||||
}
|
||||
|
||||
// Recommendation names something worth adding, with the reason.
|
||||
type Recommendation struct {
|
||||
Package string `yaml:"package,omitempty" json:"package,omitempty"`
|
||||
Capability string `yaml:"capability,omitempty" json:"capability,omitempty"`
|
||||
Reason string `yaml:"reason" json:"reason"`
|
||||
}
|
||||
|
||||
// Fragment is a typed YAML piece for one generated file (ADR-0005).
|
||||
type Fragment struct {
|
||||
ID string `yaml:"id,omitempty" json:"id,omitempty"`
|
||||
File string `yaml:"file" json:"file"`
|
||||
// Module: the fragment only applies while this module of the file's
|
||||
// package is enabled (a disabled program gets no configuration).
|
||||
Module string `yaml:"module,omitempty" json:"module,omitempty"`
|
||||
When Cond `yaml:"when,omitempty" json:"when,omitempty"`
|
||||
Set yaml.Node `yaml:"set" json:"-"`
|
||||
}
|
||||
|
||||
// ---- Solutions and tasks ----------------------------------------------------
|
||||
|
||||
// SolutionSpec is a complete use case. Tasks use the same structure.
|
||||
type SolutionSpec struct {
|
||||
BigPicture string `yaml:"bigpicture,omitempty" json:"bigpicture,omitempty"`
|
||||
Kind string `yaml:"kind,omitempty" json:"kind,omitempty"` // tasks: read-only|generate|diagnostic|planning|mutating|deployment|maintenance
|
||||
Goals []string `yaml:"goals,omitempty" json:"goals,omitempty"`
|
||||
Scenarios []string `yaml:"scenarios,omitempty" json:"scenarios,omitempty"`
|
||||
NotFor []string `yaml:"notFor,omitempty" json:"notFor,omitempty"`
|
||||
AppliesTo *AppliesTo `yaml:"appliesTo,omitempty" json:"appliesTo,omitempty"`
|
||||
Capabilities CapSets `yaml:"capabilities,omitempty" json:"capabilities"`
|
||||
Packages []PackageUse `yaml:"packages,omitempty" json:"packages"`
|
||||
Questions []Question `yaml:"questions,omitempty" json:"questions"`
|
||||
Rules []Rule `yaml:"rules,omitempty" json:"rules,omitempty"`
|
||||
Config []Fragment `yaml:"configuration,omitempty" json:"-"`
|
||||
Steps []Step `yaml:"steps,omitempty" json:"steps,omitempty"`
|
||||
Requirements []Check `yaml:"requirements,omitempty" json:"requirements,omitempty"`
|
||||
Architecture Architecture `yaml:"architecture,omitempty" json:"architecture"`
|
||||
References References `yaml:"references,omitempty" json:"references"`
|
||||
Related []string `yaml:"related,omitempty" json:"related,omitempty"`
|
||||
Operations []string `yaml:"operations,omitempty" json:"operations,omitempty"`
|
||||
Lifecycle Lifecycle `yaml:"lifecycle,omitempty" json:"lifecycle"`
|
||||
Platforms []string `yaml:"platforms,omitempty" json:"platforms,omitempty"`
|
||||
Sources []string `yaml:"sources,omitempty" json:"sources,omitempty"`
|
||||
}
|
||||
|
||||
// AppliesTo limits a task to solutions/capabilities (applicableWhen).
|
||||
type AppliesTo struct {
|
||||
Solutions []string `yaml:"solutions,omitempty" json:"solutions,omitempty"`
|
||||
Capabilities []string `yaml:"capabilities,omitempty" json:"capabilities,omitempty"`
|
||||
}
|
||||
|
||||
// CapSets are the capabilities a solution needs.
|
||||
type CapSets struct {
|
||||
Required []string `yaml:"required,omitempty" json:"required,omitempty"`
|
||||
Recommended []string `yaml:"recommended,omitempty" json:"recommended,omitempty"`
|
||||
Optional []string `yaml:"optional,omitempty" json:"optional,omitempty"`
|
||||
}
|
||||
|
||||
// PackageUse puts a package into a solution, optionally under a condition.
|
||||
type PackageUse struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Profile string `yaml:"profile,omitempty" json:"profile,omitempty"`
|
||||
When Cond `yaml:"when,omitempty" json:"when,omitempty"`
|
||||
Role string `yaml:"role,omitempty" json:"role,omitempty"` // core | optional
|
||||
Recommended bool `yaml:"recommended,omitempty" json:"recommended,omitempty"`
|
||||
Reason string `yaml:"reason,omitempty" json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// Level orders the wizard modes.
|
||||
type Level string
|
||||
|
||||
const (
|
||||
LevelQuick Level = "quick"
|
||||
LevelGuided Level = "guided"
|
||||
LevelAdvanced Level = "advanced"
|
||||
)
|
||||
|
||||
// Rank is the order of the modes (quick < guided < advanced).
|
||||
func (l Level) Rank() int {
|
||||
switch l {
|
||||
case LevelGuided:
|
||||
return 1
|
||||
case LevelAdvanced:
|
||||
return 2
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Question is one wizard question.
|
||||
type Question struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Type string `yaml:"type" json:"type"`
|
||||
Item string `yaml:"item,omitempty" json:"item,omitempty"` // list element type
|
||||
Group string `yaml:"group,omitempty" json:"group,omitempty"`
|
||||
Level Level `yaml:"level,omitempty" json:"level,omitempty"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Help string `yaml:"help,omitempty" json:"help,omitempty"`
|
||||
Description string `yaml:"description,omitempty" json:"description,omitempty"`
|
||||
Decide string `yaml:"decide,omitempty" json:"decide,omitempty"` // "Help me decide" text
|
||||
Placeholder string `yaml:"placeholder,omitempty" json:"placeholder,omitempty"`
|
||||
Examples []string `yaml:"examples,omitempty" json:"examples,omitempty"`
|
||||
Default any `yaml:"default,omitempty" json:"default,omitempty"`
|
||||
DefaultWhen []CondValue `yaml:"defaultWhen,omitempty" json:"defaultWhen,omitempty"`
|
||||
Recommended any `yaml:"recommended,omitempty" json:"recommended,omitempty"`
|
||||
Reason string `yaml:"reason,omitempty" json:"reason,omitempty"`
|
||||
Required bool `yaml:"required,omitempty" json:"required,omitempty"`
|
||||
RequiredWhen Cond `yaml:"requiredWhen,omitempty" json:"requiredWhen,omitempty"`
|
||||
VisibleWhen Cond `yaml:"visibleWhen,omitempty" json:"visibleWhen,omitempty"`
|
||||
InstanceSpecific bool `yaml:"instanceSpecific,omitempty" json:"instanceSpecific,omitempty"`
|
||||
Options []Option `yaml:"options,omitempty" json:"options,omitempty"`
|
||||
Validation Validation `yaml:"validation,omitempty" json:"validation"`
|
||||
ValidateWhen []CondCheck `yaml:"validateWhen,omitempty" json:"validateWhen,omitempty"`
|
||||
References []string `yaml:"references,omitempty" json:"references,omitempty"`
|
||||
Warnings []CondText `yaml:"warnings,omitempty" json:"warnings,omitempty"`
|
||||
Platforms []string `yaml:"platforms,omitempty" json:"platforms,omitempty"`
|
||||
Translations Translations `yaml:"translations,omitempty" json:"translations,omitempty"`
|
||||
}
|
||||
|
||||
// Option is one choice of a select/multiselect question.
|
||||
type Option struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Description string `yaml:"description,omitempty" json:"description,omitempty"`
|
||||
Recommended bool `yaml:"recommended,omitempty" json:"recommended,omitempty"`
|
||||
Reason string `yaml:"reason,omitempty" json:"reason,omitempty"`
|
||||
Support Support `yaml:"support,omitempty" json:"support,omitempty"`
|
||||
SupportNote string `yaml:"supportNote,omitempty" json:"supportNote,omitempty"`
|
||||
EnabledWhen Cond `yaml:"enabledWhen,omitempty" json:"enabledWhen,omitempty"`
|
||||
Effects Effects `yaml:",inline" json:"effects"`
|
||||
References []string `yaml:"references,omitempty" json:"references,omitempty"`
|
||||
Translations Translations `yaml:"translations,omitempty" json:"translations,omitempty"`
|
||||
}
|
||||
|
||||
// Effects are what a rule or a chosen option does (RULE-01).
|
||||
type Effects struct {
|
||||
Requires *Targets `yaml:"requires,omitempty" json:"requires,omitempty"`
|
||||
Excludes *Targets `yaml:"excludes,omitempty" json:"excludes,omitempty"`
|
||||
Conflicts *Targets `yaml:"conflicts,omitempty" json:"conflicts,omitempty"`
|
||||
Implies *Targets `yaml:"implies,omitempty" json:"implies,omitempty"`
|
||||
Recommends *Targets `yaml:"recommends,omitempty" json:"recommends,omitempty"`
|
||||
Discourages *Targets `yaml:"discourages,omitempty" json:"discourages,omitempty"`
|
||||
Provides []string `yaml:"provides,omitempty" json:"provides,omitempty"` // capabilities provided outside Yetano
|
||||
}
|
||||
|
||||
// Targets lists packages, capabilities, modules and option answers.
|
||||
type Targets struct {
|
||||
Packages []string `yaml:"packages,omitempty" json:"packages,omitempty"`
|
||||
Capabilities []string `yaml:"capabilities,omitempty" json:"capabilities,omitempty"`
|
||||
Modules []string `yaml:"modules,omitempty" json:"modules,omitempty"` // pkg/module
|
||||
Answers map[string]any `yaml:"answers,omitempty" json:"answers,omitempty"` // question -> option
|
||||
}
|
||||
|
||||
// Rule is a declarative rule of a solution or task.
|
||||
type Rule struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
When Cond `yaml:"when,omitempty" json:"when,omitempty"`
|
||||
Effects Effects `yaml:",inline" json:"effects"`
|
||||
Severity string `yaml:"severity,omitempty" json:"severity,omitempty"` // for warnings: info|warning|error
|
||||
Message string `yaml:"message,omitempty" json:"message,omitempty"`
|
||||
Reason string `yaml:"reason,omitempty" json:"reason,omitempty"`
|
||||
Source string `yaml:"source,omitempty" json:"source,omitempty"`
|
||||
}
|
||||
|
||||
// Validation constrains an answer.
|
||||
type Validation struct {
|
||||
Min *float64 `yaml:"min,omitempty" json:"min,omitempty"`
|
||||
Max *float64 `yaml:"max,omitempty" json:"max,omitempty"`
|
||||
MinItems int `yaml:"minItems,omitempty" json:"minItems,omitempty"`
|
||||
MaxItems int `yaml:"maxItems,omitempty" json:"maxItems,omitempty"`
|
||||
MinLength int `yaml:"minLength,omitempty" json:"minLength,omitempty"`
|
||||
MaxLength int `yaml:"maxLength,omitempty" json:"maxLength,omitempty"`
|
||||
Pattern string `yaml:"pattern,omitempty" json:"pattern,omitempty"`
|
||||
Message string `yaml:"message,omitempty" json:"message,omitempty"`
|
||||
}
|
||||
|
||||
// CondValue is a conditional default.
|
||||
type CondValue struct {
|
||||
When Cond `yaml:"when" json:"when"`
|
||||
Value any `yaml:"value" json:"value"`
|
||||
}
|
||||
|
||||
// CondText is a conditional message.
|
||||
type CondText struct {
|
||||
When Cond `yaml:"when,omitempty" json:"when,omitempty"`
|
||||
Severity string `yaml:"severity,omitempty" json:"severity,omitempty"`
|
||||
Text string `yaml:"text" json:"text"`
|
||||
}
|
||||
|
||||
// CondCheck is a conditional validation (validateWhen).
|
||||
type CondCheck struct {
|
||||
When Cond `yaml:"when" json:"when"`
|
||||
Validation Validation `yaml:"validation" json:"validation"`
|
||||
}
|
||||
|
||||
// Step is a setup step that is not configuration (yamase-ctl directory
|
||||
// domain add ...). The engine only lists steps; it never runs them.
|
||||
type Step struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
When Cond `yaml:"when,omitempty" json:"when,omitempty"`
|
||||
Package string `yaml:"package,omitempty" json:"package,omitempty"`
|
||||
Module string `yaml:"module,omitempty" json:"module,omitempty"`
|
||||
ForEach string `yaml:"forEach,omitempty" json:"forEach,omitempty"` // list parameter; ${item}
|
||||
Run string `yaml:"run,omitempty" json:"run,omitempty"`
|
||||
Note string `yaml:"note,omitempty" json:"note,omitempty"`
|
||||
Phase string `yaml:"phase,omitempty" json:"phase,omitempty"` // install | configure | data | dns | start | verify
|
||||
Mutating bool `yaml:"mutating,omitempty" json:"mutating,omitempty"`
|
||||
Reversible *bool `yaml:"reversible,omitempty" json:"reversible,omitempty"`
|
||||
References []string `yaml:"references,omitempty" json:"references,omitempty"`
|
||||
}
|
||||
|
||||
// Check is a requirement or doctor check (DR-01).
|
||||
type Check struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Title string `yaml:"title" json:"title"`
|
||||
Kind string `yaml:"kind" json:"kind"` // os | arch | port | dns | tls | program | command | manual
|
||||
When Cond `yaml:"when,omitempty" json:"when,omitempty"`
|
||||
Severity string `yaml:"severity,omitempty" json:"severity,omitempty"` // error | warning | info
|
||||
Local bool `yaml:"local,omitempty" json:"local,omitempty"` // needs the target system
|
||||
Params map[string]string `yaml:"params,omitempty" json:"params,omitempty"`
|
||||
Run string `yaml:"run,omitempty" json:"run,omitempty"` // command the operator can run
|
||||
Hint string `yaml:"hint,omitempty" json:"hint,omitempty"`
|
||||
References []string `yaml:"references,omitempty" json:"references,omitempty"`
|
||||
}
|
||||
|
||||
// Architecture adds solution-specific nodes and edges to the resolved graph.
|
||||
type Architecture struct {
|
||||
Nodes []ArchNode `yaml:"nodes,omitempty" json:"nodes,omitempty"`
|
||||
Edges []ArchEdge `yaml:"edges,omitempty" json:"edges,omitempty"`
|
||||
}
|
||||
|
||||
// ArchNode is an extra node (clients, external services, storage, ...).
|
||||
type ArchNode struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Type string `yaml:"type" json:"type"` // client | external | storage | network | identity | target
|
||||
Title string `yaml:"title" json:"title"`
|
||||
When Cond `yaml:"when,omitempty" json:"when,omitempty"`
|
||||
}
|
||||
|
||||
// ArchEdge connects two nodes; IDs are node IDs, "pkg:<id>", "mod:<pkg>/<id>" or "cap:<id>".
|
||||
type ArchEdge struct {
|
||||
From string `yaml:"from" json:"from"`
|
||||
To string `yaml:"to" json:"to"`
|
||||
Type string `yaml:"type" json:"type"`
|
||||
Label string `yaml:"label,omitempty" json:"label,omitempty"`
|
||||
When Cond `yaml:"when,omitempty" json:"when,omitempty"`
|
||||
}
|
||||
|
||||
// References are typed yetano: references.
|
||||
type References struct {
|
||||
Manuals []string `yaml:"manuals,omitempty" json:"manuals,omitempty"`
|
||||
Microsites []string `yaml:"microsites,omitempty" json:"microsites,omitempty"`
|
||||
BigPicture []string `yaml:"bigpicture,omitempty" json:"bigpicture,omitempty"`
|
||||
Other []string `yaml:"other,omitempty" json:"other,omitempty"`
|
||||
}
|
||||
|
||||
// All returns every reference in a stable order.
|
||||
func (r References) All() []string {
|
||||
var out []string
|
||||
out = append(out, r.Manuals...)
|
||||
out = append(out, r.Microsites...)
|
||||
out = append(out, r.BigPicture...)
|
||||
out = append(out, r.Other...)
|
||||
return out
|
||||
}
|
||||
|
||||
// Lifecycle says which lifecycle operations a solution supports.
|
||||
type Lifecycle struct {
|
||||
Configure bool `yaml:"configure,omitempty" json:"configure"`
|
||||
Validate bool `yaml:"validate,omitempty" json:"validate"`
|
||||
Plan bool `yaml:"plan,omitempty" json:"plan"`
|
||||
Deploy string `yaml:"deploy,omitempty" json:"deploy,omitempty"` // no | optional | yes
|
||||
Doctor bool `yaml:"doctor,omitempty" json:"doctor"`
|
||||
Update string `yaml:"update,omitempty" json:"update,omitempty"`
|
||||
}
|
||||
|
||||
// ---- Examples, presets, instances ------------------------------------------
|
||||
|
||||
// ExampleSpec is a curated answer set for a solution (EX-01). Examples
|
||||
// are generated by the engine like any session, so they cannot drift.
|
||||
type ExampleSpec struct {
|
||||
Solution string `yaml:"solution" json:"solution"`
|
||||
Mode Level `yaml:"mode,omitempty" json:"mode,omitempty"`
|
||||
Scenario string `yaml:"scenario,omitempty" json:"scenario,omitempty"`
|
||||
Parameters map[string]any `yaml:"parameters" json:"parameters"`
|
||||
Tested bool `yaml:"tested,omitempty" json:"tested"`
|
||||
}
|
||||
|
||||
// ---- Knowledge sources --------------------------------------------------------
|
||||
|
||||
// KnowledgeSourceSpec points at existing documentation (KNOW-04/06): the
|
||||
// documents stay where they are; sections are extracted from headings.
|
||||
type KnowledgeSourceSpec struct {
|
||||
Package string `yaml:"package" json:"package"`
|
||||
Type string `yaml:"type" json:"type"` // manual
|
||||
Format string `yaml:"format" json:"format"` // markdown
|
||||
Repository string `yaml:"repository" json:"repository"`
|
||||
Root string `yaml:"root,omitempty" json:"root,omitempty"` // dir with README.md and docs/
|
||||
Program string `yaml:"program,omitempty" json:"program,omitempty"`
|
||||
Layout string `yaml:"layout,omitempty" json:"layout,omitempty"` // core-manual (README + docs/**) | docs-tree
|
||||
Web string `yaml:"web,omitempty" json:"web,omitempty"` // URL base for topics, {slug} placeholder
|
||||
Exclude []string `yaml:"exclude,omitempty" json:"exclude,omitempty"`
|
||||
Topics []SourceTopic `yaml:"topics,omitempty" json:"topics,omitempty"`
|
||||
}
|
||||
|
||||
// SourceTopic attaches capabilities/modules to one manual topic.
|
||||
type SourceTopic struct {
|
||||
Slug string `yaml:"slug" json:"slug"`
|
||||
Capabilities []string `yaml:"capabilities,omitempty" json:"capabilities,omitempty"`
|
||||
Modules []string `yaml:"modules,omitempty" json:"modules,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// SchemaKinds maps a schema file name to the manifest it describes.
|
||||
var SchemaKinds = map[string]struct {
|
||||
Kind string
|
||||
Spec any
|
||||
}{
|
||||
"catalog": {KindCatalog, CatalogSpec{}},
|
||||
"capabilities": {KindCapabilities, CapabilitiesSpec{}},
|
||||
"package": {KindPackage, PackageSpec{}},
|
||||
"profile": {KindProfile, ProfileSpec{}},
|
||||
"solution": {KindSolution, SolutionSpec{}},
|
||||
"example": {KindExample, ExampleSpec{}},
|
||||
"knowledge-source": {KindKnowledgeSource, KnowledgeSourceSpec{}},
|
||||
}
|
||||
|
||||
// JSONSchema returns the JSON Schema (draft 2020-12) of a manifest kind,
|
||||
// derived from the Go types the loader decodes with KnownFields -- so the
|
||||
// editor's check and the loader cannot disagree (CAT-05). Several
|
||||
// documents per file ("---") are not expressible in JSON Schema; the
|
||||
// schema describes one document. Solution and Task share a schema.
|
||||
func JSONSchema(name string) map[string]any {
|
||||
k := SchemaKinds[name]
|
||||
kinds := []any{k.Kind}
|
||||
if k.Kind == KindSolution {
|
||||
kinds = append(kinds, KindTask)
|
||||
}
|
||||
return map[string]any{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://yetano.app/schema/solutions/" + name + ".schema.json",
|
||||
"title": "Yetano " + k.Kind + " manifest",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": []any{"apiVersion", "kind", "metadata"},
|
||||
"properties": map[string]any{
|
||||
"apiVersion": map[string]any{"const": APIVersion},
|
||||
"kind": map[string]any{"enum": kinds},
|
||||
"metadata": schemaOf(reflect.TypeOf(Meta{}), 0),
|
||||
"spec": schemaOf(reflect.TypeOf(k.Spec), 0),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
nodeType = reflect.TypeOf(yaml.Node{})
|
||||
condType = reflect.TypeOf(Cond{})
|
||||
)
|
||||
|
||||
func schemaOf(t reflect.Type, depth int) map[string]any {
|
||||
if depth > 12 {
|
||||
return map[string]any{}
|
||||
}
|
||||
if t.Kind() == reflect.Pointer {
|
||||
t = t.Elem()
|
||||
}
|
||||
switch {
|
||||
case t == nodeType:
|
||||
return map[string]any{"description": "native configuration (any YAML)"}
|
||||
case t == condType:
|
||||
return map[string]any{"type": "object", "description": "condition over earlier answers (ADR-0003)"}
|
||||
case t == reflect.TypeOf(Support("")):
|
||||
return map[string]any{"enum": []any{"supported", "experimental", "untested", "discouraged", "needs-prerequisites", "not-implemented", "not-supported", "impossible", "insecure"}}
|
||||
case t == reflect.TypeOf(Level("")):
|
||||
return map[string]any{"enum": []any{"quick", "guided", "advanced"}}
|
||||
}
|
||||
switch t.Kind() {
|
||||
case reflect.String:
|
||||
return map[string]any{"type": "string"}
|
||||
case reflect.Bool:
|
||||
return map[string]any{"type": "boolean"}
|
||||
case reflect.Int, reflect.Int64:
|
||||
return map[string]any{"type": "integer"}
|
||||
case reflect.Float64:
|
||||
return map[string]any{"type": "number"}
|
||||
case reflect.Slice:
|
||||
return map[string]any{"type": "array", "items": schemaOf(t.Elem(), depth+1)}
|
||||
case reflect.Map:
|
||||
return map[string]any{"type": "object", "additionalProperties": schemaOf(t.Elem(), depth+1)}
|
||||
case reflect.Interface:
|
||||
return map[string]any{}
|
||||
case reflect.Struct:
|
||||
props := map[string]any{}
|
||||
addFields(t, props, depth)
|
||||
return map[string]any{"type": "object", "additionalProperties": false, "properties": props}
|
||||
}
|
||||
return map[string]any{}
|
||||
}
|
||||
|
||||
func addFields(t reflect.Type, props map[string]any, depth int) {
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
if !f.IsExported() {
|
||||
continue
|
||||
}
|
||||
tag := f.Tag.Get("yaml")
|
||||
name, opts, _ := strings.Cut(tag, ",")
|
||||
if name == "-" {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(opts, "inline") {
|
||||
addFields(f.Type, props, depth)
|
||||
continue
|
||||
}
|
||||
if name == "" {
|
||||
name = strings.ToLower(f.Name)
|
||||
}
|
||||
props[name] = schemaOf(f.Type, depth+1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,548 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// QuestionTypes are the built-in question types (WIZ-03).
|
||||
var QuestionTypes = map[string]bool{
|
||||
"boolean": true, "select": true, "multiselect": true,
|
||||
"string": true, "text": true, "integer": true, "decimal": true,
|
||||
"date": true, "duration": true, "hostname": true, "domain": true,
|
||||
"email": true, "ip": true, "cidr": true, "port": true, "url": true,
|
||||
"path": true, "dir": true, "list": true, "keyvalue": true,
|
||||
"package": true, "profile": true, "capability": true, "provider": true,
|
||||
"target": true, "secret": true,
|
||||
}
|
||||
|
||||
// ListItemTypes may be used as list items.
|
||||
var ListItemTypes = map[string]bool{
|
||||
"string": true, "hostname": true, "domain": true, "email": true, "ip": true,
|
||||
"cidr": true, "port": true, "url": true, "path": true, "integer": true,
|
||||
}
|
||||
|
||||
var idRE = regexp.MustCompile(`^[a-z0-9][a-z0-9.-]*[a-z0-9]$|^[a-z0-9]$`)
|
||||
var questionIDRE = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]*$`)
|
||||
|
||||
// SafeRelPath reports whether p is a clean relative slash path without
|
||||
// "..", usable as an output file name (SEC-03).
|
||||
func SafeRelPath(p string) bool {
|
||||
if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, "\\") || strings.Contains(p, ":") {
|
||||
return false
|
||||
}
|
||||
if path.Clean(p) != p {
|
||||
return false
|
||||
}
|
||||
for _, part := range strings.Split(p, "/") {
|
||||
if part == ".." || part == "." || part == "" || strings.HasPrefix(part, ".") {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Validate checks the catalog's internal consistency and returns all
|
||||
// issues (load issues included). Reference targets outside the catalog
|
||||
// (manual sections, microsites) are checked by knowledge.Lint.
|
||||
func (c *Catalog) Validate() []Issue {
|
||||
v := &validator{c: c, issues: append([]Issue(nil), c.Issues...)}
|
||||
v.root()
|
||||
for _, id := range SortedIDs(c.Capabilities) {
|
||||
v.capability(c.Capabilities[id])
|
||||
}
|
||||
for _, id := range SortedIDs(c.Packages) {
|
||||
v.pkg(c.Packages[id])
|
||||
}
|
||||
for _, id := range SortedIDs(c.Profiles) {
|
||||
v.profile(c.Profiles[id])
|
||||
}
|
||||
for _, id := range SortedIDs(c.Solutions) {
|
||||
v.solution(c.Solutions[id])
|
||||
}
|
||||
for _, id := range SortedIDs(c.Tasks) {
|
||||
v.solution(c.Tasks[id])
|
||||
}
|
||||
for _, id := range SortedIDs(c.Examples) {
|
||||
v.example(c.Examples[id])
|
||||
}
|
||||
for _, id := range SortedIDs(c.Sources) {
|
||||
v.source(c.Sources[id])
|
||||
}
|
||||
return v.issues
|
||||
}
|
||||
|
||||
type validator struct {
|
||||
c *Catalog
|
||||
issues []Issue
|
||||
}
|
||||
|
||||
func (v *validator) errf(e *Entity, id, code, f string, a ...any) {
|
||||
i := Issue{Severity: "error", ID: id, Code: code, Message: fmt.Sprintf(f, a...)}
|
||||
if e != nil {
|
||||
i.File, i.Line = e.File, e.Line
|
||||
}
|
||||
v.issues = append(v.issues, i)
|
||||
}
|
||||
|
||||
func (v *validator) warnf(e *Entity, id, code, f string, a ...any) {
|
||||
i := Issue{Severity: "warning", ID: id, Code: code, Message: fmt.Sprintf(f, a...)}
|
||||
if e != nil {
|
||||
i.File, i.Line = e.File, e.Line
|
||||
}
|
||||
v.issues = append(v.issues, i)
|
||||
}
|
||||
|
||||
func (v *validator) root() {
|
||||
c := v.c
|
||||
if c.Spec.WebBase == "" {
|
||||
v.errf(nil, "catalog", "web-base", "catalog.yaml: spec.webBase is required")
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, cat := range c.Spec.Categories {
|
||||
if seen[cat.ID] {
|
||||
v.errf(nil, cat.ID, "duplicate", "duplicate category %q", cat.ID)
|
||||
}
|
||||
seen[cat.ID] = true
|
||||
if cat.Topic != "" && c.BigPicture != nil {
|
||||
if _, ok := c.BigPicture.Topics[cat.Topic]; !ok {
|
||||
v.errf(nil, cat.ID, "bigpicture-ref", "category %q: unknown Big Picture topic %q", cat.ID, cat.Topic)
|
||||
}
|
||||
}
|
||||
if cat.Topic == "" && cat.Title == "" {
|
||||
v.errf(nil, cat.ID, "category", "category %q needs a topic or a title", cat.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) capability(cp *Capability) {
|
||||
e := &Entity{File: v.c.capFile[cp.ID]}
|
||||
if !idRE.MatchString(cp.ID) || strings.Contains(cp.ID, "@") {
|
||||
v.errf(e, cp.ID, "id", "capability ID %q: lower case, dot separated, no '@' (ADR-0003)", cp.ID)
|
||||
}
|
||||
if cp.Title == "" {
|
||||
v.errf(e, cp.ID, "required", "capability %q: title is required", cp.ID)
|
||||
}
|
||||
for _, r := range append(append([]string{}, cp.Requires...), cp.Conflicts...) {
|
||||
if _, ok := v.c.Capabilities[r]; !ok {
|
||||
v.errf(e, cp.ID, "capability-ref", "capability %q names unknown capability %q", cp.ID, r)
|
||||
}
|
||||
}
|
||||
for _, x := range cp.External {
|
||||
if !x.Support.Valid() {
|
||||
v.errf(e, cp.ID, "support", "external provider %q: unknown support %q", x.ID, x.Support)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) caps(e *Entity, id string, list []string) {
|
||||
for _, cp := range list {
|
||||
if _, ok := v.c.Capabilities[cp]; !ok {
|
||||
v.errf(e, id, "capability-ref", "unknown capability %q", cp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) pkg(p *Package) {
|
||||
e := &p.Entity
|
||||
if !idRE.MatchString(p.Meta.ID) {
|
||||
v.errf(e, p.Meta.ID, "id", "package ID %q is not valid", p.Meta.ID)
|
||||
}
|
||||
if p.Spec.BigPicture != "" && v.c.BigPicture != nil {
|
||||
if _, ok := v.c.BigPicture.Products[p.Spec.BigPicture]; !ok {
|
||||
v.errf(e, p.Meta.ID, "bigpicture-ref", "unknown Big Picture program %q", p.Spec.BigPicture)
|
||||
}
|
||||
}
|
||||
if p.Spec.BigPicture == "" && p.Meta.Summary == "" {
|
||||
v.errf(e, p.Meta.ID, "required", "package needs spec.bigpicture or metadata.summary")
|
||||
}
|
||||
if len(p.Spec.Sources) == 0 {
|
||||
v.errf(e, p.Meta.ID, "sources", "package claims need sources (ANA-02)")
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, m := range p.Spec.Modules {
|
||||
if seen[m.ID] {
|
||||
v.errf(e, p.Meta.ID, "duplicate", "duplicate module %q", m.ID)
|
||||
}
|
||||
seen[m.ID] = true
|
||||
if !m.Support.Valid() {
|
||||
v.errf(e, p.Meta.ID, "support", "module %q: unknown support %q", m.ID, m.Support)
|
||||
}
|
||||
v.caps(e, p.Meta.ID+"/"+m.ID, m.Provides)
|
||||
v.caps(e, p.Meta.ID+"/"+m.ID, m.Requires)
|
||||
if m.Manual != "" {
|
||||
if _, ok := v.c.Sources[m.Manual]; !ok {
|
||||
v.errf(e, p.Meta.ID+"/"+m.ID, "source-ref", "unknown knowledge source %q", m.Manual)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) profile(p *Profile) {
|
||||
e := &p.Entity
|
||||
pkg, ok := v.c.Packages[p.Meta.Package]
|
||||
if !ok {
|
||||
v.errf(e, p.Key(), "package-ref", "profile of unknown package %q", p.Meta.Package)
|
||||
return
|
||||
}
|
||||
if _, err := v.c.ProfileChain(p.Meta.Package, p.Meta.ID); err != nil {
|
||||
v.errf(e, p.Key(), "extends", "%v", err)
|
||||
}
|
||||
for m, state := range p.Spec.Modules {
|
||||
if pkg.Module(m) == nil {
|
||||
v.errf(e, p.Key(), "module-ref", "unknown module %q", m)
|
||||
}
|
||||
if state != "enabled" && state != "disabled" {
|
||||
v.errf(e, p.Key(), "module-state", "module %q: want enabled or disabled, got %q", m, state)
|
||||
}
|
||||
}
|
||||
for _, f := range p.Spec.Config {
|
||||
v.fragment(e, p.Key(), f, nil, 0)
|
||||
}
|
||||
for _, r := range p.Spec.Recommendations {
|
||||
if r.Package != "" {
|
||||
if _, ok := v.c.Packages[r.Package]; !ok {
|
||||
v.errf(e, p.Key(), "package-ref", "recommendation names unknown package %q", r.Package)
|
||||
}
|
||||
}
|
||||
if r.Capability != "" {
|
||||
v.caps(e, p.Key(), []string{r.Capability})
|
||||
}
|
||||
if r.Reason == "" {
|
||||
v.errf(e, p.Key(), "reason", "a recommendation needs a reason (EX-03)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) fragment(e *Entity, id string, f Fragment, s *Solution, upto int) {
|
||||
if !SafeRelPath(f.File) {
|
||||
v.errf(e, id, "path", "fragment file %q: want a clean relative path", f.File)
|
||||
}
|
||||
if f.Set.Kind == 0 {
|
||||
v.errf(e, id, "fragment", "fragment for %q has no set", f.File)
|
||||
}
|
||||
if s != nil {
|
||||
v.cond(e, id, "fragment "+f.File, f.When, s, upto)
|
||||
} else if len(f.When) > 0 {
|
||||
v.errf(e, id, "fragment", "profile fragments cannot have conditions")
|
||||
}
|
||||
}
|
||||
|
||||
// cond checks a condition: well-formed and only naming questions before
|
||||
// index upto (ADR-0003).
|
||||
func (v *validator) cond(e *Entity, id, where string, c Cond, s *Solution, upto int) {
|
||||
if len(c) == 0 {
|
||||
return
|
||||
}
|
||||
if _, err := c.Eval(Env{Answers: map[string]any{}}); err != nil {
|
||||
v.errf(e, id, "condition", "%s: %v", where, err)
|
||||
}
|
||||
for _, ref := range c.Refs() {
|
||||
_, idx := s.Question(ref)
|
||||
if idx < 0 {
|
||||
v.errf(e, id, "condition", "%s: unknown question %q", where, ref)
|
||||
} else if idx >= upto {
|
||||
v.errf(e, id, "condition-order", "%s: names question %q, which is not asked before it", where, ref)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) targets(e *Entity, id string, t *Targets, s *Solution) {
|
||||
if t == nil {
|
||||
return
|
||||
}
|
||||
for _, p := range t.Packages {
|
||||
if _, ok := v.c.Packages[p]; !ok {
|
||||
v.errf(e, id, "package-ref", "unknown package %q", p)
|
||||
}
|
||||
}
|
||||
v.caps(e, id, t.Capabilities)
|
||||
for _, m := range t.Modules {
|
||||
pkg, mod, _ := strings.Cut(m, "/")
|
||||
p, ok := v.c.Packages[pkg]
|
||||
if !ok || p.Module(mod) == nil {
|
||||
v.errf(e, id, "module-ref", "unknown module %q (want package/module)", m)
|
||||
}
|
||||
}
|
||||
for q, val := range t.Answers {
|
||||
qq, _ := s.Question(q)
|
||||
if qq == nil {
|
||||
v.errf(e, id, "question-ref", "unknown question %q", q)
|
||||
continue
|
||||
}
|
||||
v.optionValue(e, id, qq, val)
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) optionValue(e *Entity, id string, q *Question, val any) {
|
||||
if q.Type != "select" && q.Type != "multiselect" {
|
||||
return
|
||||
}
|
||||
vals := asList(val)
|
||||
if _, isList := val.([]any); !isList {
|
||||
vals = []any{val}
|
||||
}
|
||||
for _, x := range vals {
|
||||
found := false
|
||||
for _, o := range q.Options {
|
||||
if Equal(o.ID, x) {
|
||||
found = true
|
||||
if !o.Support.Selectable() {
|
||||
v.errf(e, id, "default-unselectable", "question %q: value %q is %s and cannot be selected", q.ID, Scalar(x), o.Support)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
v.errf(e, id, "option-ref", "question %q has no option %q", q.ID, Scalar(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) solution(s *Solution) {
|
||||
e := &s.Entity
|
||||
id := s.Meta.ID
|
||||
if !idRE.MatchString(id) {
|
||||
v.errf(e, id, "id", "ID %q is not valid", id)
|
||||
}
|
||||
if s.Meta.Version == "" {
|
||||
v.errf(e, id, "version", "metadata.version is required (LOCK)")
|
||||
}
|
||||
if !s.Meta.Status.Valid() {
|
||||
v.errf(e, id, "support", "unknown status %q", s.Meta.Status)
|
||||
}
|
||||
if s.Spec.BigPicture != "" && v.c.BigPicture != nil {
|
||||
if _, ok := v.c.BigPicture.Stories[s.Spec.BigPicture]; !ok {
|
||||
v.errf(e, id, "bigpicture-ref", "unknown Big Picture goal %q", s.Spec.BigPicture)
|
||||
}
|
||||
}
|
||||
if s.Meta.Title == "" && s.Spec.BigPicture == "" {
|
||||
v.errf(e, id, "required", "title or spec.bigpicture is required")
|
||||
}
|
||||
if s.Kind == KindSolution && s.Meta.Category != "" {
|
||||
found := false
|
||||
for _, cat := range v.c.Spec.Categories {
|
||||
found = found || cat.ID == s.Meta.Category
|
||||
}
|
||||
if !found {
|
||||
v.errf(e, id, "category", "unknown category %q", s.Meta.Category)
|
||||
}
|
||||
}
|
||||
if s.Kind == KindTask {
|
||||
switch s.Spec.Kind {
|
||||
case "read-only", "generate", "diagnostic", "planning", "mutating", "deployment", "maintenance":
|
||||
default:
|
||||
v.errf(e, id, "task-kind", "spec.kind %q: want read-only, generate, diagnostic, planning, mutating, deployment or maintenance", s.Spec.Kind)
|
||||
}
|
||||
if s.Spec.AppliesTo != nil {
|
||||
for _, sid := range s.Spec.AppliesTo.Solutions {
|
||||
if _, ok := v.c.Solutions[sid]; !ok {
|
||||
v.errf(e, id, "solution-ref", "appliesTo: unknown solution %q", sid)
|
||||
}
|
||||
}
|
||||
v.caps(e, id, s.Spec.AppliesTo.Capabilities)
|
||||
}
|
||||
}
|
||||
v.caps(e, id, s.Spec.Capabilities.Required)
|
||||
v.caps(e, id, s.Spec.Capabilities.Recommended)
|
||||
v.caps(e, id, s.Spec.Capabilities.Optional)
|
||||
nq := len(s.Spec.Questions)
|
||||
for _, pu := range s.Spec.Packages {
|
||||
if _, ok := v.c.Packages[pu.ID]; !ok {
|
||||
v.errf(e, id, "package-ref", "unknown package %q", pu.ID)
|
||||
continue
|
||||
}
|
||||
if pu.Profile != "" {
|
||||
if _, ok := v.c.Profiles[pu.ID+"/"+pu.Profile]; !ok {
|
||||
v.errf(e, id, "profile-ref", "unknown profile %s/%s", pu.ID, pu.Profile)
|
||||
}
|
||||
}
|
||||
v.cond(e, id, "package "+pu.ID, pu.When, s, nq)
|
||||
}
|
||||
seenQ := map[string]bool{}
|
||||
for i := range s.Spec.Questions {
|
||||
q := &s.Spec.Questions[i]
|
||||
qid := id + "." + q.ID
|
||||
if !questionIDRE.MatchString(q.ID) {
|
||||
v.errf(e, qid, "id", "question ID %q: letters, digits and _ only", q.ID)
|
||||
}
|
||||
if seenQ[q.ID] {
|
||||
v.errf(e, qid, "duplicate", "duplicate question %q", q.ID)
|
||||
}
|
||||
seenQ[q.ID] = true
|
||||
if !QuestionTypes[q.Type] {
|
||||
v.errf(e, qid, "type", "unknown question type %q", q.Type)
|
||||
}
|
||||
if q.Type == "list" && !ListItemTypes[q.Item] {
|
||||
v.errf(e, qid, "type", "list needs item: one of the scalar types, got %q", q.Item)
|
||||
}
|
||||
if q.Title == "" {
|
||||
v.errf(e, qid, "required", "question needs a title")
|
||||
}
|
||||
switch q.Level {
|
||||
case "", LevelQuick, LevelGuided, LevelAdvanced:
|
||||
default:
|
||||
v.errf(e, qid, "level", "unknown level %q", q.Level)
|
||||
}
|
||||
if (q.Type == "select" || q.Type == "multiselect") && len(q.Options) == 0 {
|
||||
v.errf(e, qid, "options", "%s question without options", q.Type)
|
||||
}
|
||||
if q.Type == "secret" && q.Default != nil {
|
||||
v.errf(e, qid, "secret-default", "a secret question cannot have a default (SEC-01)")
|
||||
}
|
||||
seenO := map[string]bool{}
|
||||
anySelectable := false
|
||||
for _, o := range q.Options {
|
||||
if seenO[o.ID] {
|
||||
v.errf(e, qid, "duplicate", "duplicate option %q", o.ID)
|
||||
}
|
||||
seenO[o.ID] = true
|
||||
if !o.Support.Valid() {
|
||||
v.errf(e, qid, "support", "option %q: unknown support %q", o.ID, o.Support)
|
||||
}
|
||||
if o.Support.Selectable() {
|
||||
anySelectable = true
|
||||
} else if o.SupportNote == "" {
|
||||
v.errf(e, qid, "support-note", "option %q is %s: supportNote must say why", o.ID, o.Support)
|
||||
}
|
||||
if o.Recommended && o.Reason == "" {
|
||||
v.errf(e, qid, "reason", "recommended option %q needs a reason (EX-03)", o.ID)
|
||||
}
|
||||
v.cond(e, qid, "option "+o.ID+" enabledWhen", o.EnabledWhen, s, i)
|
||||
v.effects(e, qid+"/"+o.ID, o.Effects, s)
|
||||
}
|
||||
if len(q.Options) > 0 && !anySelectable {
|
||||
v.errf(e, qid, "unsatisfiable", "no option of %q can be selected", q.ID)
|
||||
}
|
||||
if q.Default != nil {
|
||||
v.optionValue(e, qid, q, q.Default)
|
||||
}
|
||||
for _, d := range q.DefaultWhen {
|
||||
v.cond(e, qid, "defaultWhen", d.When, s, i)
|
||||
v.optionValue(e, qid, q, d.Value)
|
||||
}
|
||||
v.cond(e, qid, "visibleWhen", q.VisibleWhen, s, i)
|
||||
v.cond(e, qid, "requiredWhen", q.RequiredWhen, s, i)
|
||||
for _, w := range q.ValidateWhen {
|
||||
v.cond(e, qid, "validateWhen", w.When, s, i)
|
||||
}
|
||||
for _, w := range q.Warnings {
|
||||
v.cond(e, qid, "warning", w.When, s, i+1)
|
||||
}
|
||||
if q.Validation.Pattern != "" {
|
||||
if _, err := regexp.Compile(q.Validation.Pattern); err != nil {
|
||||
v.errf(e, qid, "pattern", "validation pattern: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
seenR := map[string]bool{}
|
||||
for _, r := range s.Spec.Rules {
|
||||
rid := id + "#" + r.ID
|
||||
if r.ID == "" || seenR[r.ID] {
|
||||
v.errf(e, rid, "rule-id", "every rule needs a unique id")
|
||||
}
|
||||
seenR[r.ID] = true
|
||||
v.cond(e, rid, "rule when", r.When, s, nq)
|
||||
v.effects(e, rid, r.Effects, s)
|
||||
switch r.Severity {
|
||||
case "", "info", "warning", "error":
|
||||
default:
|
||||
v.errf(e, rid, "severity", "unknown severity %q", r.Severity)
|
||||
}
|
||||
}
|
||||
for i, f := range s.Spec.Config {
|
||||
fid := f.ID
|
||||
if fid == "" {
|
||||
fid = fmt.Sprint(i)
|
||||
}
|
||||
v.fragment(e, id+"#config:"+fid, f, s, nq)
|
||||
if pkg, _, ok := strings.Cut(f.File, "/"); !ok || v.c.Packages[pkg] == nil {
|
||||
v.errf(e, id, "fragment-file", "fragment file %q must start with a package directory", f.File)
|
||||
}
|
||||
}
|
||||
for _, st := range s.Spec.Steps {
|
||||
v.cond(e, id+"#step:"+st.ID, "step", st.When, s, nq)
|
||||
if st.ForEach == "$modules" && st.Package == "" {
|
||||
v.errf(e, id+"#step:"+st.ID, "step", "forEach: $modules needs a package")
|
||||
} else if st.ForEach != "" && st.ForEach != "$modules" {
|
||||
if q, _ := s.Question(st.ForEach); q == nil {
|
||||
v.errf(e, id+"#step:"+st.ID, "question-ref", "forEach names unknown question %q", st.ForEach)
|
||||
}
|
||||
}
|
||||
if st.Package != "" && v.c.Packages[st.Package] == nil {
|
||||
v.errf(e, id+"#step:"+st.ID, "package-ref", "unknown package %q", st.Package)
|
||||
}
|
||||
}
|
||||
for _, ck := range s.Spec.Requirements {
|
||||
v.cond(e, id+"#check:"+ck.ID, "requirement", ck.When, s, nq)
|
||||
}
|
||||
for _, n := range s.Spec.Architecture.Nodes {
|
||||
v.cond(e, id+"#node:"+n.ID, "node", n.When, s, nq)
|
||||
}
|
||||
for _, ed := range s.Spec.Architecture.Edges {
|
||||
v.cond(e, id+"#edge", "edge", ed.When, s, nq)
|
||||
}
|
||||
for _, r := range s.Spec.Related {
|
||||
if v.c.Lookup(r) == nil {
|
||||
v.errf(e, id, "related", "related: unknown solution or task %q", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) effects(e *Entity, id string, fx Effects, s *Solution) {
|
||||
for _, t := range []*Targets{fx.Requires, fx.Excludes, fx.Conflicts, fx.Implies, fx.Recommends, fx.Discourages} {
|
||||
v.targets(e, id, t, s)
|
||||
}
|
||||
v.caps(e, id, fx.Provides)
|
||||
}
|
||||
|
||||
func (v *validator) example(x *Example) {
|
||||
e := &x.Entity
|
||||
s, ok := v.c.Solutions[x.Spec.Solution]
|
||||
if !ok {
|
||||
s, ok = v.c.Tasks[x.Spec.Solution]
|
||||
}
|
||||
if !ok {
|
||||
v.errf(e, x.Key(), "solution-ref", "example of unknown solution %q", x.Spec.Solution)
|
||||
return
|
||||
}
|
||||
for k, val := range x.Spec.Parameters {
|
||||
q, _ := s.Question(k)
|
||||
if q == nil {
|
||||
v.errf(e, x.Key(), "question-ref", "unknown parameter %q", k)
|
||||
continue
|
||||
}
|
||||
v.optionValue(e, x.Key(), q, val)
|
||||
}
|
||||
if x.Meta.Title == "" {
|
||||
v.errf(e, x.Key(), "required", "an example needs a title")
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) source(k *KnowledgeSource) {
|
||||
e := &k.Entity
|
||||
if _, ok := v.c.Packages[k.Spec.Package]; !ok {
|
||||
v.errf(e, k.Meta.ID, "package-ref", "unknown package %q", k.Spec.Package)
|
||||
}
|
||||
if k.Spec.Type != "manual" {
|
||||
v.errf(e, k.Meta.ID, "type", "type %q: only manual is supported", k.Spec.Type)
|
||||
}
|
||||
if k.Spec.Format != "markdown" {
|
||||
v.errf(e, k.Meta.ID, "format", "format %q: only markdown is supported", k.Spec.Format)
|
||||
}
|
||||
switch k.Spec.Layout {
|
||||
case "", "core-manual", "docs-tree":
|
||||
default:
|
||||
v.errf(e, k.Meta.ID, "layout", "layout %q: want core-manual or docs-tree", k.Spec.Layout)
|
||||
}
|
||||
if k.Spec.Root != "" && !SafeRelPath(k.Spec.Root) {
|
||||
v.errf(e, k.Meta.ID, "path", "root %q: want a clean relative path", k.Spec.Root)
|
||||
}
|
||||
if !SafeRelPath(k.Spec.Repository) {
|
||||
v.errf(e, k.Meta.ID, "path", "repository %q: want a plain name", k.Spec.Repository)
|
||||
}
|
||||
for _, t := range k.Spec.Topics {
|
||||
v.caps(e, k.Meta.ID+"/"+t.Slug, t.Capabilities)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user