2024-04-18 00:07:12 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "context"
|
|
|
|
_ "encoding/json"
|
|
|
|
_ "fmt"
|
|
|
|
_ "gopkg.in/yaml.v3"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"decl/internal/resource"
|
2024-05-12 08:20:51 +00:00
|
|
|
"decl/internal/transport"
|
2024-05-14 18:26:05 +00:00
|
|
|
"decl/internal/codec"
|
2024-04-18 00:07:12 +00:00
|
|
|
"regexp"
|
2024-05-12 08:20:51 +00:00
|
|
|
_ "os"
|
2024-04-18 00:07:12 +00:00
|
|
|
"io"
|
|
|
|
"compress/gzip"
|
|
|
|
"errors"
|
|
|
|
"log/slog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DeclFile struct {
|
|
|
|
Path string `yaml:"path" json:"path"`
|
2024-05-12 08:20:51 +00:00
|
|
|
transport *transport.Reader `yaml:"-" json:"-"`
|
2024-04-18 00:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDeclFile() *DeclFile {
|
|
|
|
return &DeclFile{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
SourceTypes.Register([]string{"decl"}, func(u *url.URL) DocSource {
|
|
|
|
t := NewDeclFile()
|
2024-07-22 22:03:22 +00:00
|
|
|
t.Path,_ = filepath.Abs(filepath.Join(u.Hostname(), u.Path))
|
2024-05-12 08:20:51 +00:00
|
|
|
t.transport,_ = transport.NewReader(u)
|
2024-04-18 00:07:12 +00:00
|
|
|
return t
|
|
|
|
})
|
|
|
|
|
|
|
|
SourceTypes.Register([]string{"yaml","yml","yaml.gz","yml.gz"}, func(u *url.URL) DocSource {
|
|
|
|
t := NewDeclFile()
|
|
|
|
if u.Scheme == "file" {
|
2024-07-22 22:03:22 +00:00
|
|
|
fileAbsolutePath, _ := filepath.Abs(filepath.Join(u.Hostname(), u.Path))
|
2024-04-18 00:07:12 +00:00
|
|
|
t.Path = fileAbsolutePath
|
|
|
|
} else {
|
|
|
|
t.Path = filepath.Join(u.Hostname(), u.Path)
|
|
|
|
}
|
2024-05-12 08:20:51 +00:00
|
|
|
t.transport,_ = transport.NewReader(u)
|
2024-04-18 00:07:12 +00:00
|
|
|
return t
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *DeclFile) Type() string { return "decl" }
|
|
|
|
|
|
|
|
func (d *DeclFile) ExtractResources(filter ResourceSelector) ([]*resource.Document, error) {
|
|
|
|
documents := make([]*resource.Document, 0, 100)
|
|
|
|
|
|
|
|
GzipFileName := regexp.MustCompile(`^.*\.gz$`)
|
2024-05-12 08:20:51 +00:00
|
|
|
|
|
|
|
defer d.transport.Close()
|
|
|
|
|
2024-04-18 00:07:12 +00:00
|
|
|
var fileReader io.Reader
|
|
|
|
|
|
|
|
if GzipFileName.FindString(d.Path) == d.Path {
|
|
|
|
slog.Info("decompressing gzip", "path", d.Path)
|
2024-05-12 08:20:51 +00:00
|
|
|
zr, err := gzip.NewReader(d.transport)
|
2024-04-18 00:07:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return documents, err
|
|
|
|
}
|
|
|
|
fileReader = zr
|
|
|
|
} else {
|
2024-05-12 08:20:51 +00:00
|
|
|
fileReader = d.transport
|
2024-04-18 00:07:12 +00:00
|
|
|
}
|
2024-05-12 08:20:51 +00:00
|
|
|
|
2024-05-14 18:26:05 +00:00
|
|
|
decoder := codec.NewYAMLDecoder(fileReader)
|
2024-04-21 06:13:17 +00:00
|
|
|
slog.Info("ExtractResources()", "documents", documents)
|
2024-04-18 00:07:12 +00:00
|
|
|
index := 0
|
|
|
|
for {
|
2024-04-21 06:13:17 +00:00
|
|
|
doc := resource.NewDocument()
|
2024-04-18 00:07:12 +00:00
|
|
|
e := decoder.Decode(doc)
|
2024-07-17 08:34:57 +00:00
|
|
|
slog.Info("ExtractResources().Decode()", "document", doc, "error", e)
|
2024-04-18 00:07:12 +00:00
|
|
|
if errors.Is(e, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
return documents, e
|
|
|
|
}
|
2024-04-21 06:13:17 +00:00
|
|
|
slog.Info("ExtractResources()", "res", doc.ResourceDecls[0].Attributes)
|
2024-04-18 00:07:12 +00:00
|
|
|
if validationErr := doc.Validate(); validationErr != nil {
|
|
|
|
return documents, validationErr
|
|
|
|
}
|
2024-04-21 06:13:17 +00:00
|
|
|
documents = append(documents, doc)
|
2024-04-18 00:07:12 +00:00
|
|
|
index++
|
|
|
|
}
|
|
|
|
return documents, nil
|
|
|
|
}
|