64 lines
1.6 KiB
Go
64 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 Package struct {
|
|
PackageType resource.PackageType `yaml:"type" json:"type"`
|
|
}
|
|
|
|
func NewPackage() *Package {
|
|
return &Package{ PackageType: resource.SystemPackageType }
|
|
}
|
|
|
|
func init() {
|
|
SourceTypes.Register([]string{"package"}, func(u *url.URL) DocSource {
|
|
p := NewPackage()
|
|
p.PackageType = resource.PackageType(u.Query().Get("type"))
|
|
return p
|
|
})
|
|
|
|
}
|
|
|
|
func (p *Package) Type() string { return "package" }
|
|
|
|
func (p *Package) ExtractResources(filter ResourceSelector) ([]*resource.Document, error) {
|
|
documents := make([]*resource.Document, 0, 100)
|
|
|
|
slog.Info("package source ExtractResources()", "package", p)
|
|
installedPackages := make([]*resource.Package, 0, 100)
|
|
cmd := p.PackageType.NewReadPackagesCommand()
|
|
if out, err := cmd.Execute(p); err == nil {
|
|
slog.Info("package source ExtractResources()", "output", out)
|
|
if exErr := cmd.Extractor(out, &installedPackages); exErr != nil {
|
|
return documents, exErr
|
|
}
|
|
document := resource.NewDocument()
|
|
for _, pkg := range installedPackages {
|
|
if pkg == nil {
|
|
pkg = resource.NewPackage()
|
|
}
|
|
pkg.PackageType = p.PackageType
|
|
|
|
document.AddResourceDeclaration("package", pkg)
|
|
}
|
|
documents = append(documents, document)
|
|
} else {
|
|
slog.Info("package source ExtractResources()", "output", out, "error", err)
|
|
return documents, err
|
|
}
|
|
return documents, nil
|
|
}
|