diff --git a/internal/builtin/builtin.go b/internal/builtin/builtin.go new file mode 100644 index 0000000..3ae4377 --- /dev/null +++ b/internal/builtin/builtin.go @@ -0,0 +1,69 @@ +// Copyright 2024 Matthew Rich . All rights reserved. + +package builtin + +import ( +_ "context" +_ "encoding/json" + "fmt" +_ "gopkg.in/yaml.v3" +_ "net/url" +_ "regexp" +_ "strings" +_ "os" +_ "io" +_ "compress/gzip" +_ "archive/tar" +_ "errors" +_ "path/filepath" +_ "decl/internal/codec" + "decl/internal/data" + "decl/internal/fs" + "decl/internal/folio" +_ "decl/internal/resource" +_ "decl/internal/config" +_ "decl/internal/fan" + "embed" + "log/slog" +) + +//go:embed documents/*.jx.yaml +var documentFiles embed.FS + +func Load(uri folio.URI) (documents []data.Document, err error) { + var extractor data.Converter + var sourceResource data.Resource + if extractor, err = folio.DocumentRegistry.ConverterTypes.New(string(uri)); err == nil { + slog.Info("Load() extractor", "error", err) + + targetDeclaration := folio.NewDeclaration() + if err = targetDeclaration.NewResource((*string)(&uri)); err == nil { + slog.Info("Load() extract many", "resource", sourceResource, "error", err, "uri", uri, "extractor", extractor) + sourceResource = targetDeclaration.Attributes + sourceResource.(data.FileResource).SetFS(documentFiles) + documents, err = extractor.(data.ManyExtractor).ExtractMany(sourceResource, nil) + slog.Info("Load() extract many", "resource", sourceResource, "error", err) + } + } + return +} + +func BuiltInDocuments() (documents []data.Document, err error) { + + docFs := fs.NewWalkDir(documentFiles, "", func(fsys fs.FS, path string, file fs.DirEntry) (err error) { + u := folio.URI(fmt.Sprintf("file://%s", path)) + slog.Info("BuiltInDocuments()", "file", u) + + if ! file.IsDir() { + if loadDocs, loadErr := Load(u); loadErr == nil { + documents = append(documents, loadDocs...) + } else { + err = loadErr + } + } + return + }) + + err = docFs.Walk(nil) + return documents, err +} diff --git a/internal/builtin/builtin_test.go b/internal/builtin/builtin_test.go new file mode 100644 index 0000000..72a7754 --- /dev/null +++ b/internal/builtin/builtin_test.go @@ -0,0 +1,49 @@ +// Copyright 2024 Matthew Rich . All rights reserved. + +package builtin + +import ( + "github.com/stretchr/testify/assert" + "log/slog" + "os" + "testing" + "decl/tests/tempdir" + "decl/internal/folio" +) + +var TempDir tempdir.Path = "testbuiltin" + +func TestMain(m *testing.M) { + err := TempDir.Create() + if err != nil || TempDir == "" { + slog.Error("Failed creating temp dir", "error", err) + } + + rc := m.Run() + + TempDir.Remove() + os.Exit(rc) +} + +func TestBuiltInLoad(t *testing.T) { + docs, err := Load("file://documents/facter.jx.yaml") + assert.Nil(t, err) + assert.Greater(t, len(docs), 0) +} + +func TestBuiltInDocuments(t *testing.T) { + docs, err := BuiltInDocuments() + assert.Nil(t, err) + config, ok := folio.DocumentRegistry.GetDocument("file://documents/facter.jx.yaml") + + assert.True(t, ok) + assert.Greater(t, len(docs), 0) + + slog.Info("TestBuiltInDocuments()", "docuemnt", config) + + assert.True(t, config.HasConfig("facts")) + c := config.GetConfig("facts") + v, e := c.GetValue("virtual") + assert.Nil(t, e) + assert.Equal(t, "physical", v) +} diff --git a/internal/builtin/documents/facter.jx.yaml b/internal/builtin/documents/facter.jx.yaml new file mode 100644 index 0000000..688d095 --- /dev/null +++ b/internal/builtin/documents/facter.jx.yaml @@ -0,0 +1,9 @@ +configurations: +- name: facts + type: exec + values: + path: /usr/bin/facter + args: + - "-j" + format: "json" + diff --git a/internal/builtin/documents/system.jx.yaml b/internal/builtin/documents/system.jx.yaml new file mode 100644 index 0000000..986b68a --- /dev/null +++ b/internal/builtin/documents/system.jx.yaml @@ -0,0 +1,4 @@ +configurations: +- name: system + type: system + values: {}