0
0
package manifests
import (
"fmt"
"io/fs"
"sync"
apisv1alpha2 "github.com/kcp-dev/sdk/apis/apis/v1alpha2"
"github.com/johnandersen777/socialweb-computer-kcp/deploy"
)
const exportGlob = "*-apiexport.yaml"
var exportSchemas = sync.OnceValues(func() (map[string]map[string]bool, error) {
paths, err := fs.Glob(deploy.FS, exportGlob)
if err != nil {
return nil, fmt.Errorf("manifests: %s: %w", exportGlob, err)
}
out := map[string]map[string]bool{}
for _, path := range paths {
body, err := fs.ReadFile(deploy.FS, path)
if err != nil {
return nil, fmt.Errorf("manifests: %w", err)
}
objs, err := Decode(path, body)
if err != nil {
return nil, err
}
for _, o := range objs {
e, ok := o.Typed.(*apisv1alpha2.APIExport)
if !ok {
continue
}
schemas := map[string]bool{}
for _, r := range e.Spec.Resources {
schemas[r.Schema] = true
}
out[e.Name] = schemas
}
}
return out, nil
})
func ExportSchemas(export string) (map[string]bool, error) {
all, err := exportSchemas()
if err != nil {
return nil, err
}
return all[export], nil
}