2024-09-24 19:26:40 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"decl/internal/data"
|
|
|
|
"decl/internal/folio"
|
|
|
|
_ "decl/internal/fan"
|
|
|
|
_ "decl/internal/config"
|
|
|
|
_ "decl/internal/resource"
|
|
|
|
"decl/internal/fs"
|
2024-09-25 04:41:26 +00:00
|
|
|
"decl/internal/builtin"
|
2024-09-26 06:46:16 +00:00
|
|
|
"errors"
|
2024-09-24 19:26:40 +00:00
|
|
|
"fmt"
|
|
|
|
"context"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2024-09-26 06:46:16 +00:00
|
|
|
ErrFailedResources error = errors.New("Failed Resources")
|
2024-10-02 20:26:02 +00:00
|
|
|
ErrFailedDocuments error = errors.New("Document errors")
|
2024-09-24 19:26:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
Target folio.URI
|
|
|
|
ImportedMap map[folio.URI]data.Document
|
|
|
|
Documents []data.Document
|
|
|
|
emitter data.Converter
|
|
|
|
merged data.Document
|
|
|
|
Config data.Document
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient() *App {
|
|
|
|
a := &App{ ImportedMap: make(map[folio.URI]data.Document), Documents: make([]data.Document, 0, 100) }
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2024-09-25 04:41:26 +00:00
|
|
|
// Load compiled-in config documents.
|
|
|
|
func (a *App) BuiltInConfiguration() (err error) {
|
|
|
|
var defaultConfigurations []data.Document
|
|
|
|
if defaultConfigurations, err = builtin.BuiltInDocuments(); len(defaultConfigurations) > 0 {
|
|
|
|
slog.Info("Client.BuiltInConfiguration()", "documents", defaultConfigurations, "error", err)
|
|
|
|
a.Config.AppendConfigurations(defaultConfigurations)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:26:40 +00:00
|
|
|
// Load config documents from default system config path. Ignore if missing.
|
|
|
|
func (a *App) SystemConfiguration(configPath string) (err error) {
|
|
|
|
var extractor data.Converter
|
|
|
|
var sourceResource data.Resource
|
|
|
|
if a.Config == nil {
|
|
|
|
a.Config = folio.DocumentRegistry.NewDocument("file:///etc/jx/runtimeconfig.jx.yaml")
|
|
|
|
}
|
|
|
|
if configPath != "" {
|
|
|
|
//configURI := folio.URI(configPath)
|
|
|
|
var loaded []data.Document
|
|
|
|
docFs := fs.NewWalkDir(os.DirFS(configPath), configPath, func(fsys fs.FS, path string, file fs.DirEntry) (loadErr error) {
|
|
|
|
u := folio.URI(fmt.Sprintf("file://%s", path))
|
|
|
|
|
|
|
|
if ! file.IsDir() {
|
2024-09-25 04:41:26 +00:00
|
|
|
slog.Info("Client.SystemConfiguration()", "uri", u)
|
2024-09-24 19:26:40 +00:00
|
|
|
if extractor, loadErr = folio.DocumentRegistry.ConverterTypes.New(string(u)); loadErr == nil {
|
|
|
|
if sourceResource, loadErr = u.NewResource(nil); loadErr == nil {
|
|
|
|
if loaded, loadErr = extractor.(data.ManyExtractor).ExtractMany(sourceResource, nil); loadErr == nil {
|
|
|
|
a.Config.AppendConfigurations(loaded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
err = docFs.Walk(nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) MergeDocuments() {
|
|
|
|
a.merged = folio.DocumentRegistry.NewDocument("file://-")
|
|
|
|
for _, d := range a.Documents {
|
|
|
|
for _, declaration := range d.(*folio.Document).ResourceDeclarations {
|
|
|
|
a.merged.AddDeclaration((data.Declaration)(declaration))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) SetOutput(uri string) (err error) {
|
|
|
|
if uri == "-" {
|
|
|
|
uri = "jx://-"
|
|
|
|
}
|
|
|
|
a.Target = folio.URI(uri)
|
|
|
|
if a.emitter, err = folio.DocumentRegistry.ConverterTypes.New(uri); err != nil {
|
|
|
|
return fmt.Errorf("Failed opening target: %s, %w", uri, err)
|
|
|
|
}
|
2024-10-04 00:30:49 +00:00
|
|
|
slog.Info("Client.SetOutput()", "uri", uri, "emitter", a.emitter)
|
2024-09-24 19:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each document has an `imports` keyword which can be used to load dependencies
|
|
|
|
func (a *App) LoadDocumentImports() error {
|
2024-11-10 18:27:31 +00:00
|
|
|
slog.Info("Client.LoadDocumentImports()", "documents", a.Documents)
|
2024-09-27 02:13:01 +00:00
|
|
|
for i, d := range a.Documents {
|
|
|
|
importedDocs := d.ImportedDocuments()
|
2024-11-10 18:27:31 +00:00
|
|
|
slog.Info("Client.LoadDocumentImports()", "imported", importedDocs)
|
2024-09-27 02:13:01 +00:00
|
|
|
for _, importedDocument := range importedDocs {
|
2024-09-24 19:26:40 +00:00
|
|
|
docURI := folio.URI(importedDocument.GetURI())
|
|
|
|
if _, ok := a.ImportedMap[docURI]; !ok {
|
|
|
|
a.ImportedMap[docURI] = importedDocument
|
2024-09-27 02:13:01 +00:00
|
|
|
a.Documents = append(a.Documents, nil)
|
|
|
|
copy(a.Documents[i+1:], a.Documents[i:])
|
|
|
|
a.Documents[i] = importedDocument
|
|
|
|
/*
|
2024-09-24 19:26:40 +00:00
|
|
|
if _, outputErr := a.emitter.Emit(importedDocument, nil); outputErr != nil {
|
|
|
|
return outputErr
|
|
|
|
}
|
2024-09-27 02:13:01 +00:00
|
|
|
*/
|
2024-09-24 19:26:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *App) ImportResource(ctx context.Context, uri string) (err error) {
|
|
|
|
if len(a.Documents) < 1 {
|
|
|
|
a.Documents = append(a.Documents, folio.DocumentRegistry.NewDocument(""))
|
|
|
|
}
|
|
|
|
resourceURI := folio.URI(uri)
|
2024-10-16 17:26:42 +00:00
|
|
|
u := resourceURI.Parse().URL()
|
2024-09-24 19:26:40 +00:00
|
|
|
if u == nil {
|
|
|
|
return fmt.Errorf("Failed adding resource: %s", uri)
|
|
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "file"
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range a.Documents {
|
|
|
|
if newResource, newResourceErr := d.NewResource(uri); newResourceErr == nil {
|
|
|
|
if _, err = newResource.Read(ctx); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return newResourceErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) ImportSource(uri string) (loadedDocuments []data.Document, err error) {
|
2024-10-16 17:26:42 +00:00
|
|
|
if source := folio.URI(uri).Parse().URL(); source != nil {
|
|
|
|
if source.Scheme == "" {
|
|
|
|
source.Scheme = "file"
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Info("Client.ImportSource()", "uri", uri, "source", source, "error", err)
|
|
|
|
if loadedDocuments, err = folio.DocumentRegistry.LoadFromParsedURI(source); err == nil && loadedDocuments != nil {
|
|
|
|
a.Documents = append(a.Documents, loadedDocuments...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = folio.ErrInvalidURI
|
2024-09-24 19:26:40 +00:00
|
|
|
}
|
2024-10-16 17:26:42 +00:00
|
|
|
|
2024-10-04 00:30:49 +00:00
|
|
|
slog.Info("Client.ImportSource()", "uri", uri, "error", err)
|
2024-09-24 19:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Import(docs []string) (err error) {
|
|
|
|
for _, source := range docs {
|
|
|
|
if _, err = a.ImportSource(source); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Apply(ctx context.Context, deleteResources bool) (err error) {
|
2024-10-02 20:26:02 +00:00
|
|
|
var errorsCount int = 0
|
2024-09-24 19:26:40 +00:00
|
|
|
for _, d := range a.Documents {
|
|
|
|
d.SetConfig(a.Config)
|
|
|
|
|
|
|
|
var overrideState string = ""
|
|
|
|
if deleteResources {
|
|
|
|
overrideState = "delete"
|
|
|
|
}
|
|
|
|
d.ResolveIds(ctx)
|
|
|
|
|
2024-09-25 04:41:26 +00:00
|
|
|
_ = d.Apply("stat")
|
|
|
|
|
2024-09-24 19:26:40 +00:00
|
|
|
if ! d.CheckConstraints() {
|
2024-09-25 04:41:26 +00:00
|
|
|
slog.Info("Client.Apply() document constraints failed", "requires", d)
|
2024-10-02 20:26:02 +00:00
|
|
|
d.AddError(fmt.Errorf("%w: %s", folio.ErrConstraintFailure, d.GetURI()))
|
|
|
|
errorsCount++
|
2024-09-24 19:26:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-09 22:31:39 +00:00
|
|
|
slog.Info("Client.Apply()", "uri", d.GetURI(), "document", d, "state", overrideState, "error", err)
|
2024-09-24 19:26:40 +00:00
|
|
|
if e := d.(*folio.Document).Apply(overrideState); e != nil {
|
|
|
|
slog.Info("Client.Apply() error", "error", e)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if d.Failures() > 0 {
|
2024-10-02 20:26:02 +00:00
|
|
|
d.AddError(fmt.Errorf("%w: %d, %w", ErrFailedResources, d.Failures(), err))
|
|
|
|
errorsCount++
|
2024-09-24 19:26:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-02 20:26:02 +00:00
|
|
|
|
|
|
|
if errorsCount > 0 {
|
|
|
|
return fmt.Errorf("%w: %d", ErrFailedDocuments, errorsCount)
|
|
|
|
}
|
2024-09-24 19:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) ImportCmd(ctx context.Context, docs []string, resourceURI string, quiet bool, merge bool) (err error) {
|
2024-10-04 00:30:49 +00:00
|
|
|
defer a.Close()
|
2024-09-24 19:26:40 +00:00
|
|
|
if err = a.Import(docs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = a.LoadDocumentImports(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resourceURI) > 0 {
|
|
|
|
if err = a.ImportResource(ctx, resourceURI); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if quiet {
|
|
|
|
err = a.Quiet()
|
|
|
|
} else {
|
|
|
|
if merge {
|
|
|
|
a.MergeDocuments()
|
|
|
|
}
|
|
|
|
err = a.Emit()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) ApplyCmd(ctx context.Context, docs []string, quiet bool, deleteResources bool) (err error) {
|
2024-10-04 00:30:49 +00:00
|
|
|
defer a.Close()
|
2024-10-02 20:26:02 +00:00
|
|
|
var failedResources error
|
2024-09-24 19:26:40 +00:00
|
|
|
if err = a.Import(docs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = a.LoadDocumentImports(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-02 20:26:02 +00:00
|
|
|
if failedResources = a.Apply(ctx, deleteResources); failedResources != nil {
|
|
|
|
slog.Info("Client.ApplyCmd()", "client", a, "error", failedResources)
|
|
|
|
if ! errors.Is(failedResources, ErrFailedResources) && ! errors.Is(failedResources, ErrFailedDocuments) {
|
|
|
|
return failedResources
|
2024-09-26 06:46:16 +00:00
|
|
|
}
|
2024-09-24 19:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if quiet {
|
|
|
|
err = a.Quiet()
|
|
|
|
} else {
|
|
|
|
err = a.Emit()
|
2024-10-02 20:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if failedResources != nil {
|
2024-09-24 19:26:40 +00:00
|
|
|
if err != nil {
|
2024-10-02 20:26:02 +00:00
|
|
|
return fmt.Errorf("%w %w", failedResources, err)
|
|
|
|
} else {
|
|
|
|
return failedResources
|
2024-09-24 19:26:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-02 20:26:02 +00:00
|
|
|
|
2024-09-24 19:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Diff(left []data.Document, right []data.Document) (err error) {
|
|
|
|
output := os.Stdout
|
|
|
|
slog.Info("jx diff ", "right", right, "left", left)
|
|
|
|
index := 0
|
|
|
|
for {
|
|
|
|
if index >= len(right) && index >= len(left) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if index >= len(right) {
|
|
|
|
if _, err = left[index].Diff(folio.DocumentRegistry.NewDocument(""), output); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if index >= len(left) {
|
|
|
|
if _, err = folio.DocumentRegistry.NewDocument("").Diff(right[index], output); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err = left[index].Diff(right[index], output); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) DiffCmd(docs []string) (err error) {
|
|
|
|
output := os.Stdout
|
|
|
|
|
|
|
|
var leftDocuments, rightDocuments []data.Document
|
2024-09-25 04:41:26 +00:00
|
|
|
var rightSource folio.URI
|
2024-09-24 19:26:40 +00:00
|
|
|
|
|
|
|
//leftSource := folio.URI(docs[0])
|
2024-09-25 04:41:26 +00:00
|
|
|
if len(docs) > 1 {
|
|
|
|
rightSource = folio.URI(docs[1])
|
|
|
|
}
|
2024-09-24 19:26:40 +00:00
|
|
|
|
|
|
|
if leftDocuments, err = a.ImportSource(docs[0]); err == nil {
|
|
|
|
if rightSource.IsEmpty() {
|
|
|
|
for _, doc := range leftDocuments {
|
|
|
|
_, err = doc.DiffState(output)
|
|
|
|
}
|
|
|
|
} else {
|
2024-09-25 04:41:26 +00:00
|
|
|
if rightDocuments, err = a.ImportSource(docs[1]); err == nil {
|
|
|
|
err = a.Diff(leftDocuments, rightDocuments)
|
|
|
|
}
|
2024-09-24 19:26:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 04:41:26 +00:00
|
|
|
func (a *App) ConfigCmd(docs []string, includeSystemConfig bool) (err error) {
|
2024-10-04 00:30:49 +00:00
|
|
|
defer a.Close()
|
2024-09-25 04:41:26 +00:00
|
|
|
if err = a.BuiltInConfiguration(); err != nil {
|
|
|
|
slog.Warn("BuiltInConfiguration()", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = a.Import(docs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = a.LoadDocumentImports(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if includeSystemConfig {
|
2024-09-27 03:58:14 +00:00
|
|
|
if _, err = a.emitter.Emit(a.Config, nil); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-09-25 04:41:26 +00:00
|
|
|
}
|
|
|
|
_, err = a.emitter.(data.ManyEmitter).EmitMany(a.Documents, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:26:40 +00:00
|
|
|
func (a *App) Quiet() (err error) {
|
|
|
|
output := os.Stdout
|
|
|
|
for _, d := range a.Documents {
|
|
|
|
for _, dr := range d.Declarations() {
|
|
|
|
if _, err = output.Write([]byte(fmt.Sprintf("%s\n", dr.Resource().URI()))); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Emit() (err error) {
|
|
|
|
if a.merged == nil {
|
|
|
|
for _, d := range a.Documents {
|
2024-10-04 00:30:49 +00:00
|
|
|
slog.Info("Client.Emit() document", "document", d)
|
2024-09-24 19:26:40 +00:00
|
|
|
if _, err = a.emitter.Emit(d, nil); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err = a.emitter.Emit(a.merged, nil); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Close() (err error) {
|
2024-10-04 00:30:49 +00:00
|
|
|
if a.emitter != nil {
|
|
|
|
slog.Info("Client.Close() emitter", "emitter", a.emitter)
|
|
|
|
return a.emitter.Close()
|
|
|
|
}
|
|
|
|
return
|
2024-09-24 19:26:40 +00:00
|
|
|
}
|