Skip to content

Commit

Permalink
add dedicated flag --resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
g-pavlov committed Oct 13, 2020
1 parent 73c3914 commit 78910f8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cmd/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type cmdFlags struct {
markdownFmt bool
ghOAuthToken string
dryRun bool
resolve bool
clientMetering bool
hugo bool
hugoPrettyUrls bool
Expand Down Expand Up @@ -74,6 +75,8 @@ func (flags *cmdFlags) Configure(command *cobra.Command) {
"Applies formatting rules to source markdown.")
command.Flags().BoolVar(&flags.dryRun, "dry-run", false,
"Runs the command end-to-end but instead of writing files, it will output the proejcted file/folder hierarchy to the standard output and statistics for the processing of each file.")
command.Flags().BoolVar(&flags.resolve, "resolve", false,
"Resolves the documentation structure and prints it to the standard output. The resolution expands nodeSelector constructs into node hierarchies.")
command.Flags().IntVar(&flags.minWorkersCount, "min-workers", 10,
"Minimum number of parallel workers.")
command.Flags().IntVar(&flags.maxWorkersCount, "max-workers", 25,
Expand Down Expand Up @@ -133,6 +136,7 @@ func NewOptions(f *cmdFlags) *Options {
GitHubTokens: tokens,
Metering: metering,
DryRunWriter: dryRunWriter,
Resolve: f.resolve,
Hugo: hugoOptions,
}
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Options struct {
GitHubTokens map[string]string
Metering *Metering
DryRunWriter io.Writer
Resolve bool
Hugo *hugo.Options
}

Expand All @@ -55,6 +56,7 @@ func NewReactor(ctx context.Context, options *Options) *reactor.Reactor {
Processor: nil,
ResourceHandlers: initResourceHanlders(ctx, options),
DryRunWriter: dryRunWriters,
Resolve: options.Resolve,
}
if options.DryRunWriter != nil {
o.Writer = dryRunWriters.GetWriter(options.DestinationPath)
Expand Down
16 changes: 10 additions & 6 deletions pkg/reactor/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Options struct {
Writer writers.Writer
ResourceHandlers []resourcehandlers.ResourceHandler
DryRunWriter writers.DryRunWriter
Resolve bool
}

// NewReactor creates a Reactor from Options
Expand All @@ -47,6 +48,7 @@ func NewReactor(o *Options) *Reactor {
DocController: docController,
DownloadController: downloadController,
DryRunWriter: o.DryRunWriter,
Resolve: o.Resolve,
}
return r
}
Expand All @@ -59,6 +61,7 @@ type Reactor struct {
DocController DocumentController
DownloadController DownloadController
DryRunWriter writers.DryRunWriter
Resolve bool
}

// Run starts build operation on docStruct
Expand All @@ -67,7 +70,7 @@ func (r *Reactor) Run(ctx context.Context, docStruct *api.Documentation, dryRun
err error
ld *localityDomain
)
if err := r.Resolve(ctx, docStruct.Root); err != nil {
if err := r.ResolveStructure(ctx, docStruct.Root); err != nil {
return err
}

Expand All @@ -81,12 +84,11 @@ func (r *Reactor) Run(ctx context.Context, docStruct *api.Documentation, dryRun
}
}

if dryRun {
if r.Resolve {
s, err := api.Serialize(docStruct)
if err != nil {
return err
}
// TODO: either reuse dry run's wrapped writer or build different command for that
os.Stdout.Write([]byte(s))
os.Stdout.Write([]byte("\n\n"))
}
Expand All @@ -106,12 +108,12 @@ func (r *Reactor) Run(ctx context.Context, docStruct *api.Documentation, dryRun
return nil
}

// Resolve builds the subnodes hierarchy of a node based on the natural nodes
// ResolveStructure builds the subnodes hierarchy of a node based on the natural nodes
// hierarchy and on rules such as those in NodeSelector.
// The node hierarchy is resolved by an appropriate handler selected based
// on the NodeSelector path URI
// The resulting model is the actual flight plan for replicating resources.
func (r *Reactor) Resolve(ctx context.Context, node *api.Node) error {
func (r *Reactor) ResolveStructure(ctx context.Context, node *api.Node) error {
node.SetParentsDownwards()
if node.NodeSelector != nil {
var handler resourcehandlers.ResourceHandler
Expand All @@ -121,10 +123,12 @@ func (r *Reactor) Resolve(ctx context.Context, node *api.Node) error {
if err := handler.ResolveNodeSelector(ctx, node); err != nil {
return err
}
// remove node selctors after resolution
node.NodeSelector = nil
}
if len(node.Nodes) > 0 {
for _, n := range node.Nodes {
if err := r.Resolve(ctx, n); err != nil {
if err := r.ResolveStructure(ctx, n); err != nil {
return err
}
}
Expand Down

0 comments on commit 78910f8

Please sign in to comment.