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:
parent
9e5b1987fa
commit
158fe21026
9 changed files with 26 additions and 33 deletions
|
@ -20,7 +20,8 @@ func newConfigCmd() *cobra.Command {
|
||||||
Use: "config [<key> [value]]",
|
Use: "config [<key> [value]]",
|
||||||
Short: "Query, set, replace, or unset configuration values",
|
Short: "Query, set, replace, or unset configuration values",
|
||||||
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ func newDeployCmd() *cobra.Command {
|
||||||
"By default, the package to execute is loaded from the current directory. Optionally, an\n" +
|
"By default, the package to execute is loaded from the current directory. Optionally, an\n" +
|
||||||
"explicit path can be provided using the [package] argument.",
|
"explicit path can be provided using the [package] argument.",
|
||||||
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ func newDestroyCmd() *cobra.Command {
|
||||||
"Warning: although old snapshots can be used to recreate an environment, this command\n" +
|
"Warning: although old snapshots can be used to recreate an environment, this command\n" +
|
||||||
"is generally irreversable and should be used with great care.",
|
"is generally irreversable and should be used with great care.",
|
||||||
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,15 +92,11 @@ func newEnvCmd() *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func initEnvCmd(args []string) (*envCmdInfo, error) {
|
func initEnvCmd(name string, pkgarg string) (*envCmdInfo, error) {
|
||||||
// Read in the name of the environment to use.
|
return initEnvCmdName(tokens.QName(name), pkgarg)
|
||||||
if len(args) == 0 || args[0] == "" {
|
|
||||||
return nil, goerr.Errorf("missing required environment name")
|
|
||||||
}
|
|
||||||
return initEnvCmdName(tokens.QName(args[0]), args[1:])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 the name is blank, use the default.
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = getCurrentEnv()
|
name = getCurrentEnv()
|
||||||
|
@ -115,12 +111,6 @@ func initEnvCmdName(name tokens.QName, args []string) (*envCmdInfo, error) {
|
||||||
return nil, goerr.Errorf("could not read environment information")
|
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(target != nil)
|
||||||
contract.Assert(checkpoint != nil)
|
contract.Assert(checkpoint != nil)
|
||||||
return &envCmdInfo{
|
return &envCmdInfo{
|
||||||
|
|
|
@ -22,7 +22,13 @@ func newEnvRmCmd() *cobra.Command {
|
||||||
"\n" +
|
"\n" +
|
||||||
"After this command completes, the environment will no longer be available for deployments.",
|
"After this command completes, the environment will no longer be available for deployments.",
|
||||||
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,14 @@ func NewLumiCmd() *cobra.Command {
|
||||||
return cmd
|
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
|
// 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.
|
// 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) {
|
func prepareCompiler(pkgarg string) (compiler.Compiler, *pack.Package) {
|
||||||
|
|
|
@ -34,14 +34,8 @@ func newPackEvalCmd() *cobra.Command {
|
||||||
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
||||||
contract.Assertf(!dotOutput, "TODO[pulumi/pulumi-fabric#235]: DOT files not yet supported")
|
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.
|
// First, load and compile the package.
|
||||||
result := compile(pkgarg)
|
result := compile(pkgargFromArgs(args))
|
||||||
if result == nil {
|
if result == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -51,7 +45,7 @@ func newPackEvalCmd() *cobra.Command {
|
||||||
|
|
||||||
// If configuration was requested, load it up and populate the object state.
|
// If configuration was requested, load it up and populate the object state.
|
||||||
if configEnv != "" {
|
if configEnv != "" {
|
||||||
envInfo, err := initEnvCmdName(tokens.QName(configEnv), args)
|
envInfo, err := initEnvCmdName(tokens.QName(configEnv), pkgargFromArgs(args))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
// 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.
|
// 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 {
|
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.
|
// 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.
|
// Now perform the compilation and extract the heap snapshot.
|
||||||
if pkg == nil {
|
if pkg == nil {
|
||||||
return comp.Verify()
|
return comp.Verify()
|
||||||
|
|
|
@ -47,7 +47,7 @@ func newPlanCmd() *cobra.Command {
|
||||||
"By default, the package to execute is loaded from the current directory. Optionally, an\n" +
|
"By default, the package to execute is loaded from the current directory. Optionally, an\n" +
|
||||||
"explicit path can be provided using the [package] argument.",
|
"explicit path can be provided using the [package] argument.",
|
||||||
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue