From 04f7281e8c23291347efb519f8032637cee08e83 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 11 Jul 2014 20:51:26 -0700 Subject: [PATCH] command/plan: can take no args and default to pwd for config --- command/plan.go | 28 +++++++++++++++++++++------- command/plan_test.go | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/command/plan.go b/command/plan.go index 06cb9e7bb..e41466087 100644 --- a/command/plan.go +++ b/command/plan.go @@ -33,13 +33,22 @@ func (c *PlanCommand) Run(args []string) int { return 1 } + var path string args = cmdFlags.Args() - if len(args) != 1 { + if len(args) > 1 { c.Ui.Error( - "The plan command expects only one argument with the path\n" + + "The plan command expects at most one argument with the path\n" + "to a Terraform configuration.\n") cmdFlags.Usage() return 1 + } else if len(args) == 1 { + path = args[0] + } else { + var err error + path, err = os.Getwd() + if err != nil { + c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err)) + } } // Load up the state @@ -59,7 +68,7 @@ func (c *PlanCommand) Run(args []string) int { } } - b, err := config.Load(args[0]) + b, err := config.LoadDir(path) if err != nil { c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err)) return 1 @@ -111,10 +120,14 @@ func (c *PlanCommand) Run(args []string) int { func (c *PlanCommand) Help() string { helpText := ` -Usage: terraform plan [options] [terraform.tf] +Usage: terraform plan [options] [dir] - Shows the differences between the Terraform configuration and - the actual state of an infrastructure. + Generates an execution plan for Terraform. + + This execution plan can be reviewed prior to running apply to get a + sense for what Terraform will do. Optionally, the plan can be saved to + a Terraform plan file, and apply can take this plan file to execute + this plan exactly. Options: @@ -127,7 +140,8 @@ Options: -refresh=true Update state prior to checking for differences. -state=statefile Path to a Terraform state file to use to look - up Terraform-managed resources. + up Terraform-managed resources. By default it will + use the state "terraform.tfstate" if it exists. ` return strings.TrimSpace(helpText) diff --git a/command/plan_test.go b/command/plan_test.go index 84963d318..61c22b8a6 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -10,6 +10,29 @@ import ( "github.com/mitchellh/cli" ) +func TestPlan(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + if err := os.Chdir(testFixturePath("plan")); err != nil { + t.Fatalf("err: %s", err) + } + defer os.Chdir(cwd) + + p := testProvider() + ui := new(cli.MockUi) + c := &PlanCommand{ + ContextOpts: testCtxConfig(p), + Ui: ui, + } + + args := []string{} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } +} + func TestPlan_destroy(t *testing.T) { originalState := &terraform.State{ Resources: map[string]*terraform.ResourceState{