package catalog import ( "fmt" "io/fs" "os" "path" "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:"-"` fsys fs.FS 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) { 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{ Dir: dir, fsys: fsys, 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 := fs.ReadFile(bp.fsys, path.Clean(filepath.ToSlash(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 := fs.ReadDir(bp.fsys, path.Clean(filepath.ToSlash(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 "" }