Supply diag.Sink at backend construction time

This change eliminates the diag.Sink field, Diag, on the Compiland struct.
Instead, we should provide it at backend provider construction time.  This
is consistent with how other phases of the compiler work and also ensures
the backends can properly implement the core.Phase interface.
This commit is contained in:
joeduffy 2016-11-19 11:28:40 -08:00
parent a23d151610
commit ac5df8ba40
4 changed files with 14 additions and 8 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/marapongo/mu/pkg/compiler/backends/clouds"
"github.com/marapongo/mu/pkg/compiler/core"
"github.com/marapongo/mu/pkg/compiler/predef"
"github.com/marapongo/mu/pkg/diag"
"github.com/marapongo/mu/pkg/errors"
)
@ -20,15 +21,20 @@ import (
// Lambda, and so on, for the actual services in those stack templates.
//
// For more details, see https://github.com/marapongo/mu/blob/master/docs/targets.md#amazon-web-services-aws
func New() clouds.Cloud {
return &awsCloud{}
func New(d diag.Sink) clouds.Cloud {
return &awsCloud{d: d}
}
type awsCloud struct {
clouds.Cloud
d diag.Sink
// TODO: support cloud provider options (e.g., ranging from simple like YAML vs. JSON to complex like IAM).
}
func (c *awsCloud) Diag() diag.Sink {
return c.d
}
func (c *awsCloud) CodeGen(comp core.Compiland) {
// For now, this routine simply generates the equivalent CloudFormation stack for the input. Eventually this needs
// to do a whole lot more, which the following running list of TODOs will serve as a reminder about:
@ -41,7 +47,7 @@ func (c *awsCloud) CodeGen(comp core.Compiland) {
// TODO: actually save this (and any other outputs) to disk, rather than spewing to STDOUT.
y, err := yaml.Marshal(cf)
if err != nil {
comp.Diag.Errorf(ErrorMarshalingCloudFormationTemplate.WithDocument(comp.Doc), err)
c.Diag().Errorf(ErrorMarshalingCloudFormationTemplate.WithDocument(comp.Doc), err)
return
}
fmt.Printf("%v:\n", nm)

View file

@ -9,14 +9,15 @@ import (
"github.com/marapongo/mu/pkg/compiler/backends/clouds/aws"
"github.com/marapongo/mu/pkg/compiler/backends/schedulers"
"github.com/marapongo/mu/pkg/compiler/core"
"github.com/marapongo/mu/pkg/diag"
)
func New(arch Arch) core.Backend {
func New(arch Arch, d diag.Sink) core.Backend {
var be core.Backend
switch arch.Cloud {
case clouds.AWSArch:
be = aws.New()
be = aws.New(d)
case clouds.NoArch:
glog.Fatalf("Expected a valid cloud architecture for backends.New")
default:

View file

@ -117,8 +117,8 @@ func (c *compiler) buildDocument(doc *diag.Document, outp string) {
}
// Now get the backend cloud provider to process the stack from here on out.
be := backends.New(arch)
be.CodeGen(core.Compiland{c.Diag(), target, doc, stack})
be := backends.New(arch, c.Diag())
be.CodeGen(core.Compiland{target, doc, stack})
}
}

View file

@ -16,7 +16,6 @@ type Backend interface {
// Compiland contains all of settings passed from front-end to back-end compiler phases.
type Compiland struct {
Diag diag.Sink // a shared diagnostics sink to use for warnings, errors, etc.
Target *ast.Target // the compilation target.
Doc *diag.Document // the document from which the root stack came.
Stack *ast.Stack // the root stack to compile.