Don't flow entire args array into the core of the engine

We were passing along the entire args array to the implementation of
most commands, but the only place this was used was to pass one piece
of information to the compiler we create in one case. Let's get
explicit about the stuff we share from the CLI layer into the
implementation of the commands and make this stuff well typed instead
of a bag of strings.
This commit is contained in:
Matt Ellis 2017-08-22 09:55:56 -07:00
parent b00c84a792
commit c3b8972dce
5 changed files with 29 additions and 15 deletions

View file

@ -114,13 +114,20 @@ func initEnvCmdName(name tokens.QName, args []string) (*envCmdInfo, error) {
if checkpoint == nil {
return nil, goerr.Errorf("could not read environment information")
}
var pkgarg string
if len(args) > 0 {
pkgarg = args[0]
}
contract.Assert(target != nil)
contract.Assert(checkpoint != nil)
return &envCmdInfo{
Target: target,
Checkpoint: checkpoint,
Snapshot: snapshot,
Args: args,
PackageArg: pkgarg,
}, nil
}
@ -128,7 +135,7 @@ type envCmdInfo struct {
Target *deploy.Target // the target environment.
Checkpoint *environment.Checkpoint // the full serialized checkpoint from which this came.
Snapshot *deploy.Snapshot // the environment's latest deployment snapshot
Args []string // the args after extracting the environment name
PackageArg string // an optional path to a package to pass to the compiler
}
func confirmPrompt(msg string, name tokens.QName) bool {

View file

@ -48,14 +48,9 @@ func NewLumiCmd() *cobra.Command {
return cmd
}
func prepareCompiler(args []string) (compiler.Compiler, *pack.Package) {
// TODO[pulumi/pulumi-fabric#88]: enable arguments to flow to the package itself. In that case, we want to split the
// arguments at the --, if any, so we can still pass arguments to the compiler itself in these cases.
var pkgarg string
if len(args) > 0 {
pkgarg = args[0]
}
// TODO[pulumi/pulumi-fabric#88]: enable arguments to flow to the package itself. In that case, we want to split the
// arguments at the --, if any, so we can still pass arguments to the compiler itself in these cases.
func prepareCompiler(pkgarg string) (compiler.Compiler, *pack.Package) {
// Create a compiler options object and map any flags and arguments to settings on it.
opts := core.DefaultOptions()
@ -83,9 +78,9 @@ func prepareCompiler(args []string) (compiler.Compiler, *pack.Package) {
// compile just uses the standard logic to parse arguments, options, and to locate/compile a package. It returns the
// compilation result, or nil if an error occurred (in which case, we would expect diagnostics to have been output).
func compile(args []string) *compileResult {
func compile(pkgarg string) *compileResult {
// Prepare the compiler info and, provided it succeeds, perform the compilation.
if comp, pkg := prepareCompiler(args); comp != nil {
if comp, pkg := prepareCompiler(pkgarg); comp != nil {
var b binder.Binder
var pkgsym *symbols.Package
if pkg == nil {

View file

@ -34,8 +34,14 @@ func newPackEvalCmd() *cobra.Command {
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
contract.Assertf(!dotOutput, "TODO[pulumi/pulumi-fabric#235]: DOT files not yet supported")
var pkgarg string
if len(args) > 0 {
pkgarg = args[0]
}
// First, load and compile the package.
result := compile(args)
result := compile(pkgarg)
if result == nil {
return nil
}

View file

@ -37,8 +37,14 @@ func newPackVerifyCmd() *cobra.Command {
// verify creates a compiler, much like compile, but only performs binding and verification on it. If verification
// succeeds, the return value is true; if verification fails, errors will have been output, and the return is false.
func verify(cmd *cobra.Command, args []string) bool {
var pkgarg string
if len(args) > 0 {
pkgarg = args[0]
}
// Prepare the compiler info and, provided it succeeds, perform the verification.
if comp, pkg := prepareCompiler(args); comp != nil {
if comp, pkg := prepareCompiler(pkgarg); comp != nil {
// Now perform the compilation and extract the heap snapshot.
if pkg == nil {
return comp.Verify()

View file

@ -127,7 +127,7 @@ func plan(info *envCmdInfo, opts deployOptions) (*planResult, error) {
}
// First, compile the package, in preparatin for interpreting it and creating resources.
result := compile(info.Args)
result := compile(info.PackageArg)
if result == nil || !result.B.Ctx().Diag.Success() {
return nil, fmt.Errorf("Errors during compilation: %v", result.B.Ctx().Diag.Errors())
}