Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-02-01 18:07:30 +01:00
|
|
|
"fmt"
|
2017-02-10 00:56:15 +01:00
|
|
|
"os"
|
2017-01-19 00:52:26 +01:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-01-19 00:52:26 +01:00
|
|
|
"github.com/marapongo/mu/pkg/compiler"
|
2017-01-23 17:10:49 +01:00
|
|
|
"github.com/marapongo/mu/pkg/compiler/core"
|
2017-01-19 00:52:26 +01:00
|
|
|
"github.com/marapongo/mu/pkg/graph"
|
2017-02-10 00:56:15 +01:00
|
|
|
"github.com/marapongo/mu/pkg/graph/dotconv"
|
2017-01-26 03:38:53 +01:00
|
|
|
"github.com/marapongo/mu/pkg/tokens"
|
Revive some compiler tests
This change revives some compiler tests that are still lingering around
from the old architecture, before our latest round of ship burning.
It also fixes up some bugs uncovered during this:
* Don't claim that a symbol's kind is incorrect in the binder error
message when it wasn't found. Instead, say that it was missing.
* Do not attempt to compile if an error was issued during workspace
resolution and/or loading of the Mufile. This leads to trying to
load an empty path and badness quickly ensues (crash).
* Issue an error if the Mufile wasn't found (this got lost apparently).
* Rename the ErrorMissingPackageName message to ErrorInvalidPackageName,
since missing names are now caught by our new fancy decoder that
understands required versus optional fields. We still need to guard
against illegal characters in the name, including the empty string "".
* During decoding, reject !src.IsValid elements. This represents the
zero value and should be treated equivalently to a missing field.
* Do not permit empty strings "" as Names or QNames. The old logic
accidentally permitted them because regexp.FindString("") == "", no
matter the regex!
* Move the TestDiagSink abstraction to a new pkg/util/testutil package,
allowing us to share this common code across multiple package tests.
* Fix up a few messages that needed tidying or to use Infof vs. Info.
The binder tests -- deleted in this -- are about to come back, however,
I am splitting up the changes, since this represents a passing fixed point.
2017-01-27 00:30:08 +01:00
|
|
|
"github.com/marapongo/mu/pkg/util/cmdutil"
|
2017-01-19 00:52:26 +01:00
|
|
|
"github.com/marapongo/mu/pkg/util/contract"
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
)
|
|
|
|
|
2017-01-29 00:01:36 +01:00
|
|
|
func newEvalCmd() *cobra.Command {
|
2017-02-10 00:56:15 +01:00
|
|
|
var dotOutput bool
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
var cmd = &cobra.Command{
|
2017-01-29 00:01:36 +01:00
|
|
|
Use: "eval [blueprint] [-- [args]]",
|
|
|
|
Short: "Evaluate a MuPackage and create its MuGL graph representation",
|
|
|
|
Long: "Evaluate a MuPackage and create its MuGL graph representation.\n" +
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
"\n" +
|
|
|
|
"A graph is a topologically sorted directed-acyclic-graph (DAG), representing a\n" +
|
|
|
|
"collection of resources that may be used in a deployment operation like plan or apply.\n" +
|
|
|
|
"This graph is produced by evaluating the contents of a Mu blueprint package, and\n" +
|
|
|
|
"does not actually perform any updates to the target environment.\n" +
|
|
|
|
"\n" +
|
|
|
|
"By default, a blueprint package is loaded from the current directory. Optionally,\n" +
|
|
|
|
"a path to a blueprint elsewhere can be provided as the [blueprint] argument.",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
// If there's a --, we need to separate out the command args from the stack args.
|
|
|
|
flags := cmd.Flags()
|
|
|
|
dashdash := flags.ArgsLenAtDash()
|
|
|
|
var packArgs []string
|
|
|
|
if dashdash != -1 {
|
|
|
|
packArgs = args[dashdash:]
|
|
|
|
args = args[0:dashdash]
|
|
|
|
}
|
|
|
|
|
2017-01-19 00:52:26 +01:00
|
|
|
// Create a compiler options object and map any flags and arguments to settings on it.
|
2017-01-23 17:10:49 +01:00
|
|
|
opts := core.DefaultOptions()
|
2017-01-19 00:52:26 +01:00
|
|
|
opts.Args = dashdashArgsToMap(packArgs)
|
|
|
|
|
|
|
|
// In the case of an argument, load that specific package and new up a compiler based on its base path.
|
|
|
|
// Otherwise, use the default workspace and package logic (which consults the current working directory).
|
|
|
|
var mugl graph.Graph
|
|
|
|
if len(args) == 0 {
|
|
|
|
comp, err := compiler.Newwd(opts)
|
|
|
|
if err != nil {
|
|
|
|
contract.Failf("fatal: %v", err)
|
|
|
|
}
|
|
|
|
mugl = comp.Compile()
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
} else {
|
2017-01-19 00:52:26 +01:00
|
|
|
fn := args[0]
|
2017-01-26 17:51:22 +01:00
|
|
|
if pkg := cmdutil.ReadPackageFromArg(fn); pkg != nil {
|
|
|
|
var comp compiler.Compiler
|
|
|
|
var err error
|
|
|
|
if fn == "-" {
|
|
|
|
comp, err = compiler.Newwd(opts)
|
|
|
|
} else {
|
|
|
|
comp, err = compiler.New(filepath.Dir(fn), opts)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
contract.Failf("fatal: %v", err)
|
|
|
|
}
|
|
|
|
mugl = comp.CompilePackage(pkg)
|
2017-01-19 00:52:26 +01:00
|
|
|
}
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
}
|
2017-01-19 00:52:26 +01:00
|
|
|
if mugl == nil {
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, serialize that MuGL graph so that it's suitable for printing/serializing.
|
2017-02-10 00:56:15 +01:00
|
|
|
if dotOutput {
|
|
|
|
// Convert the output to a DOT file.
|
|
|
|
if err := dotconv.Print(mugl, os.Stdout); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: failed to write DOT file to output: %v\n", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just print a very basic, yet (hopefully) aesthetically pleasinge, ascii-ization of the graph.
|
|
|
|
shown := make(map[graph.Vertex]bool)
|
|
|
|
for _, root := range mugl.Roots() {
|
|
|
|
printVertex(root, shown, "")
|
|
|
|
}
|
2017-02-01 18:07:30 +01:00
|
|
|
}
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-10 00:56:15 +01:00
|
|
|
cmd.PersistentFlags().BoolVar(
|
|
|
|
&dotOutput, "dot", false,
|
|
|
|
"Output the graph as a DOT digraph (graph description language)")
|
|
|
|
|
Add scaffolding for mu apply, compile, and plan
This adds scaffolding but no real functionality yet, as part of
marapongo/mu#41. I am landing this now because I need to take a
not-so-brief detour to gut and overhaul the core of the existing
compiler (parsing, semantic analysis, binding, code-gen, etc),
including merging the new pkg/pack contents back into the primary
top-level namespaces (like pkg/ast and pkg/encoding).
After that, I can begin driving the compiler to achieve the
desired effects of mu compile, first and foremost, and then plan
and apply later on.
2017-01-17 23:40:55 +01:00
|
|
|
return cmd
|
|
|
|
}
|
2017-01-19 00:52:26 +01:00
|
|
|
|
2017-02-01 18:07:30 +01:00
|
|
|
// printVertex just pretty-prints a graph. The output is not serializable, it's just for display purposes.
|
|
|
|
// TODO: option to print properties.
|
|
|
|
// TODO: full serializability, including a DOT file option.
|
|
|
|
func printVertex(v graph.Vertex, shown map[graph.Vertex]bool, indent string) {
|
|
|
|
s := v.Obj().Type()
|
|
|
|
if shown[v] {
|
|
|
|
fmt.Printf("%v%v: <cycle...>\n", indent, s)
|
|
|
|
} else {
|
|
|
|
shown[v] = true // prevent cycles.
|
|
|
|
fmt.Printf("%v%v:\n", indent, s)
|
|
|
|
for _, out := range v.Outs() {
|
|
|
|
printVertex(out, shown, indent+" -> ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 00:52:26 +01:00
|
|
|
// dashdashArgsToMap is a simple args parser that places incoming key/value pairs into a map. These are then used
|
|
|
|
// during MuPackage compilation as inputs to the main entrypoint function.
|
|
|
|
// TODO: this is fairly rudimentary; we eventually want to support arrays, maps, and complex types.
|
2017-01-26 03:38:53 +01:00
|
|
|
func dashdashArgsToMap(args []string) core.Args {
|
|
|
|
mapped := make(core.Args)
|
2017-01-19 00:52:26 +01:00
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
|
|
|
arg := args[i]
|
|
|
|
|
|
|
|
// Eat - or -- at the start.
|
|
|
|
if arg[0] == '-' {
|
|
|
|
arg = arg[1:]
|
|
|
|
if arg[0] == '-' {
|
|
|
|
arg = arg[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now find a k=v, and split the k/v part.
|
|
|
|
if eq := strings.IndexByte(arg, '='); eq != -1 {
|
|
|
|
// For --k=v, simply store v underneath k's entry.
|
2017-01-26 03:38:53 +01:00
|
|
|
mapped[tokens.Name(arg[:eq])] = arg[eq+1:]
|
2017-01-19 00:52:26 +01:00
|
|
|
} else {
|
|
|
|
if i+1 < len(args) && args[i+1][0] != '-' {
|
|
|
|
// If the next arg doesn't start with '-' (i.e., another flag) use its value.
|
2017-01-26 03:38:53 +01:00
|
|
|
mapped[tokens.Name(arg)] = args[i+1]
|
2017-01-19 00:52:26 +01:00
|
|
|
i++
|
|
|
|
} else if arg[0:3] == "no-" {
|
|
|
|
// For --no-k style args, strip off the no- prefix and store false underneath k.
|
2017-01-26 03:38:53 +01:00
|
|
|
mapped[tokens.Name(arg[3:])] = false
|
2017-01-19 00:52:26 +01:00
|
|
|
} else {
|
|
|
|
// For all other --k args, assume this is a boolean flag, and set the value of k to true.
|
2017-01-26 03:38:53 +01:00
|
|
|
mapped[tokens.Name(arg)] = true
|
2017-01-19 00:52:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapped
|
|
|
|
}
|