From 78910f83f3c1b1b35990f1ee076256ed5f981fb6 Mon Sep 17 00:00:00 2001 From: Georgi Pavlov Date: Tue, 13 Oct 2020 08:53:18 +0000 Subject: [PATCH] add dedicated flag --resolve --- cmd/app/cmd.go | 4 ++++ cmd/app/factory.go | 2 ++ pkg/reactor/reactor.go | 16 ++++++++++------ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/app/cmd.go b/cmd/app/cmd.go index f07c9d32..fd47e53f 100644 --- a/cmd/app/cmd.go +++ b/cmd/app/cmd.go @@ -22,6 +22,7 @@ type cmdFlags struct { markdownFmt bool ghOAuthToken string dryRun bool + resolve bool clientMetering bool hugo bool hugoPrettyUrls bool @@ -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, @@ -133,6 +136,7 @@ func NewOptions(f *cmdFlags) *Options { GitHubTokens: tokens, Metering: metering, DryRunWriter: dryRunWriter, + Resolve: f.resolve, Hugo: hugoOptions, } } diff --git a/cmd/app/factory.go b/cmd/app/factory.go index df3fe317..1b441cf4 100644 --- a/cmd/app/factory.go +++ b/cmd/app/factory.go @@ -32,6 +32,7 @@ type Options struct { GitHubTokens map[string]string Metering *Metering DryRunWriter io.Writer + Resolve bool Hugo *hugo.Options } @@ -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) diff --git a/pkg/reactor/reactor.go b/pkg/reactor/reactor.go index c79fbd6a..c97d8bee 100644 --- a/pkg/reactor/reactor.go +++ b/pkg/reactor/reactor.go @@ -28,6 +28,7 @@ type Options struct { Writer writers.Writer ResourceHandlers []resourcehandlers.ResourceHandler DryRunWriter writers.DryRunWriter + Resolve bool } // NewReactor creates a Reactor from Options @@ -47,6 +48,7 @@ func NewReactor(o *Options) *Reactor { DocController: docController, DownloadController: downloadController, DryRunWriter: o.DryRunWriter, + Resolve: o.Resolve, } return r } @@ -59,6 +61,7 @@ type Reactor struct { DocController DocumentController DownloadController DownloadController DryRunWriter writers.DryRunWriter + Resolve bool } // Run starts build operation on docStruct @@ -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 } @@ -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")) } @@ -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 @@ -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 } }