Files
certimate/internal/pluginhost/catalog.go
2026-07-27 09:53:50 +08:00

98 lines
2.6 KiB
Go

package pluginhost
import (
"sort"
"sync"
)
const (
SourcePlugin = "plugin"
SourceMarketplace = "marketplace"
)
type CatalogEntry struct {
Source string `json:"source"`
ProviderType string `json:"providerType"`
AccessProviderType string `json:"accessProviderType"`
DeployCategory string `json:"deployCategory"`
DisplayNameKey string `json:"displayNameKey"`
AccessDisplayNameKey string `json:"accessDisplayNameKey"`
Icon string `json:"icon,omitempty"`
I18n map[string]map[string]string `json:"i18n,omitempty"`
AccessUsages []string `json:"accessUsages,omitempty"`
Priority int `json:"priority"`
Description string `json:"description,omitempty"`
Deployers []string `json:"deployers,omitempty"`
}
type Catalog struct {
mu sync.RWMutex
entries []*CatalogEntry
}
func NewCatalog() *Catalog {
return &Catalog{}
}
func (c *Catalog) Add(entry *CatalogEntry) {
c.mu.Lock()
defer c.mu.Unlock()
c.entries = append(c.entries, entry)
c.sortLocked()
c.buildDeployersLocked()
}
func (c *Catalog) sortLocked() {
sort.SliceStable(c.entries, func(i, j int) bool {
if c.entries[i].Priority != c.entries[j].Priority {
return c.entries[i].Priority > c.entries[j].Priority
}
return c.entries[i].ProviderType < c.entries[j].ProviderType
})
}
func (c *Catalog) buildDeployersLocked() {
byAccess := make(map[string][]string)
for _, e := range c.entries {
byAccess[e.AccessProviderType] = append(byAccess[e.AccessProviderType], e.ProviderType)
}
for _, e := range c.entries {
e.Deployers = byAccess[e.ProviderType]
}
}
func (c *Catalog) Entries() []CatalogEntry {
c.mu.RLock()
defer c.mu.RUnlock()
out := make([]CatalogEntry, len(c.entries))
for i, e := range c.entries {
out[i] = *e
}
return out
}
func (c *Catalog) Remove(providerType string) {
c.mu.Lock()
defer c.mu.Unlock()
for i, e := range c.entries {
if e.ProviderType == providerType {
c.entries = append(c.entries[:i], c.entries[i+1:]...)
c.buildDeployersLocked()
return
}
}
}
func (c *Catalog) I18nBundles() map[string]map[string]map[string]string {
c.mu.RLock()
defer c.mu.RUnlock()
out := make(map[string]map[string]map[string]string, len(c.entries))
for _, e := range c.entries {
if len(e.I18n) == 0 {
continue
}
out[e.ProviderType] = e.I18n
}
return out
}