Skip to content

Commit

Permalink
simple file system resource handler
Browse files Browse the repository at this point in the history
  • Loading branch information
g-pavlov committed Oct 26, 2020
1 parent 6f94d12 commit 26c329c
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 34 deletions.
5 changes: 4 additions & 1 deletion cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
//"github.com/gardener/docforge/pkg/metrics"
"github.com/gardener/docforge/pkg/processors"
"github.com/gardener/docforge/pkg/reactor"
"github.com/gardener/docforge/pkg/resourcehandlers/fs"
ghrs "github.com/gardener/docforge/pkg/resourcehandlers/github"

"github.com/google/go-github/v32/github"
Expand Down Expand Up @@ -112,7 +113,9 @@ func WithHugo(reactorOptions *reactor.Options, o *Options) {
// initResourceHandlers initializes the resource handler
// objects used by reactors
func initResourceHandlers(ctx context.Context, o *Options) []resourcehandlers.ResourceHandler {
rhs := []resourcehandlers.ResourceHandler{}
rhs := []resourcehandlers.ResourceHandler{
fs.NewFSResourceHandler(),
}
if o.GitHubTokens != nil {
if token, ok := o.GitHubTokens["github.com"]; ok {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ type Template struct {
// Mandatory
Path string `yaml:"path"`
// Sources maps variable names to ContentSelectors that will be
// used as specification for the content to fetch and assign ot that
// used as specification for the content to fetch and assign to
// these variables
Sources map[string]*ContentSelector `yaml:"sources,omitempty"`
}
Expand Down
29 changes: 16 additions & 13 deletions pkg/reactor/content_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,24 @@ func (c *nodeContentProcessor) resolveLink(ctx context.Context, node *api.Node,
if err != nil {
return nil, text, title, nil, err
}
// can we handle this destination?
if u.IsAbs() && c.resourceHandlers.Get(destination) == nil {
// It's a valid absolute link that is not in our scope. Leave it be.
return &destination, text, title, nil, err
}
// force destination to absolute URL
handler := c.resourceHandlers.Get(contentSourcePath)
if handler == nil {
return &destination, text, title, nil, nil
if u.IsAbs() {
// can we handle changes to this destination?
if c.resourceHandlers.Get(destination) == nil {
// we don't have a handler for it. Leave it be.
return &destination, text, title, nil, err
}
absLink = destination
}
absLink, err = handler.BuildAbsLink(contentSourcePath, destination)
if err != nil {
return nil, text, title, nil, err
if len(absLink) == 0 {
// build absolute path for the destination using contentSourcePath as base
handler := c.resourceHandlers.Get(contentSourcePath)
if handler == nil {
return &destination, text, title, nil, nil
}
if absLink, err = handler.BuildAbsLink(contentSourcePath, destination); err != nil {
return nil, text, title, nil, err
}
}

// rewrite link if required
if gLinks := c.globalLinksConfig; gLinks != nil {
globalRewrites = gLinks.Rewrites
Expand Down
34 changes: 21 additions & 13 deletions pkg/reactor/document_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"sync"
"text/template"

"github.com/gardener/docforge/pkg/api"
Expand All @@ -31,6 +31,7 @@ type DocumentWorker struct {
NodeContentProcessor NodeContentProcessor
GitHubInfoController GitInfoController
templates map[string]*template.Template
rwLock sync.RWMutex
}

// DocumentWorkTask implements jobs#Task
Expand All @@ -53,6 +54,20 @@ func (g *GenericReader) Read(ctx context.Context, source string) ([]byte, error)
return nil, fmt.Errorf("failed to get handler to read from %s", source)
}

func (w *DocumentWorker) getTemplate(name string) *template.Template {
defer w.rwLock.Unlock()
w.rwLock.Lock()
if tmpl, ok := w.templates[name]; ok {
return tmpl
}
return nil
}
func (w *DocumentWorker) setTemplate(name string, tmpl *template.Template) {
defer w.rwLock.Unlock()
w.rwLock.Lock()
w.templates[name] = tmpl
}

// Work implements Worker#Work function
func (w *DocumentWorker) Work(ctx context.Context, task interface{}, wq jobs.WorkQueue) *jobs.WorkerError {
if task, ok := task.(*DocumentWorkTask); ok {
Expand Down Expand Up @@ -96,21 +111,14 @@ func (w *DocumentWorker) Work(ctx context.Context, task interface{}, wq jobs.Wor
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 = w.getTemplate(task.Node.Template.Path); tmpl == nil {
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 {
if tmpl, err = template.New(task.Node.Template.Path).Parse(string(templateBlob)); err != nil {
return jobs.NewWorkerError(err, 0)
}
w.templates[task.Node.Template.Path] = tmpl
w.setTemplate(task.Node.Template.Path, tmpl)
}
if err := tmpl.Execute(&b, vars); err != nil {
return jobs.NewWorkerError(err, 0)
Expand Down
12 changes: 6 additions & 6 deletions pkg/reactor/document_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,27 @@ func TestDocumentWorkerWork(t *testing.T) {
testOutput := "#Heading 1\n"
rhRegistry := resourcehandlers.NewRegistry(&FakeResourceHandler{})
testworker := &DocumentWorker{
&TestWriter{
Writer: &TestWriter{
make(map[string][]byte),
},
&TestReader{
Reader: &TestReader{
make(map[string][]byte),
},
&TestProcessor{
Processor: &TestProcessor{
func(documentBlob []byte, node *api.Node) ([]byte, error) {
return documentBlob, nil
},
},
&nodeContentProcessor{
NodeContentProcessor: &nodeContentProcessor{
downloadController: NewDownloadController(&TestReader{
make(map[string][]byte),
}, &TestWriter{
make(map[string][]byte),
}, 1, false, rhRegistry),
resourceHandlers: rhRegistry,
},
nil,
nil,
GitHubInfoController: nil,
templates: nil,
}

testCases := []struct {
Expand Down
Loading

0 comments on commit 26c329c

Please sign in to comment.