jx/internal/resource/document.go

151 lines
3.2 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 (
"encoding/json"
2024-04-19 07:52:10 +00:00
"fmt"
2024-03-25 20:31:06 +00:00
"gopkg.in/yaml.v3"
"io"
2024-04-09 19:30:05 +00:00
"log/slog"
_ "net/url"
2024-04-19 07:52:10 +00:00
"github.com/sters/yaml-diff/yamldiff"
"strings"
2024-03-20 16:15:27 +00:00
)
type Document struct {
ResourceDecls []Declaration `json:"resources" 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
}
2024-04-19 07:52:10 +00:00
func (d *Document) Clone() *Document {
clone := NewDocument()
clone.ResourceDecls = make([]Declaration, len(d.ResourceDecls))
for i, res := range d.ResourceDecls {
clone.ResourceDecls[i] = *res.Clone()
}
return clone
}
2024-03-20 16:15:27 +00:00
func (d *Document) Load(r io.Reader) error {
2024-04-05 17:22:17 +00:00
c := NewYAMLDecoder(r)
return c.Decode(d);
}
func (d *Document) Validate() error {
jsonDocument, jsonErr := d.JSON()
2024-04-09 19:30:05 +00:00
slog.Info("document.Validate() json", "json", jsonDocument, "err", jsonErr)
if jsonErr == nil {
2024-04-09 19:30:05 +00:00
s := NewSchema("document")
err := s.Validate(string(jsonDocument))
if err != nil {
return err
}
2024-04-09 19:30:05 +00:00
/*
for i := range d.ResourceDecls {
if e := d.ResourceDecls[i].Resource().Validate(); e != nil {
return fmt.Errorf("failed to validate resource %s; %w", d.ResourceDecls[i].Resource().URI(), e)
}
2024-03-25 20:31:06 +00:00
}
2024-04-09 19:30:05 +00:00
*/
2024-03-25 20:31:06 +00:00
}
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-04-21 06:13:17 +00:00
if d == nil {
panic("Undefined Document")
}
slog.Info("Document.Apply()", "declarations", d)
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 {
e := NewYAMLEncoder(w)
2024-04-03 19:27:16 +00:00
err := e.Encode(d);
if err == nil {
return e.Close()
}
e.Close()
return err
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 = TypeName(resourceType)
decl.Attributes = resourceDeclaration
2024-03-25 20:31:06 +00:00
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()
2024-04-03 19:27:16 +00:00
if e := decl.SetURI(uri); e != nil {
return e
}
2024-03-25 20:31:06 +00:00
d.ResourceDecls = append(d.ResourceDecls, *decl)
//}
return nil
2024-03-20 19:23:31 +00:00
}
func (d *Document) JSON() ([]byte, error) {
return json.Marshal(d)
}
func (d *Document) YAML() ([]byte, error) {
return yaml.Marshal(d)
}
2024-04-19 07:52:10 +00:00
func (d *Document) Diff(with *Document, output io.Writer) (string, error) {
opts := []yamldiff.DoOptionFunc{}
if output == nil {
output = &strings.Builder{}
}
ydata, yerr := d.YAML()
if yerr != nil {
return "", yerr
}
yamlDiff,yamlDiffErr := yamldiff.Load(string(ydata))
if yamlDiffErr != nil {
return "", yamlDiffErr
}
wdata,werr := with.YAML()
if werr != nil {
return "", werr
}
withDiff,withDiffErr := yamldiff.Load(string(wdata))
if withDiffErr != nil {
return "", withDiffErr
}
for _,diff := range yamldiff.Do(yamlDiff, withDiff, opts...) {
slog.Info("Diff()", "diff", diff)
2024-04-21 18:06:53 +00:00
fmt.Printf("yaml %#v with %#v\n", yamlDiff, withDiff)
2024-04-22 06:11:17 +00:00
_,e := output.Write([]byte(diff.Dump()))
if e != nil {
return "", e
}
2024-04-19 07:52:10 +00:00
}
slog.Info("Document.Diff() ", "document.yaml", ydata, "with.yaml", wdata)
if stringOutput, ok := output.(*strings.Builder); ok {
return stringOutput.String(), nil
}
return "", nil
}