jx/internal/resource/document.go
2024-03-20 12:23:31 -07:00

68 lines
1.5 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package resource
import (
_ "fmt"
_ "log"
"io"
"gopkg.in/yaml.v3"
_ "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
}