Skip to content

Commit

Permalink
document node content by template
Browse files Browse the repository at this point in the history
  • Loading branch information
g-pavlov committed Oct 25, 2020
1 parent cbd750f commit a6b6ac0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ type Template struct {
// Sources maps variable names to ContentSelectors that will be
// used as specification for the content to fetch and assign ot that
// these variables
Sources map[string]*ContentSelector `yaml:"path,omitempty"`
Sources map[string]*ContentSelector `yaml:"sources,omitempty"`
}

// Links defines how document links are processed.
Expand Down
42 changes: 41 additions & 1 deletion pkg/reactor/document_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"text/template"

"github.com/gardener/docforge/pkg/api"
"github.com/gardener/docforge/pkg/jobs"
Expand All @@ -28,6 +30,7 @@ type DocumentWorker struct {
processors.Processor
NodeContentProcessor NodeContentProcessor
GitHubInfoController GitInfoController
templates map[string]*template.Template
}

// DocumentWorkTask implements jobs#Task
Expand Down Expand Up @@ -75,7 +78,44 @@ func (w *DocumentWorker) Work(ctx context.Context, task interface{}, wq jobs.Wor
b.Write(sourceBlob)
}
}
// TODO: implement read by template
if task.Node.Template != nil {
vars := map[string]string{}
for varName, content := range task.Node.Template.Sources {
if sourceBlob, err = w.Reader.Read(ctx, content.Source); err != nil {
return jobs.NewWorkerError(err, 0)
}
if len(sourceBlob) == 0 {
continue
}
if sourceBlob, err = w.NodeContentProcessor.ReconcileLinks(ctx, task.Node, content.Source, sourceBlob); err != nil {
return jobs.NewWorkerError(err, 0)
}
vars[varName] = string(sourceBlob)
}
var (
templateBlob []byte
tmpl *template.Template
)
if tmpl, ok = w.templates[task.Node.Template.Path]; !ok {
// TODO: temporary solution for local templates
if !strings.HasPrefix(task.Node.Template.Path, "https") {
if templateBlob, err = ioutil.ReadFile(task.Node.Template.Path); err != nil {
return jobs.NewWorkerError(err, 0)
}
} else {
if templateBlob, err = w.Reader.Read(ctx, task.Node.Template.Path); err != nil {
return jobs.NewWorkerError(err, 0)
}
}
if tmpl, err = template.New("test").Parse(string(templateBlob)); err != nil {
return jobs.NewWorkerError(err, 0)
}
w.templates[task.Node.Template.Path] = tmpl
}
if err := tmpl.Execute(&b, vars); err != nil {
return jobs.NewWorkerError(err, 0)
}
}
if len(task.Node.Source) > 0 {
if sourceBlob, err = w.Reader.Read(ctx, task.Node.Source); err != nil {
return jobs.NewWorkerError(err, 0)
Expand Down
1 change: 1 addition & 0 deletions pkg/reactor/document_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestDocumentWorkerWork(t *testing.T) {
resourceHandlers: rhRegistry,
},
nil,
nil,
}

testCases := []struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/reactor/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"strings"
"text/template"

"github.com/gardener/docforge/pkg/processors"
"github.com/google/uuid"
Expand Down Expand Up @@ -51,6 +52,7 @@ func NewReactor(o *Options) *Reactor {
NodeContentProcessor: NewNodeContentProcessor(o.ResourcesPath, o.GlobalLinksConfig, downloadController, o.FailFast, o.MarkdownFmt, o.RewriteEmbedded, rhRegistry),
Processor: o.Processor,
GitHubInfoController: gitInfoController,
templates: map[string]*template.Template{},
}
docController := NewDocumentController(worker, o.MaxWorkersCount, o.FailFast)
r := &Reactor{
Expand Down

0 comments on commit a6b6ac0

Please sign in to comment.