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()) }