jx/internal/resource/document.go

69 lines
1.4 KiB
Go
Raw Normal View History

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