Skip to content

Commit

Permalink
command/refresh: better error message if bad state
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jul 12, 2014
1 parent 9a6f1e5 commit ff75d15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
24 changes: 24 additions & 0 deletions command/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ func (c *RefreshCommand) Run(args []string) int {
stateOutPath = statePath
}

// Verify that the state path exists
if _, err := os.Stat(statePath); err != nil {
if os.IsNotExist(err) {
c.Ui.Error(fmt.Sprintf(
"The Terraform state file for your infrastructure does not\n"+
"exist. The 'refresh' command only works and only makes sense\n"+
"when there is existing state that Terraform is managing. Please\n"+
"double-check the value given below and try again. If you\n"+
"haven't created infrastructure with Terraform yet, use the\n"+
"'terraform apply' command.\n\n"+
"Path: %s",
statePath))
return 1
}

c.Ui.Error(fmt.Sprintf(
"There was an error reading the Terraform state that is needed\n"+
"for refreshing. The path and error are shown below.\n\n"+
"Path: %s\n\nError: %s",
statePath,
err))
return 1
}

// Build the context based on the arguments given
c.ContextOpts.Hooks = append(c.ContextOpts.Hooks, &UiHook{Ui: c.Ui})
ctx, err := ContextArg(configPath, statePath, c.ContextOpts)
Expand Down
17 changes: 17 additions & 0 deletions command/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ func TestRefresh(t *testing.T) {
}
}

func TestRefresh_badState(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &RefreshCommand{
ContextOpts: testCtxConfig(p),
Ui: ui,
}

args := []string{
"-state", "i-should-not-exist-ever",
testFixturePath("refresh"),
}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}

func TestRefresh_cwd(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
Expand Down

0 comments on commit ff75d15

Please sign in to comment.