Refactor how args are used in the engine

This change pushes the teasing apart of a `pkgarg` from `args` "up"
towards the entry point of the CLI functions.
This commit is contained in:
Matt Ellis 2017-08-22 10:07:25 -07:00
parent 9e5b1987fa
commit 158fe21026
9 changed files with 26 additions and 33 deletions

View file

@ -20,7 +20,8 @@ func newConfigCmd() *cobra.Command {
Use: "config [<key> [value]]",
Short: "Query, set, replace, or unset configuration values",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
info, err := initEnvCmdName(tokens.QName(env), args)
info, err := initEnvCmdName(tokens.QName(env), pkgargFromArgs(args))
if err != nil {
return err
}

View file

@ -45,7 +45,7 @@ func newDeployCmd() *cobra.Command {
"By default, the package to execute is loaded from the current directory. Optionally, an\n" +
"explicit path can be provided using the [package] argument.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
info, err := initEnvCmdName(tokens.QName(env), args)
info, err := initEnvCmdName(tokens.QName(env), pkgargFromArgs(args))
if err != nil {
return err
}

View file

@ -28,7 +28,7 @@ func newDestroyCmd() *cobra.Command {
"Warning: although old snapshots can be used to recreate an environment, this command\n" +
"is generally irreversable and should be used with great care.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
info, err := initEnvCmdName(tokens.QName(env), args)
info, err := initEnvCmdName(tokens.QName(env), pkgargFromArgs(args))
if err != nil {
return err
}

View file

@ -92,15 +92,11 @@ func newEnvCmd() *cobra.Command {
return cmd
}
func initEnvCmd(args []string) (*envCmdInfo, error) {
// Read in the name of the environment to use.
if len(args) == 0 || args[0] == "" {
return nil, goerr.Errorf("missing required environment name")
}
return initEnvCmdName(tokens.QName(args[0]), args[1:])
func initEnvCmd(name string, pkgarg string) (*envCmdInfo, error) {
return initEnvCmdName(tokens.QName(name), pkgarg)
}
func initEnvCmdName(name tokens.QName, args []string) (*envCmdInfo, error) {
func initEnvCmdName(name tokens.QName, pkgarg string) (*envCmdInfo, error) {
// If the name is blank, use the default.
if name == "" {
name = getCurrentEnv()
@ -115,12 +111,6 @@ func initEnvCmdName(name tokens.QName, args []string) (*envCmdInfo, error) {
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{

View file

@ -22,7 +22,13 @@ func newEnvRmCmd() *cobra.Command {
"\n" +
"After this command completes, the environment will no longer be available for deployments.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
info, err := initEnvCmd(args)
if len(args) == 0 || args[0] == "" {
return errors.Errorf("missing required environment name")
}
info, err := initEnvCmd(args[0], pkgargFromArgs(args[1:]))
if err != nil {
return err
}

View file

@ -48,6 +48,14 @@ func NewLumiCmd() *cobra.Command {
return cmd
}
func pkgargFromArgs(args []string) string {
if len(args) > 0 {
return args[0]
}
return ""
}
// 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) {

View file

@ -34,14 +34,8 @@ 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(pkgarg)
result := compile(pkgargFromArgs(args))
if result == nil {
return nil
}
@ -51,7 +45,7 @@ func newPackEvalCmd() *cobra.Command {
// If configuration was requested, load it up and populate the object state.
if configEnv != "" {
envInfo, err := initEnvCmdName(tokens.QName(configEnv), args)
envInfo, err := initEnvCmdName(tokens.QName(configEnv), pkgargFromArgs(args))
if err != nil {
return err
}

View file

@ -37,14 +37,8 @@ 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(pkgarg); comp != nil {
if comp, pkg := prepareCompiler(pkgargFromArgs(args)); comp != nil {
// Now perform the compilation and extract the heap snapshot.
if pkg == nil {
return comp.Verify()

View file

@ -47,7 +47,7 @@ func newPlanCmd() *cobra.Command {
"By default, the package to execute is loaded from the current directory. Optionally, an\n" +
"explicit path can be provided using the [package] argument.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
info, err := initEnvCmdName(tokens.QName(env), args)
info, err := initEnvCmdName(tokens.QName(env), pkgargFromArgs(args))
if err != nil {
return err
}