pulumi/pkg/engine/events.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

82 lines
1.6 KiB
Go

// Copyright 2017, Pulumi Corporation. All rights reserved.
package engine
import (
"fmt"
"github.com/pulumi/pulumi/pkg/diag"
)
// Event represents an event generated by the engine during an operation. The underlying
// type for the `Payload` field will differ depending on the value of the `Type` field
type Event struct {
Type string
Payload interface{}
}
// DiagEventPayload is the payload for an event with type `diag`
type DiagEventPayload struct {
Severity diag.Severity
UseColor bool
Message string
}
func stdOutEventWithColor(s fmt.Stringer) Event {
return Event{Type: "stdoutcolor", Payload: s.String()}
}
func diagDebugEvent(useColor bool, msg string) Event {
return Event{
Type: "diag",
Payload: DiagEventPayload{
Severity: diag.Debug,
UseColor: useColor,
Message: msg,
},
}
}
func diagInfoEvent(useColor bool, msg string) Event {
return Event{
Type: "diag",
Payload: DiagEventPayload{
Severity: diag.Info,
UseColor: useColor,
Message: msg,
},
}
}
func diagInfoerrEvent(useColor bool, msg string) Event {
return Event{
Type: "diag",
Payload: DiagEventPayload{
Severity: diag.Infoerr,
UseColor: useColor,
Message: msg,
},
}
}
func diagErrorEvent(useColor bool, msg string) Event {
return Event{
Type: "diag",
Payload: DiagEventPayload{
Severity: diag.Error,
UseColor: useColor,
Message: msg,
},
}
}
func diagWarningEvent(useColor bool, msg string) Event {
return Event{
Type: "diag",
Payload: DiagEventPayload{
Severity: diag.Warning,
UseColor: useColor,
Message: msg,
},
}
}