jx/internal/fan/group.go

77 lines
1.9 KiB
Go
Raw Normal View History

2024-07-17 08:34:57 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package fan
2024-07-17 08:34:57 +00:00
import (
_ "context"
_ "encoding/json"
_ "fmt"
_ "gopkg.in/yaml.v3"
"net/url"
_ "path/filepath"
"decl/internal/resource"
"decl/internal/folio"
"decl/internal/data"
2024-07-17 08:34:57 +00:00
_ "os"
_ "io"
"log/slog"
)
type Group struct {
GroupType resource.GroupType `yaml:"type" json:"type"`
}
func NewGroup() *Group {
return &Group{ GroupType: resource.SystemGroupType }
}
func init() {
folio.DocumentRegistry.ConverterTypes.Register([]string{"group"}, func(u *url.URL) data.Converter {
2024-07-17 08:34:57 +00:00
groupSource := NewGroup()
groupType := u.Query().Get("type")
if len(groupType) > 0 {
groupSource.GroupType = resource.GroupType(groupType)
}
return groupSource
})
}
func (g *Group) Type() data.TypeName { return "group" }
2024-07-17 08:34:57 +00:00
func (g *Group) Extract(sourceResource data.Resource, filter data.ElementSelector) (document data.Document, err error) {
2024-07-17 08:34:57 +00:00
slog.Info("group source ExtractResources()", "group", g)
Groups := make([]*resource.Group, 0, 100)
cmd := g.GroupType.NewReadGroupsCommand()
if cmd == nil {
return document, resource.ErrUnsupportedGroupType
2024-07-17 08:34:57 +00:00
}
if out, err := cmd.Execute(g); err == nil {
slog.Info("group source Extract()", "output", out)
2024-07-17 08:34:57 +00:00
if exErr := cmd.Extractor(out, &Groups); exErr != nil {
return document, exErr
2024-07-17 08:34:57 +00:00
}
document = folio.DocumentRegistry.NewDocument("group://-")
2024-07-17 08:34:57 +00:00
for _, grp := range Groups {
if grp == nil {
grp = resource.NewGroup()
}
grp.GroupType = g.GroupType
document.AddResourceDeclaration("group", grp)
}
} else {
slog.Info("group source ExtractResources()", "output", out, "error", err)
return document, err
2024-07-17 08:34:57 +00:00
}
return document, nil
}
func (g *Group) Emit(document data.Document, filter data.ElementSelector) (resourceTarget data.Resource, err error) {
return nil, data.ErrUnsupportedConversion
2024-07-17 08:34:57 +00:00
}
func (g *Group) Close() error {
return nil
}