jx/internal/resource/document.go
Matthew Rich e695278d0c
All checks were successful
Declarative Tests / test (push) Successful in 48s
go fmt
2024-03-25 13:31:06 -07:00

69 lines
1.4 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package resource
import (
_ "fmt"
"gopkg.in/yaml.v3"
"io"
_ "log"
_ "net/url"
)
type Document struct {
ResourceDecls []Declaration `yaml:"resources"`
}
func NewDocument() *Document {
return &Document{}
}
func (d *Document) Load(r io.Reader) error {
yamlDecoder := yaml.NewDecoder(r)
yamlDecoder.Decode(d)
for i := range d.ResourceDecls {
if _, e := d.ResourceDecls[i].LoadResourceFromYaml(); e != nil {
return e
}
}
return nil
}
func (d *Document) Resources() []Declaration {
return d.ResourceDecls
}
func (d *Document) Apply() error {
for i := range d.ResourceDecls {
if e := d.ResourceDecls[i].Resource().Apply(); e != nil {
return e
}
}
return nil
}
func (d *Document) Generate(w io.Writer) error {
yamlEncoder := yaml.NewEncoder(w)
yamlEncoder.Encode(d)
return yamlEncoder.Close()
}
func (d *Document) AddResourceDeclaration(resourceType string, resourceDeclaration Resource) {
decl := NewDeclaration()
decl.Type = resourceType
decl.Implementation = resourceDeclaration
decl.UpdateYamlFromResource()
d.ResourceDecls = append(d.ResourceDecls, *decl)
}
func (d *Document) AddResource(uri string) error {
//parsedResourceURI, e := url.Parse(uri)
//if e == nil {
decl := NewDeclaration()
decl.SetURI(uri)
decl.UpdateYamlFromResource()
d.ResourceDecls = append(d.ResourceDecls, *decl)
//}
return nil
}