69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package source
|
|
|
|
import (
|
|
_ "context"
|
|
_ "encoding/json"
|
|
_ "fmt"
|
|
_ "gopkg.in/yaml.v3"
|
|
"net/url"
|
|
_ "path/filepath"
|
|
"decl/internal/resource"
|
|
_ "os"
|
|
_ "io"
|
|
"log/slog"
|
|
)
|
|
|
|
type Group struct {
|
|
GroupType resource.GroupType `yaml:"type" json:"type"`
|
|
}
|
|
|
|
func NewGroup() *Group {
|
|
return &Group{ GroupType: resource.SystemGroupType }
|
|
}
|
|
|
|
func init() {
|
|
SourceTypes.Register([]string{"group"}, func(u *url.URL) DocSource {
|
|
groupSource := NewGroup()
|
|
groupType := u.Query().Get("type")
|
|
if len(groupType) > 0 {
|
|
groupSource.GroupType = resource.GroupType(groupType)
|
|
}
|
|
return groupSource
|
|
})
|
|
|
|
}
|
|
|
|
func (g *Group) Type() string { return "group" }
|
|
|
|
func (g *Group) ExtractResources(filter ResourceSelector) ([]*resource.Document, error) {
|
|
documents := make([]*resource.Document, 0, 100)
|
|
|
|
slog.Info("group source ExtractResources()", "group", g)
|
|
Groups := make([]*resource.Group, 0, 100)
|
|
cmd := g.GroupType.NewReadGroupsCommand()
|
|
if cmd == nil {
|
|
return documents, resource.ErrUnsupportedGroupType
|
|
}
|
|
if out, err := cmd.Execute(g); err == nil {
|
|
slog.Info("group source ExtractResources()", "output", out)
|
|
if exErr := cmd.Extractor(out, &Groups); exErr != nil {
|
|
return documents, exErr
|
|
}
|
|
document := resource.NewDocument()
|
|
for _, grp := range Groups {
|
|
if grp == nil {
|
|
grp = resource.NewGroup()
|
|
}
|
|
grp.GroupType = g.GroupType
|
|
document.AddResourceDeclaration("group", grp)
|
|
}
|
|
documents = append(documents, document)
|
|
} else {
|
|
slog.Info("group source ExtractResources()", "output", out, "error", err)
|
|
return documents, err
|
|
}
|
|
return documents, nil
|
|
}
|