727 lines
20 KiB
Go
727 lines
20 KiB
Go
package cli
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"bufio"
|
||
|
|
"errors"
|
||
|
|
"flag"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"yetano.dev/yetano/core/solutions/catalog"
|
||
|
|
"yetano.dev/yetano/core/solutions/engine"
|
||
|
|
"yetano.dev/yetano/core/solutions/knowledge"
|
||
|
|
"yetano.dev/yetano/core/solutions/ref"
|
||
|
|
"yetano.dev/yetano/core/solutions/store"
|
||
|
|
)
|
||
|
|
|
||
|
|
// multi is a repeatable flag.
|
||
|
|
type multi []string
|
||
|
|
|
||
|
|
func (m *multi) String() string { return strings.Join(*m, ",") }
|
||
|
|
func (m *multi) Set(s string) error { *m = append(*m, s); return nil }
|
||
|
|
|
||
|
|
func (a *app) solution(args []string) error {
|
||
|
|
if len(args) == 0 {
|
||
|
|
return a.usage("")
|
||
|
|
}
|
||
|
|
sub, rest := args[0], args[1:]
|
||
|
|
switch sub {
|
||
|
|
case "list", "ls":
|
||
|
|
return a.list(rest, false)
|
||
|
|
case "search":
|
||
|
|
return a.searchCmd(rest, []string{ref.Solution, ref.Task, ref.Example})
|
||
|
|
case "show":
|
||
|
|
return a.show(rest, false)
|
||
|
|
case "setup", "new":
|
||
|
|
return a.setup(rest, false)
|
||
|
|
case "state", "evaluate":
|
||
|
|
return a.stateCmd(rest)
|
||
|
|
case "generate":
|
||
|
|
return a.generate(rest)
|
||
|
|
case "validate":
|
||
|
|
return a.validate(rest)
|
||
|
|
case "plan":
|
||
|
|
return a.plan(rest)
|
||
|
|
case "doctor":
|
||
|
|
return a.doctor(rest)
|
||
|
|
case "config":
|
||
|
|
return a.config(rest)
|
||
|
|
case "architecture", "arch":
|
||
|
|
return a.architecture(rest)
|
||
|
|
case "handbook":
|
||
|
|
return a.handbook(rest)
|
||
|
|
case "package":
|
||
|
|
return a.packageCmd(rest)
|
||
|
|
case "instance", "instances":
|
||
|
|
return a.instance(rest)
|
||
|
|
case "preset", "presets":
|
||
|
|
return a.preset(rest)
|
||
|
|
case "catalog":
|
||
|
|
return a.catalogCmd(rest)
|
||
|
|
}
|
||
|
|
return a.usage("solution " + sub)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) task(args []string) error {
|
||
|
|
if len(args) == 0 {
|
||
|
|
return a.usage("")
|
||
|
|
}
|
||
|
|
sub, rest := args[0], args[1:]
|
||
|
|
switch sub {
|
||
|
|
case "list", "ls":
|
||
|
|
return a.list(rest, true)
|
||
|
|
case "search":
|
||
|
|
return a.searchCmd(rest, []string{ref.Task})
|
||
|
|
case "show":
|
||
|
|
return a.show(rest, true)
|
||
|
|
case "run", "setup":
|
||
|
|
return a.setup(rest, true)
|
||
|
|
}
|
||
|
|
return a.usage("task " + sub)
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- list, show ------------------------------------------------------------
|
||
|
|
|
||
|
|
type listItem struct {
|
||
|
|
ID string `json:"id"`
|
||
|
|
Kind string `json:"kind"`
|
||
|
|
Title string `json:"title"`
|
||
|
|
Summary string `json:"summary"`
|
||
|
|
Category string `json:"category,omitempty"`
|
||
|
|
Status catalog.Support `json:"status"`
|
||
|
|
Featured bool `json:"featured,omitempty"`
|
||
|
|
TaskKind string `json:"taskKind,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) items(tasks bool) []listItem {
|
||
|
|
m := a.cat.Solutions
|
||
|
|
if tasks {
|
||
|
|
m = a.cat.Tasks
|
||
|
|
}
|
||
|
|
var out []listItem
|
||
|
|
for _, id := range catalog.SortedIDs(m) {
|
||
|
|
s := m[id]
|
||
|
|
e := a.ix.Entry(ref.For(map[bool]string{false: ref.Solution, true: ref.Task}[tasks], id))
|
||
|
|
title, summary := s.Meta.Title, s.Meta.Summary
|
||
|
|
if e != nil {
|
||
|
|
title, summary = e.Title, e.Summary
|
||
|
|
}
|
||
|
|
st := s.Meta.Status
|
||
|
|
if st == "" {
|
||
|
|
st = catalog.Supported
|
||
|
|
}
|
||
|
|
out = append(out, listItem{ID: id, Kind: strings.ToLower(s.Kind), Title: a.tr(s.Meta.Translations, "title", title),
|
||
|
|
Summary: a.tr(s.Meta.Translations, "summary", summary), Category: s.Meta.Category, Status: st, Featured: s.Meta.Featured, TaskKind: s.Spec.Kind})
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) list(args []string, tasks bool) error {
|
||
|
|
fs := a.flags("list")
|
||
|
|
category := fs.String("category", "", "only this category")
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := a.load(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
items := a.items(tasks)
|
||
|
|
if *category != "" {
|
||
|
|
var f []listItem
|
||
|
|
for _, it := range items {
|
||
|
|
if it.Category == *category {
|
||
|
|
f = append(f, it)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
items = f
|
||
|
|
}
|
||
|
|
if a.jsonOut {
|
||
|
|
if items == nil {
|
||
|
|
items = []listItem{}
|
||
|
|
}
|
||
|
|
return a.printJSON(items)
|
||
|
|
}
|
||
|
|
for _, it := range items {
|
||
|
|
mark := ""
|
||
|
|
if it.Status != catalog.Supported {
|
||
|
|
mark = " [" + string(it.Status) + "]"
|
||
|
|
}
|
||
|
|
a.say("%-22s %s%s\n%-22s %s\n", it.ID, it.Title, mark, "", wrap(it.Summary, 70, strings.Repeat(" ", 23)))
|
||
|
|
}
|
||
|
|
if len(items) == 0 {
|
||
|
|
a.note("nothing found\n")
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) lookup(id string, tasks bool) (*catalog.Solution, error) {
|
||
|
|
s := a.cat.Lookup(id)
|
||
|
|
if s == nil {
|
||
|
|
var hint string
|
||
|
|
if groups := a.ix.Search(id, knowledge.SearchOptions{Kinds: []string{ref.Solution, ref.Task}, Limit: 3}); len(groups) > 0 {
|
||
|
|
hint = " -- did you mean " + strings.TrimPrefix(strings.TrimPrefix(groups[0].Hits[0].Ref, "yetano:solution/"), "yetano:task/") + "?"
|
||
|
|
}
|
||
|
|
return nil, fmt.Errorf("%w: %s%s", engine.ErrNotFound, id, hint)
|
||
|
|
}
|
||
|
|
return s, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type showOut struct {
|
||
|
|
listItem
|
||
|
|
Goals []string `json:"goals,omitempty"`
|
||
|
|
Scenarios []string `json:"scenarios,omitempty"`
|
||
|
|
NotFor []string `json:"notFor,omitempty"`
|
||
|
|
Outcome string `json:"outcome,omitempty"`
|
||
|
|
Packages []string `json:"packages"`
|
||
|
|
Capabilities catalog.CapSets `json:"capabilities"`
|
||
|
|
Platforms []string `json:"platforms,omitempty"`
|
||
|
|
Questions int `json:"questions"`
|
||
|
|
Quick []string `json:"quickQuestions"`
|
||
|
|
Examples []map[string]string `json:"examples"`
|
||
|
|
References []knowledge.Resolution `json:"references"`
|
||
|
|
Related []string `json:"related,omitempty"`
|
||
|
|
Backlinks []string `json:"backlinks,omitempty"`
|
||
|
|
Version string `json:"version"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) show(args []string, tasks bool) error {
|
||
|
|
fs := a.flags("show")
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if fs.NArg() != 1 {
|
||
|
|
return fail(ExitUsage, "usage: %s %s show <id>", a.env.Program, map[bool]string{false: "solution", true: "task"}[tasks])
|
||
|
|
}
|
||
|
|
if err := a.load(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
s, err := a.lookup(fs.Arg(0), tasks)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
var item listItem
|
||
|
|
for _, it := range a.items(s.IsTask()) {
|
||
|
|
if it.ID == s.Meta.ID {
|
||
|
|
item = it
|
||
|
|
}
|
||
|
|
}
|
||
|
|
o := showOut{listItem: item, Goals: s.Spec.Goals, Scenarios: s.Spec.Scenarios, NotFor: s.Spec.NotFor,
|
||
|
|
Capabilities: s.Spec.Capabilities, Platforms: s.Spec.Platforms, Questions: len(s.Spec.Questions), Related: s.Spec.Related,
|
||
|
|
Version: s.Meta.Version, Examples: []map[string]string{}}
|
||
|
|
if bp := a.cat.BigPicture; bp != nil {
|
||
|
|
if g, ok := bp.Stories[s.Spec.BigPicture]; ok {
|
||
|
|
o.Outcome = g.Outcome
|
||
|
|
}
|
||
|
|
}
|
||
|
|
seen := map[string]bool{}
|
||
|
|
for _, p := range s.Spec.Packages {
|
||
|
|
if !seen[p.ID] {
|
||
|
|
seen[p.ID] = true
|
||
|
|
o.Packages = append(o.Packages, p.ID)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, q := range s.Spec.Questions {
|
||
|
|
if q.Level == "" || q.Level == catalog.LevelQuick {
|
||
|
|
o.Quick = append(o.Quick, q.ID)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, k := range catalog.SortedIDs(a.cat.Examples) {
|
||
|
|
x := a.cat.Examples[k]
|
||
|
|
if x.Spec.Solution == s.Meta.ID {
|
||
|
|
o.Examples = append(o.Examples, map[string]string{"id": x.Meta.ID, "title": a.tr(x.Meta.Translations, "title", x.Meta.Title), "summary": x.Meta.Summary})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, r := range s.Spec.References.All() {
|
||
|
|
if res, err := a.ix.Resolve(r); err == nil {
|
||
|
|
o.References = append(o.References, res)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
kind := ref.Solution
|
||
|
|
if s.IsTask() {
|
||
|
|
kind = ref.Task
|
||
|
|
}
|
||
|
|
o.Backlinks = a.ix.BacklinksOf(ref.For(kind, s.Meta.ID))
|
||
|
|
if a.jsonOut {
|
||
|
|
return a.printJSON(o)
|
||
|
|
}
|
||
|
|
a.say("%s (%s %s, %s)\n\n", o.Title, o.ID, o.Version, o.Status)
|
||
|
|
a.say("%s\n", wrap(o.Summary, 76, ""))
|
||
|
|
if o.Outcome != "" {
|
||
|
|
a.say("\nResult: %s\n", wrap(o.Outcome, 76, " "))
|
||
|
|
}
|
||
|
|
section := func(title string, list []string) {
|
||
|
|
if len(list) > 0 {
|
||
|
|
a.say("\n%s\n", title)
|
||
|
|
for _, l := range list {
|
||
|
|
a.say(" - %s\n", wrap(l, 72, " "))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
section("Goals", o.Goals)
|
||
|
|
section("Good for", o.Scenarios)
|
||
|
|
section("Not for", o.NotFor)
|
||
|
|
a.say("\nPackages: %s\n", strings.Join(o.Packages, ", "))
|
||
|
|
a.say("Needs: %s\n", strings.Join(o.Capabilities.Required, ", "))
|
||
|
|
if len(o.Platforms) > 0 {
|
||
|
|
a.say("Platforms: %s\n", strings.Join(o.Platforms, ", "))
|
||
|
|
}
|
||
|
|
a.say("Questions: %d (quick setup asks %d)\n", o.Questions, len(o.Quick))
|
||
|
|
if len(o.Examples) > 0 {
|
||
|
|
a.say("\nExamples\n")
|
||
|
|
for _, x := range o.Examples {
|
||
|
|
a.say(" %-16s %s\n", x["id"], x["title"])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if len(o.References) > 0 {
|
||
|
|
a.say("\nRead more\n")
|
||
|
|
for _, r := range o.References {
|
||
|
|
a.say(" %s\n", refLine(r))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
cmd := "solution setup"
|
||
|
|
if s.IsTask() {
|
||
|
|
cmd = "task run"
|
||
|
|
}
|
||
|
|
a.say("\nStart: %s %s %s [--quick|--guided|--advanced] [--example <id>]\n", a.env.Program, cmd, o.ID)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func refLine(r knowledge.Resolution) string {
|
||
|
|
where := r.Web
|
||
|
|
if r.CLI != "" && (where == "" || r.Kind == ref.Manual) {
|
||
|
|
where = r.CLI
|
||
|
|
}
|
||
|
|
title := r.Title
|
||
|
|
if title == "" {
|
||
|
|
title = r.Ref
|
||
|
|
}
|
||
|
|
if !r.Exists {
|
||
|
|
title += " (missing)"
|
||
|
|
}
|
||
|
|
return fmt.Sprintf("%-40s %s", title, where)
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- instances from files or the store ----------------------------------------
|
||
|
|
|
||
|
|
// instanceFrom reads --file, or a stored instance named by the first argument.
|
||
|
|
func (a *app) instanceFrom(file string, fs *flag.FlagSet) (*engine.Instance, error) {
|
||
|
|
if file != "" {
|
||
|
|
var f *os.File
|
||
|
|
var err error
|
||
|
|
if file == "-" {
|
||
|
|
b := bufio.NewReader(a.env.Stdin)
|
||
|
|
in, err := engine.ReadInstance(b)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fail(ExitUsage, "stdin: %v", err)
|
||
|
|
}
|
||
|
|
return in, nil
|
||
|
|
}
|
||
|
|
f, err = os.Open(file)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fail(ExitNotFound, "%v", err)
|
||
|
|
}
|
||
|
|
defer f.Close()
|
||
|
|
in, err := engine.ReadInstance(f)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fail(ExitUsage, "%s: %v", file, err)
|
||
|
|
}
|
||
|
|
return in, nil
|
||
|
|
}
|
||
|
|
if fs.NArg() < 1 {
|
||
|
|
return nil, fail(ExitUsage, "name a saved instance or pass --file <solution.yaml>")
|
||
|
|
}
|
||
|
|
return a.store.LoadInstance(fs.Arg(0))
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) presetFor(in *engine.Instance, name string) (*engine.Preset, error) {
|
||
|
|
if name == "" {
|
||
|
|
name = in.Spec.Preset
|
||
|
|
}
|
||
|
|
if name == "" {
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
if strings.HasSuffix(name, ".yaml") || strings.Contains(name, string(filepath.Separator)) {
|
||
|
|
f, err := os.Open(name)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
defer f.Close()
|
||
|
|
return engine.ReadPreset(f)
|
||
|
|
}
|
||
|
|
return a.store.LoadPreset(name)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) evaluate(in *engine.Instance, preset *engine.Preset) (*engine.State, error) {
|
||
|
|
if in.Spec.Locale == "" {
|
||
|
|
in.Spec.Locale = a.locale
|
||
|
|
}
|
||
|
|
return a.eng.Evaluate(in, preset)
|
||
|
|
}
|
||
|
|
|
||
|
|
// common flags of the commands that take an instance
|
||
|
|
type instFlags struct {
|
||
|
|
file *string
|
||
|
|
preset *string
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) instFlags(fs *flag.FlagSet) instFlags {
|
||
|
|
return instFlags{
|
||
|
|
file: fs.String("file", "", "solution.yaml (SolutionInstance or WizardSession); - for stdin"),
|
||
|
|
preset: fs.String("preset", "", "preset name or file (default: the instance's)"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) stateOf(fs *flag.FlagSet, f instFlags) (*engine.Instance, *engine.State, error) {
|
||
|
|
if err := a.load(); err != nil {
|
||
|
|
return nil, nil, err
|
||
|
|
}
|
||
|
|
in, err := a.instanceFrom(*f.file, fs)
|
||
|
|
if err != nil {
|
||
|
|
return nil, nil, err
|
||
|
|
}
|
||
|
|
p, err := a.presetFor(in, *f.preset)
|
||
|
|
if err != nil {
|
||
|
|
return nil, nil, err
|
||
|
|
}
|
||
|
|
st, err := a.evaluate(in, p)
|
||
|
|
return in, st, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// stateCmd prints the full engine state: the shared client API as JSON
|
||
|
|
// (API-01); GUI and web clients render exactly this.
|
||
|
|
func (a *app) stateCmd(args []string) error {
|
||
|
|
fs := a.flags("solution state")
|
||
|
|
f := a.instFlags(fs)
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
a.jsonOut = true
|
||
|
|
_, st, err := a.stateOf(fs, f)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return a.printJSON(st)
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- generate, validate, plan -------------------------------------------------
|
||
|
|
|
||
|
|
func (a *app) generate(args []string) error {
|
||
|
|
fs := a.flags("solution generate")
|
||
|
|
f := a.instFlags(fs)
|
||
|
|
output := fs.String("output", "", "output directory (required)")
|
||
|
|
force := fs.Bool("force", false, "overwrite files changed by hand")
|
||
|
|
locked := fs.Bool("locked", false, "fail if the catalog changed the meaning since the last generation (solution.lock.yaml)")
|
||
|
|
dry := fs.Bool("dry-run", false, "list the files, write nothing")
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if *output == "" && !*dry {
|
||
|
|
return fail(ExitUsage, "--output <dir> is required")
|
||
|
|
}
|
||
|
|
in, st, err := a.stateOf(fs, f)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return a.writeOut(in, st, *output, *force, *locked, *dry)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) writeOut(in *engine.Instance, st *engine.State, dir string, force, locked, dry bool) error {
|
||
|
|
out, err := a.eng.Generate(st, in, a.ix, false)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if dir != "" && !dry {
|
||
|
|
if prev, err := engine.ReadLock(dir); err == nil && prev != nil {
|
||
|
|
if drift := engine.LockDrift(prev, out.Lock); len(drift) > 0 {
|
||
|
|
if locked {
|
||
|
|
return fail(ExitConflict, "the catalog changed since the last generation: %s (run without --locked to accept)", strings.Join(drift, "; "))
|
||
|
|
}
|
||
|
|
a.note("note: since the last generation: %s\n", strings.Join(drift, "; "))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if err := engine.Write(dir, out, force); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if a.jsonOut {
|
||
|
|
return a.printJSON(map[string]any{"output": dir, "dryRun": dry, "files": out.Files, "complete": st.Complete, "messages": st.Messages})
|
||
|
|
}
|
||
|
|
verb := "wrote"
|
||
|
|
if dry {
|
||
|
|
verb = "would write"
|
||
|
|
}
|
||
|
|
a.note("%s %d files to %s\n", verb, len(out.Files), dir)
|
||
|
|
for _, f := range out.Files {
|
||
|
|
a.say(" %s\n", f.Path)
|
||
|
|
}
|
||
|
|
a.printMessages(st)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) printMessages(st *engine.State) {
|
||
|
|
for _, m := range st.Messages {
|
||
|
|
a.note("%-7s %s\n", strings.ToUpper(m.Severity), wrap(m.Text, 70, " "))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// validate: is the instance complete and valid; with --native also run
|
||
|
|
// the programs' own validators on a generated directory.
|
||
|
|
func (a *app) validate(args []string) error {
|
||
|
|
fs := a.flags("solution validate")
|
||
|
|
f := a.instFlags(fs)
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
_, st, err := a.stateOf(fs, f)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
type qerr struct {
|
||
|
|
Question string `json:"question"`
|
||
|
|
Errors []string `json:"errors"`
|
||
|
|
}
|
||
|
|
var qerrs []qerr
|
||
|
|
for _, q := range st.Questions {
|
||
|
|
if len(q.Errors) > 0 {
|
||
|
|
qerrs = append(qerrs, qerr{q.ID, q.Errors})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if a.jsonOut {
|
||
|
|
if err := a.printJSON(map[string]any{"complete": st.Complete, "missing": st.Progress.Missing, "errors": qerrs, "stale": st.Stale, "messages": st.Messages}); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
for _, q := range qerrs {
|
||
|
|
a.say("ERROR %s: %s\n", q.Question, strings.Join(q.Errors, "; "))
|
||
|
|
}
|
||
|
|
for _, m := range st.Progress.Missing {
|
||
|
|
a.say("MISSING %s\n", m)
|
||
|
|
}
|
||
|
|
for _, s := range st.Stale {
|
||
|
|
a.say("STALE %s=%v: %s\n", s.Question, s.Value, s.Reason)
|
||
|
|
}
|
||
|
|
a.printMessages(st)
|
||
|
|
if st.Complete {
|
||
|
|
a.say("valid: %s is complete\n", st.Solution.ID)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !st.Complete {
|
||
|
|
return exitErr{ExitError, errors.New("")}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) plan(args []string) error {
|
||
|
|
fs := a.flags("solution plan")
|
||
|
|
f := a.instFlags(fs)
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
_, st, err := a.stateOf(fs, f)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
p := engine.PlanOf(st)
|
||
|
|
if a.jsonOut {
|
||
|
|
return a.printJSON(p)
|
||
|
|
}
|
||
|
|
a.say("Plan for %s (%s) -- nothing is executed by the guide.\n", firstNonEmpty(st.Instance, st.Solution.ID), p.Spec.Solution)
|
||
|
|
phase := ""
|
||
|
|
for i, s := range p.Spec.Steps {
|
||
|
|
if s.Phase != phase {
|
||
|
|
phase = s.Phase
|
||
|
|
a.say("\n%s\n", strings.ToUpper(phase))
|
||
|
|
}
|
||
|
|
mark := " "
|
||
|
|
if s.Mutating {
|
||
|
|
mark = "*"
|
||
|
|
}
|
||
|
|
a.say("%3d.%s %s\n", i+1, mark, s.Title)
|
||
|
|
if s.Run != "" {
|
||
|
|
a.say(" $ %s\n", s.Run)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
a.say("\n* changes the system\n")
|
||
|
|
if !st.Complete {
|
||
|
|
a.printMessages(st)
|
||
|
|
return fail(ExitUsage, "the solution is incomplete: %s", strings.Join(st.Progress.Missing, ", "))
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func firstNonEmpty(a ...string) string {
|
||
|
|
for _, s := range a {
|
||
|
|
if s != "" {
|
||
|
|
return s
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- config, architecture, handbook ------------------------------------------
|
||
|
|
|
||
|
|
func (a *app) config(args []string) error {
|
||
|
|
if len(args) == 0 {
|
||
|
|
return fail(ExitUsage, "usage: %s solution config show|explain ...", a.env.Program)
|
||
|
|
}
|
||
|
|
switch args[0] {
|
||
|
|
case "show":
|
||
|
|
fs := a.flags("solution config show")
|
||
|
|
f := a.instFlags(fs)
|
||
|
|
fs.Bool("effective", true, "show the effective, merged configuration (the default)")
|
||
|
|
origins := fs.Bool("origins", false, "show where each value comes from")
|
||
|
|
if err := a.parse(fs, args[1:]); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
_, st, err := a.stateOf(fs, f)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if a.jsonOut {
|
||
|
|
return a.printJSON(st.Files)
|
||
|
|
}
|
||
|
|
for _, file := range st.Files {
|
||
|
|
a.say("--- %s\n%s\n", file.Path, file.Content)
|
||
|
|
if *origins {
|
||
|
|
for _, k := range sortedKeys(file.Origins) {
|
||
|
|
o := file.Origins[k]
|
||
|
|
a.say(" %-40s %s %s\n", k, o.Source, strings.Join(o.Params, ","))
|
||
|
|
}
|
||
|
|
a.say("\n")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
case "explain":
|
||
|
|
fs := a.flags("solution config explain")
|
||
|
|
f := a.instFlags(fs)
|
||
|
|
if err := a.parse(fs, args[1:]); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
// [instance] <file> <path>
|
||
|
|
pos := fs.Args()
|
||
|
|
var file, path string
|
||
|
|
switch {
|
||
|
|
case *f.file != "" && len(pos) == 2:
|
||
|
|
file, path = pos[0], pos[1]
|
||
|
|
case *f.file == "" && len(pos) == 3:
|
||
|
|
file, path = pos[1], pos[2]
|
||
|
|
default:
|
||
|
|
return fail(ExitUsage, "usage: %s solution config explain <instance>|--file f <file> <path> (e.g. yamase/yamase.yaml tls.cert-file)", a.env.Program)
|
||
|
|
}
|
||
|
|
_, st, err := a.stateOf(fs, f)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
ex := engine.Explain(st, file, path)
|
||
|
|
if a.jsonOut {
|
||
|
|
return a.printJSON(ex)
|
||
|
|
}
|
||
|
|
if !ex.Found {
|
||
|
|
return fail(ExitNotFound, "%s: %s is not set by the guide (the program's built-in default applies)", ex.File, path)
|
||
|
|
}
|
||
|
|
a.say("%s %s\n set by %s (%s layer)\n", ex.File, path, ex.Origin.Source, ex.Origin.Layer)
|
||
|
|
for _, p := range sortedKeys(ex.Answers) {
|
||
|
|
a.say(" uses answer %s = %v (%s)\n", p, ex.Answers[p], ex.Origins[p])
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
return a.usage("solution config " + args[0])
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) architecture(args []string) error {
|
||
|
|
fs := a.flags("solution architecture")
|
||
|
|
f := a.instFlags(fs)
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
_, st, err := a.stateOf(fs, f)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if a.jsonOut {
|
||
|
|
return a.printJSON(st.Graph)
|
||
|
|
}
|
||
|
|
a.say("%s", engine.GraphText(st.Graph))
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) handbook(args []string) error {
|
||
|
|
fs := a.flags("solution handbook")
|
||
|
|
f := a.instFlags(fs)
|
||
|
|
output := fs.String("output", "", "directory to write the handbook to (default: print index)")
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
_, st, err := a.stateOf(fs, f)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
pages := a.eng.Handbook(st, a.ix)
|
||
|
|
if *output == "" {
|
||
|
|
for _, p := range pages {
|
||
|
|
a.say("%s\n", p.Content)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
for _, p := range pages {
|
||
|
|
path := filepath.Join(*output, filepath.FromSlash(strings.TrimPrefix(p.Path, "handbook/")))
|
||
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(path, p.Content, 0o644); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
a.note("wrote %d pages to %s\n", len(pages), *output)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *app) packageCmd(args []string) error {
|
||
|
|
fs := a.flags("solution package")
|
||
|
|
if err := a.parse(fs, args); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := a.load(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if fs.NArg() != 1 {
|
||
|
|
return fail(ExitUsage, "usage: %s solution package <id>", a.env.Program)
|
||
|
|
}
|
||
|
|
p := a.cat.Packages[fs.Arg(0)]
|
||
|
|
if p == nil {
|
||
|
|
return fmt.Errorf("%w: package %s", engine.ErrNotFound, fs.Arg(0))
|
||
|
|
}
|
||
|
|
r := ref.For(ref.Package, p.Meta.ID)
|
||
|
|
back := a.ix.BacklinksOf(r)
|
||
|
|
if a.jsonOut {
|
||
|
|
return a.printJSON(map[string]any{"package": p, "summary": a.eng.PackageSummary(p.Meta.ID), "usedBy": back})
|
||
|
|
}
|
||
|
|
a.say("%s -- %s\n\n", p.Meta.ID, wrap(a.eng.PackageSummary(p.Meta.ID), 70, " "))
|
||
|
|
for _, m := range p.Spec.Modules {
|
||
|
|
sup := ""
|
||
|
|
if m.Support != "" && m.Support != catalog.Supported {
|
||
|
|
sup = " [" + string(m.Support) + "]"
|
||
|
|
}
|
||
|
|
a.say(" %-14s %s%s\n", m.ID, m.Title, sup)
|
||
|
|
if len(m.Provides) > 0 {
|
||
|
|
a.say(" %-14s provides %s\n", "", strings.Join(m.Provides, ", "))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if len(back) > 0 {
|
||
|
|
a.say("\nWhat would you like to do with %s?\n", p.Meta.ID)
|
||
|
|
for _, b := range back {
|
||
|
|
if strings.HasPrefix(b, "yetano:solution/") || strings.HasPrefix(b, "yetano:task/") {
|
||
|
|
res, _ := a.ix.Resolve(b)
|
||
|
|
a.say(" %-30s %s\n", res.Title, res.CLI)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if res, err := a.ix.Resolve(ref.For(ref.Microsite, p.Meta.ID)); err == nil && res.Web != "" {
|
||
|
|
a.say("\nMicrosite: %s\n", res.Web)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
var _ = store.ErrNotFound
|