Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1c0b8a116 | ||
|
|
c8a219a84b |
No files matched your search
@@ -2,7 +2,9 @@ package catalog
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -16,6 +18,7 @@ import (
|
|||||||
// kept.
|
// kept.
|
||||||
type BigPicture struct {
|
type BigPicture struct {
|
||||||
Dir string `json:"-"`
|
Dir string `json:"-"`
|
||||||
|
fsys fs.FS
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
WebPath string `json:"webPath"` // "map/"
|
WebPath string `json:"webPath"` // "map/"
|
||||||
Topics map[string]*BPTopic `json:"topics"`
|
Topics map[string]*BPTopic `json:"topics"`
|
||||||
@@ -89,8 +92,14 @@ type BPInterface struct {
|
|||||||
|
|
||||||
// LoadBigPicture reads dir/manifest.yaml and the files it names.
|
// LoadBigPicture reads dir/manifest.yaml and the files it names.
|
||||||
func LoadBigPicture(dir string) (*BigPicture, error) {
|
func LoadBigPicture(dir string) (*BigPicture, error) {
|
||||||
|
return LoadBigPictureFS(os.DirFS(dir), dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadBigPictureFS reads manifest.yaml and the files it names from fsys
|
||||||
|
// (an embedded copy, e.g. in the browser); dir only names it in messages.
|
||||||
|
func LoadBigPictureFS(fsys fs.FS, dir string) (*BigPicture, error) {
|
||||||
bp := &BigPicture{
|
bp := &BigPicture{
|
||||||
Dir: dir, WebPath: "map/",
|
Dir: dir, fsys: fsys, WebPath: "map/",
|
||||||
Topics: map[string]*BPTopic{}, Stories: map[string]*BPStory{},
|
Topics: map[string]*BPTopic{}, Stories: map[string]*BPStory{},
|
||||||
Products: map[string]*BPProduct{}, Interfaces: map[string]BPInterface{},
|
Products: map[string]*BPProduct{}, Interfaces: map[string]BPInterface{},
|
||||||
}
|
}
|
||||||
@@ -157,7 +166,7 @@ func (bp *BigPicture) read(name string, v any) error {
|
|||||||
if name == "" {
|
if name == "" {
|
||||||
return fmt.Errorf("big picture: manifest names no file")
|
return fmt.Errorf("big picture: manifest names no file")
|
||||||
}
|
}
|
||||||
b, err := os.ReadFile(filepath.Join(bp.Dir, name))
|
b, err := fs.ReadFile(bp.fsys, path.Clean(filepath.ToSlash(name)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("big picture: %w", err)
|
return fmt.Errorf("big picture: %w", err)
|
||||||
}
|
}
|
||||||
@@ -172,7 +181,7 @@ func (bp *BigPicture) read(name string, v any) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bp *BigPicture) readDir(sub string, fn func(string) error) error {
|
func (bp *BigPicture) readDir(sub string, fn func(string) error) error {
|
||||||
ents, err := os.ReadDir(filepath.Join(bp.Dir, sub))
|
ents, err := fs.ReadDir(bp.fsys, path.Clean(filepath.ToSlash(sub)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("big picture: %w", err)
|
return fmt.Errorf("big picture: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,6 +156,14 @@ func Load(root string) (*Catalog, error) {
|
|||||||
// LoadFS reads a catalog from fsys (root is only used for messages and
|
// 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).
|
// to find the Big Picture relative to it, when fsys is a directory).
|
||||||
func LoadFS(fsys fs.FS, root string) (*Catalog, error) {
|
func LoadFS(fsys fs.FS, root string) (*Catalog, error) {
|
||||||
|
return LoadFSWith(fsys, root, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadFSWith is LoadFS with the Big Picture read from bigPicture instead
|
||||||
|
// of the directory catalog.yaml names -- for a catalog without a disk
|
||||||
|
// (embedded in a program, or in the browser as WebAssembly). nil: as
|
||||||
|
// LoadFS.
|
||||||
|
func LoadFSWith(fsys fs.FS, root string, bigPicture fs.FS) (*Catalog, error) {
|
||||||
c := &Catalog{
|
c := &Catalog{
|
||||||
Root: root,
|
Root: root,
|
||||||
Capabilities: map[string]*Capability{},
|
Capabilities: map[string]*Capability{},
|
||||||
@@ -206,7 +214,15 @@ func LoadFS(fsys fs.FS, root string) (*Catalog, error) {
|
|||||||
if !sawRoot {
|
if !sawRoot {
|
||||||
c.add("error", "catalog.yaml", 0, "", "no-root", "no Catalog manifest (catalog.yaml) found")
|
c.add("error", "catalog.yaml", 0, "", "no-root", "no Catalog manifest (catalog.yaml) found")
|
||||||
}
|
}
|
||||||
if c.Spec.BigPicture != "" {
|
if c.Spec.BigPicture != "" && bigPicture != nil {
|
||||||
|
bp, err := LoadBigPictureFS(bigPicture, c.Spec.BigPicture)
|
||||||
|
if err != nil {
|
||||||
|
c.add("error", "catalog.yaml", 0, "", "bigpicture", err.Error())
|
||||||
|
} else {
|
||||||
|
c.BigPicture = bp
|
||||||
|
c.Files = append(c.Files, bp.Files...)
|
||||||
|
}
|
||||||
|
} else if c.Spec.BigPicture != "" {
|
||||||
bpDir := c.Spec.BigPicture
|
bpDir := c.Spec.BigPicture
|
||||||
if !filepath.IsAbs(bpDir) {
|
if !filepath.IsAbs(bpDir) {
|
||||||
bpDir = filepath.Join(root, bpDir)
|
bpDir = filepath.Join(root, bpDir)
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package catalog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A catalog without a disk (embedded, or in the browser) loads the Big
|
||||||
|
// Picture from its own fs.FS and gives the same catalog as from disk.
|
||||||
|
func TestLoadFSWith(t *testing.T) {
|
||||||
|
disk, err := Load("../engine/testdata/cat")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// "/nonexistent" as root: nothing may be read from disk
|
||||||
|
emb, err := LoadFSWith(os.DirFS("../engine/testdata/cat"), "/nonexistent", os.DirFS("../engine/testdata/bp"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if emb.BigPicture == nil || len(emb.BigPicture.Products) == 0 {
|
||||||
|
t.Fatal("no Big Picture")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(SortedIDs(disk.BigPicture.Products), SortedIDs(emb.BigPicture.Products)) || disk.Hash() != emb.Hash() {
|
||||||
|
t.Fatalf("embedded catalog differs: %v / %v", disk.Hash(), emb.Hash())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,13 +23,17 @@ import (
|
|||||||
"yetano.dev/yetano/core/solutions/store"
|
"yetano.dev/yetano/core/solutions/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Exit codes, the same as yet's (yet/internal/cliutil).
|
// Exit codes, the same as yet's (yet/internal/cliutil). 0-3 are the
|
||||||
|
// family-wide ones (docs/program-conventions.md in the meta-repo); 3 is
|
||||||
|
// "interrupted" there, so "not found" is 7 (owner decision 2026-12-01,
|
||||||
|
// it was 3 up to solutions/v0.1.0).
|
||||||
const (
|
const (
|
||||||
ExitOK = 0
|
ExitOK = 0
|
||||||
ExitError = 1
|
ExitError = 1
|
||||||
ExitUsage = 2
|
ExitUsage = 2
|
||||||
ExitNotFound = 3
|
ExitInterrupted = 3 // stopped by SIGINT/SIGTERM (the context was cancelled)
|
||||||
ExitConflict = 4
|
ExitConflict = 4
|
||||||
|
ExitNotFound = 7
|
||||||
)
|
)
|
||||||
|
|
||||||
// Env is what the embedding program provides.
|
// Env is what the embedding program provides.
|
||||||
@@ -103,6 +107,9 @@ func Run(ctx context.Context, args []string, env Env) int {
|
|||||||
code = ee.code
|
code = ee.code
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
|
case ctx.Err() != nil:
|
||||||
|
// whatever failed, it failed because we were told to stop
|
||||||
|
code = ExitInterrupted
|
||||||
case errors.Is(err, engine.ErrNotFound), errors.Is(err, store.ErrNotFound):
|
case errors.Is(err, engine.ErrNotFound), errors.Is(err, store.ErrNotFound):
|
||||||
code = ExitNotFound
|
code = ExitNotFound
|
||||||
case errors.Is(err, engine.ErrModified):
|
case errors.Is(err, engine.ErrModified):
|
||||||
|
|||||||
@@ -147,3 +147,22 @@ func TestInteractiveWizard(t *testing.T) {
|
|||||||
t.Error("export did not write the configuration")
|
t.Error("export did not write the configuration")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ctrl-C: a cancelled context is exit 3, the family-wide "interrupted"
|
||||||
|
// (docs/program-conventions.md); "not found" moved to 7 for it.
|
||||||
|
func TestInterrupted(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
var out, errb bytes.Buffer
|
||||||
|
code := Run(ctx, []string{"solution", "show", "nope"}, Env{
|
||||||
|
Stdin: strings.NewReader(""), Stdout: &out, Stderr: &errb,
|
||||||
|
Program: "yetano-solutions", StoreDir: t.TempDir(), Catalogs: []string{"../engine/testdata/cat"},
|
||||||
|
Getenv: func(string) string { return "" },
|
||||||
|
})
|
||||||
|
if code != ExitInterrupted || ExitInterrupted != 3 || ExitNotFound != 7 {
|
||||||
|
t.Fatalf("cancelled: exit %d (%s)", code, errb.String())
|
||||||
|
}
|
||||||
|
if r := run(t, t.TempDir(), "solution", "show", "nope"); r.code != 7 {
|
||||||
|
t.Fatalf("not found: exit %d", r.code)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"yetano.dev/yetano/core"
|
"yetano.dev/yetano/core"
|
||||||
"yetano.dev/yetano/core/paths"
|
"yetano.dev/yetano/core/paths"
|
||||||
@@ -19,7 +20,7 @@ import (
|
|||||||
var version = "dev"
|
var version = "dev"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
defer stop()
|
defer stop()
|
||||||
// instances, sessions and presets are user data: core/paths' data
|
// instances, sessions and presets are user data: core/paths' data
|
||||||
// directory (nothing is created until something is saved)
|
// directory (nothing is created until something is saved)
|
||||||
|
|||||||
@@ -224,7 +224,9 @@ func (ix *Index) manuals() {
|
|||||||
if d.IsDir() && strings.HasPrefix(d.Name(), ".") {
|
if d.IsDir() && strings.HasPrefix(d.Name(), ".") {
|
||||||
return fs.SkipDir
|
return fs.SkipDir
|
||||||
}
|
}
|
||||||
if !d.IsDir() && strings.HasSuffix(p, ".md") {
|
// *.conflict.md: a wiki-sync conflict copy (meta-repo
|
||||||
|
// AGENTS.md), never documentation of its own
|
||||||
|
if !d.IsDir() && strings.HasSuffix(p, ".md") && !strings.HasSuffix(p, ".conflict.md") {
|
||||||
files = append(files, rel)
|
files = append(files, rel)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in new issue
Block a user