add builtin docs
This commit is contained in:
parent
eab06e2f6c
commit
2288f4edd0
69
internal/builtin/builtin.go
Normal file
69
internal/builtin/builtin.go
Normal file
@ -0,0 +1,69 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. 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
|
||||
}
|
49
internal/builtin/builtin_test.go
Normal file
49
internal/builtin/builtin_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. 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)
|
||||
}
|
9
internal/builtin/documents/facter.jx.yaml
Normal file
9
internal/builtin/documents/facter.jx.yaml
Normal file
@ -0,0 +1,9 @@
|
||||
configurations:
|
||||
- name: facts
|
||||
type: exec
|
||||
values:
|
||||
path: /usr/bin/facter
|
||||
args:
|
||||
- "-j"
|
||||
format: "json"
|
||||
|
4
internal/builtin/documents/system.jx.yaml
Normal file
4
internal/builtin/documents/system.jx.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
configurations:
|
||||
- name: system
|
||||
type: system
|
||||
values: {}
|
Loading…
Reference in New Issue
Block a user