From 344fd805f4d0fcb2296c25e582e1f3b659c53ca7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 19 Jun 2014 12:17:56 -0700 Subject: [PATCH] command/apply: fail if state doesn't exist and no -init --- command/apply.go | 17 +++++++++++++++-- command/apply_test.go | 2 -- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/command/apply.go b/command/apply.go index cd6f7af9f..3cbe61358 100644 --- a/command/apply.go +++ b/command/apply.go @@ -40,8 +40,6 @@ func (c *ApplyCommand) Run(args []string) int { return 1 } - // TODO: if state, but not exist, -init required - if statePath == "" { c.Ui.Error("-state cannot be blank") return 1 @@ -49,6 +47,21 @@ func (c *ApplyCommand) Run(args []string) int { if stateOutPath == "" { stateOutPath = statePath } + if !init { + if _, err := os.Stat(statePath); err != nil { + c.Ui.Error(fmt.Sprintf( + "There was an error reading the state file. The path\n"+ + "and error are shown below. If you're trying to build a\n"+ + "brand new infrastructure, explicitly pass the '-init'\n"+ + "flag to Terraform to tell it it is okay to build new\n"+ + "state.\n\n"+ + "Path: %s\n"+ + "Error: %s", + statePath, + err)) + return 1 + } + } // Load up the state var state *terraform.State diff --git a/command/apply_test.go b/command/apply_test.go index 236d52ed9..35d5e34d3 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -153,8 +153,6 @@ func TestApply_stateNoExist(t *testing.T) { "-state=idontexist.tfstate", testFixturePath("apply"), } - // TODO - return if code := c.Run(args); code != 1 { t.Fatalf("bad: \n%s", ui.OutputWriter.String()) }