77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package fan
|
||
|
|
||
|
import (
|
||
|
_ "context"
|
||
|
_ "encoding/json"
|
||
|
"fmt"
|
||
|
_ "gopkg.in/yaml.v3"
|
||
|
"net/url"
|
||
|
_ "path/filepath"
|
||
|
"decl/internal/data"
|
||
|
"decl/internal/resource"
|
||
|
"decl/internal/folio"
|
||
|
_ "os"
|
||
|
_ "io"
|
||
|
"log/slog"
|
||
|
)
|
||
|
|
||
|
type Package struct {
|
||
|
PackageType resource.PackageType `yaml:"type" json:"type"`
|
||
|
}
|
||
|
|
||
|
func NewPackage() *Package {
|
||
|
return &Package{ PackageType: resource.SystemPackageType }
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
folio.DocumentRegistry.ConverterTypes.Register([]string{"package"}, func(u *url.URL) data.Converter {
|
||
|
p := NewPackage()
|
||
|
packageType := u.Query().Get("type")
|
||
|
if len(packageType) > 0 {
|
||
|
p.PackageType = resource.PackageType(packageType)
|
||
|
}
|
||
|
return p
|
||
|
})
|
||
|
|
||
|
}
|
||
|
|
||
|
func (p *Package) Type() data.TypeName { return "package" }
|
||
|
|
||
|
func (p *Package) Extract(sourceResource data.Resource, filter data.ElementSelector) (document data.Document, err error) {
|
||
|
slog.Info("fan.Package.Extract()", "package", p)
|
||
|
|
||
|
installedPackages := make([]*resource.Package, 0, 100)
|
||
|
cmd := p.PackageType.NewReadPackagesCommand()
|
||
|
if cmd == nil {
|
||
|
return document, fmt.Errorf("%w: %s", resource.ErrUnsupportedPackageType, p.PackageType)
|
||
|
}
|
||
|
|
||
|
var out []byte
|
||
|
if out, err = cmd.Execute(p); err == nil {
|
||
|
slog.Info("fan.Package.Extract()", "output", out)
|
||
|
if err = cmd.Extractor(out, &installedPackages); err == nil {
|
||
|
document = folio.DocumentRegistry.NewDocument("file://-")
|
||
|
for _, pkg := range installedPackages {
|
||
|
if pkg == nil {
|
||
|
pkg = resource.NewPackage()
|
||
|
}
|
||
|
if _, err = document.NewResource(pkg.URI()); err != nil {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
slog.Info("fan.Package.Extract()", "output", out, "error", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (p *Package) Emit(document data.Document, filter data.ElementSelector) (resourceTarget data.Resource, err error) {
|
||
|
return nil, data.ErrUnsupportedConversion
|
||
|
}
|
||
|
|
||
|
func (p *Package) Close() error {
|
||
|
return nil
|
||
|
}
|