command/apply: fail if state doesn't exist and no -init

This commit is contained in:
Mitchell Hashimoto 2014-06-19 12:17:56 -07:00
parent 879921d70f
commit 344fd805f4
2 changed files with 15 additions and 4 deletions

View File

@ -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

View File

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