Skip to content

Commit

Permalink
end-to-end dryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
g-pavlov committed Oct 12, 2020
1 parent fa0de29 commit 51d83b6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 18 deletions.
14 changes: 11 additions & 3 deletions cmd/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package app
import (
"context"
"flag"
"io"
"os"

"github.com/gardener/docforge/pkg/hugo"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -92,9 +94,10 @@ func (flags *cmdFlags) Configure(command *cobra.Command) {
// NewOptions creates an options object from flags
func NewOptions(f *cmdFlags) *Options {
var (
tokens map[string]string
metering *Metering
hugoOptions *hugo.Options
tokens map[string]string
metering *Metering
hugoOptions *hugo.Options
dryRunWriter io.Writer
)
if len(f.ghOAuthToken) > 0 {
tokens = map[string]string{
Expand All @@ -115,6 +118,10 @@ func NewOptions(f *cmdFlags) *Options {
}
}

if f.dryRun {
dryRunWriter = os.Stdout
}

return &Options{
DestinationPath: f.destinationPath,
FailFast: f.failFast,
Expand All @@ -125,6 +132,7 @@ func NewOptions(f *cmdFlags) *Options {
MarkdownFmt: f.markdownFmt,
GitHubTokens: tokens,
Metering: metering,
DryRunWriter: dryRunWriter,
Hugo: hugoOptions,
}
}
Expand Down
32 changes: 25 additions & 7 deletions cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"io"
"path/filepath"

"github.com/gardener/docforge/pkg/hugo"
Expand Down Expand Up @@ -30,6 +31,7 @@ type Options struct {
MarkdownFmt bool
GitHubTokens map[string]string
Metering *Metering
DryRunWriter io.Writer
Hugo *hugo.Options
}

Expand All @@ -50,13 +52,23 @@ func NewReactor(ctx context.Context, options *Options) *reactor.Reactor {
ResourceDownloadWorkersCount: options.ResourceDownloadWorkersCount,
MarkdownFmt: options.MarkdownFmt,
Processor: nil,
Writer: &writers.FSWriter{
ResourceHandlers: initResourceHanlders(ctx, options),
DryRunWriter: options.DryRunWriter,
}
if options.DryRunWriter != nil {
o.Writer = &writers.DryRunWriter{
Writer: options.DryRunWriter,
}
o.ResourceDownloadWriter = &writers.DryRunWriter{
Writer: options.DryRunWriter,
}
} else {
o.Writer = &writers.FSWriter{
Root: options.DestinationPath,
},
ResourceDownloadWriter: &writers.FSWriter{
}
o.ResourceDownloadWriter = &writers.FSWriter{
Root: filepath.Join(options.DestinationPath, options.ResourcesPath),
},
ResourceHandlers: initResourceHanlders(ctx, options),
}
}

if options.Hugo != nil {
Expand All @@ -76,8 +88,14 @@ func WithHugo(reactorOptions *reactor.Options, o *Options) {
hugo.NewProcessor(hugoOptions),
},
}
hugoOptions.Writer = &writers.FSWriter{
Root: filepath.Join(o.DestinationPath),
if o.DryRunWriter != nil {
hugoOptions.Writer = &writers.DryRunWriter{
Writer: o.DryRunWriter,
}
} else {
hugoOptions.Writer = &writers.FSWriter{
Root: filepath.Join(o.DestinationPath),
}
}
reactorOptions.Writer = hugo.NewWriter(hugoOptions)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/jobs/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ func TestController(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), tc.timeout)
defer cancel()
job := &Job{
FailFast: tc.failFast,
ID: "Test",
MaxWorkers: tc.workersCount,
MinWorkers: tc.workersCount,
FailFast: tc.failFast,
ID: "Test",
MaxWorkers: tc.workersCount,
MinWorkers: tc.workersCount,
IsWorkerExitsOnEmptyQueue: true,
Worker: WorkerFunc(tc.worker.work),
Queue: NewWorkQueue(tc.tasksCount),
Worker: WorkerFunc(tc.worker.work),
Queue: NewWorkQueue(tc.tasksCount),
}

c := NewController(job)
Expand Down
1 change: 1 addition & 0 deletions pkg/reactor/content_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func (c *NodeContentProcessor) reconcileHTMLLinks(ctx context.Context, docNode *
return documentBytes, errors.ErrorOrNil()
}

// Download represents a resource that can be downloaded
type Download struct {
url string
resourceName string
Expand Down
8 changes: 6 additions & 2 deletions pkg/reactor/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reactor
import (
"context"
"fmt"
"io"

"github.com/gardener/docforge/pkg/processors"
"k8s.io/klog/v2"
Expand All @@ -26,6 +27,7 @@ type Options struct {
ResourceDownloadWriter writers.Writer
Writer writers.Writer
ResourceHandlers []resourcehandlers.ResourceHandler
DryRunWriter io.Writer
}

// NewReactor creates a Reactor from Options
Expand All @@ -44,6 +46,7 @@ func NewReactor(o *Options) *Reactor {
ResourceHandlers: rhRegistry,
DocController: docController,
DownloadController: downloadController,
DryRunWriter: o.DryRunWriter,
}
return r
}
Expand All @@ -55,6 +58,7 @@ type Reactor struct {
localityDomain *localityDomain
DocController DocumentController
DownloadController DownloadController
DryRunWriter io.Writer
}

// Run starts build operation on docStruct
Expand All @@ -77,8 +81,8 @@ func (r *Reactor) Run(ctx context.Context, docStruct *api.Documentation, dryRun
if err != nil {
return err
}
fmt.Println(s)
return nil
r.DryRunWriter.Write([]byte(s))
r.DryRunWriter.Write([]byte("\n\n"))
}

ctx, cancel := context.WithCancel(ctx)
Expand Down
32 changes: 32 additions & 0 deletions pkg/writers/dryRunWriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package writers

import (
"fmt"
"io"

"github.com/gardener/docforge/pkg/api"
)

// DryRunWriter is implementation of Writer interface for writing blobs to the file system
type DryRunWriter struct {
Writer io.Writer
Root string
root *file
}

type file struct {
name string
indent int
}

type folder struct {
file
files []*file
}

func (f *DryRunWriter) Write(name, path string, docBlob []byte, node *api.Node) error {
fmt.Printf("writing %s/%s\n", path, name)
// TODO: write file/folder structure synchronizing parallel write requests
// into a single hierarchy
return nil
}

0 comments on commit 51d83b6

Please sign in to comment.