pulumi/pkg/engine/preview.go
Matt Ellis 7587bcd7ec Have engine emit "events" instead of writing to streams
Previously, the engine would write to io.Writer's to display output.
When hosted in `pulumi` these writers were tied to os.Stdout and
os.Stderr, but other applications hosting the engine could send them
other places (e.g. a log to be sent to an another application later).

While much better than just using the ambient streams, this was still
not the best. It would be ideal if the engine could just emit strongly
typed events and whatever is hosting the engine could care about
displaying them.

As a first step down that road, we move to a model where operations on
the engine now take a `chan engine.Event` and during the course of the
operation, events are written to this channel. It is the
responsibility of the caller of the method to read from the channel
until it is closed (singifying that the operation is complete).

The events we do emit are still intermingle presentation with data,
which is unfortunate, but can be improved over time. Most of the
events today are just colorized in the client and printed to stdout or
stderr without much thought.
2017-10-09 18:24:56 -07:00

69 lines
2.3 KiB
Go

// Copyright 2017, Pulumi Corporation. All rights reserved.
package engine
import (
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/diag"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/contract"
)
type PreviewOptions struct {
Package string // the package to compute the preview for
Analyzers []string // an optional set of analyzers to run as part of this deployment.
Debug bool // true to enable resource debugging output.
Parallel int // the degree of parallelism for resource operations (<=1 for serial).
ShowConfig bool // true to show the configuration variables being used.
ShowReplacementSteps bool // true to show the replacement steps in the plan.
ShowSames bool // true to show the resources that aren't updated, in addition to those that are.
Summary bool // true if we should only summarize resources and operations.
}
func (eng *Engine) Preview(environment tokens.QName, events chan Event, opts PreviewOptions) error {
contract.Require(environment != tokens.QName(""), "environment")
contract.Require(events != nil, "events")
info, err := eng.planContextFromEnvironment(environment, opts.Package)
if err != nil {
return err
}
diag := newEventSink(events, diag.FormatOptions{
Colors: true,
Debug: opts.Debug,
})
deployOpts := deployOptions{
Debug: opts.Debug,
Destroy: false,
DryRun: true,
Analyzers: opts.Analyzers,
Parallel: opts.Parallel,
ShowConfig: opts.ShowConfig,
ShowReplacementSteps: opts.ShowReplacementSteps,
ShowSames: opts.ShowSames,
Summary: opts.Summary,
Events: events,
Diag: diag,
}
defer close(events)
result, err := eng.plan(info, deployOpts)
if err != nil {
return err
}
if result != nil {
defer contract.IgnoreClose(result)
if err := eng.printPlan(result); err != nil {
return err
}
}
if !diag.Success() {
// If any error occurred while walking the plan, be sure to let the developer know. Otherwise,
// although error messages may have spewed to the output, the final lines of the plan may look fine.
return errors.New("One or more errors occurred during the creation of this preview")
}
return nil
}