package catalog import ( _ "embed" "fmt" "os" "path/filepath" "sort" "strings" "gopkg.in/yaml.v3" "git.starwarslab.us/ATOM_IT/Deployr/internal/platform" ) //go:embed data/apps.yaml var embeddedYAML []byte // Parse reads a catalog document. func Parse(data []byte, origin string) (*Catalog, error) { var c Catalog dec := yaml.NewDecoder(strings.NewReader(string(data))) dec.KnownFields(true) if err := dec.Decode(&c); err != nil { return nil, fmt.Errorf("%s: %w", origin, err) } if err := c.validate(origin); err != nil { return nil, err } return &c, nil } // Embedded returns the catalog compiled into the binary. func Embedded() (*Catalog, error) { c, err := Parse(embeddedYAML, "embedded catalog") if err != nil { return nil, err } c.reindex() return c, nil } // Load returns the embedded catalog with overlays merged over it. Overlays are // read from the well-known catalog.d directories and then from extra (files or // directories), so an operator can add or override apps without rebuilding. func Load(extra []string) (*Catalog, error) { base, err := Embedded() if err != nil { return nil, err } var files []string for _, dir := range platform.CatalogDirs() { files = append(files, yamlFilesIn(dir)...) } for _, p := range extra { p = platform.ExpandPath(p) info, err := os.Stat(p) if err != nil { return nil, fmt.Errorf("catalog %q: %w", p, err) } if info.IsDir() { files = append(files, yamlFilesIn(p)...) } else { files = append(files, p) } } for _, f := range files { data, err := os.ReadFile(f) if err != nil { return nil, fmt.Errorf("catalog %q: %w", f, err) } overlay, err := Parse(data, f) if err != nil { return nil, err } base.merge(overlay) } base.reindex() return base, nil } func yamlFilesIn(dir string) []string { var out []string for _, pattern := range []string{"*.yaml", "*.yml"} { matches, err := filepath.Glob(filepath.Join(dir, pattern)) if err == nil { out = append(out, matches...) } } sort.Strings(out) return out } // merge applies an overlay: apps are replaced whole by ID, profiles by name. // Replacing rather than deep-merging keeps overrides predictable — what you // read in the overlay file is exactly what will run. func (c *Catalog) merge(overlay *Catalog) { for _, app := range overlay.Apps { replaced := false for i, existing := range c.Apps { if strings.EqualFold(existing.ID, app.ID) { c.Apps[i] = app replaced = true break } } if !replaced { c.Apps = append(c.Apps, app) } } if len(overlay.Profiles) > 0 && c.Profiles == nil { c.Profiles = map[string][]string{} } for name, apps := range overlay.Profiles { c.Profiles[name] = apps } } func (c *Catalog) validate(origin string) error { seen := map[string]bool{} for _, app := range c.Apps { switch { case app.ID == "": return fmt.Errorf("%s: an app entry is missing an id", origin) case seen[strings.ToLower(app.ID)]: return fmt.Errorf("%s: duplicate app id %q", origin, app.ID) case app.Name == "": return fmt.Errorf("%s: app %q is missing a name", origin, app.ID) case len(app.Platforms()) == 0: return fmt.Errorf("%s: app %q defines no platform targets", origin, app.ID) } seen[strings.ToLower(app.ID)] = true for _, goos := range app.Platforms() { t := app.TargetFor(goos) if len(t.Sources) == 0 { return fmt.Errorf("%s: app %q has no %s sources", origin, app.ID, goos) } for i, s := range t.Sources { if s.Provider == "" { return fmt.Errorf("%s: app %q %s source %d has no provider", origin, app.ID, goos, i+1) } // direct and driver sources carry a location rather than a // package identifier; everything else needs an id. switch s.Provider { case "direct", "driver": if s.URL == "" && len(s.URLs) == 0 { return fmt.Errorf("%s: app %q %s %s source has no url or path", origin, app.ID, goos, s.Provider) } default: if s.ID == "" { return fmt.Errorf("%s: app %q %s %s source has no id", origin, app.ID, goos, s.Provider) } } } } } return nil } func (c *Catalog) reindex() { c.index = make(map[string]*App, len(c.Apps)*2) for _, app := range c.Apps { c.index[strings.ToLower(app.ID)] = app for _, alias := range app.Aliases { c.index[strings.ToLower(alias)] = app } } sort.Slice(c.Apps, func(i, j int) bool { return strings.ToLower(c.Apps[i].ID) < strings.ToLower(c.Apps[j].ID) }) } // Get looks up an app by ID or alias. func (c *Catalog) Get(name string) (*App, bool) { app, ok := c.index[strings.ToLower(strings.TrimSpace(name))] return app, ok } // Resolve expands a list of command-line names into apps. A name prefixed with // "@" refers to a profile. Unknown names are returned separately so the caller // can report all of them at once rather than dying on the first. func (c *Catalog) Resolve(names []string) (apps []*App, unknown []string) { seen := map[string]bool{} add := func(app *App) { if !seen[app.ID] { seen[app.ID] = true apps = append(apps, app) } } for _, raw := range names { name := strings.TrimSpace(raw) if name == "" { continue } if strings.HasPrefix(name, "@") { members, ok := c.Profiles[strings.TrimPrefix(name, "@")] if !ok { unknown = append(unknown, name) continue } for _, m := range members { if app, ok := c.Get(m); ok { add(app) } else { unknown = append(unknown, m) } } continue } if app, ok := c.Get(name); ok { add(app) continue } unknown = append(unknown, name) } return apps, unknown } // ForPlatform returns every app installable on the given GOOS. func (c *Catalog) ForPlatform(goos string) []*App { var out []*App for _, app := range c.Apps { if app.TargetFor(goos) != nil { out = append(out, app) } } return out } // Suggest returns catalog IDs similar to name, for "did you mean" output. func (c *Catalog) Suggest(name string) []string { name = strings.ToLower(strings.TrimPrefix(name, "@")) var out []string for _, app := range c.Apps { id := strings.ToLower(app.ID) if strings.Contains(id, name) || strings.Contains(name, id) || strings.Contains(strings.ToLower(app.Name), name) { out = append(out, app.ID) } } sort.Strings(out) if len(out) > 5 { out = out[:5] } return out }