Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command: adjust exit code of state rm #22300

Merged
merged 8 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions command/state_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,21 @@ func (c *StateRmCommand) Run(args []string) int {
return 1
}

if len(diags) > 0 {
if len(diags) > 0 && isCount != 0 {
c.showDiagnostics(diags)
}

if isCount == 0 {
c.Ui.Output("No matching resource instances found.")
} else {
c.Ui.Output(fmt.Sprintf("Successfully removed %d resource instance(s).", isCount))
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid target address",
"No matching objects found. To view the available instances, use \"terraform state list\". Please modify the address to reference a specific instance.",
))
c.showDiagnostics(diags)
return 1
}

c.Ui.Output(fmt.Sprintf("Successfully removed %d resource instance(s).", isCount))
return 0
}

Expand Down
9 changes: 2 additions & 7 deletions command/state_rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,9 @@ func TestStateRmNonExist(t *testing.T) {
"-state", statePath,
"test_instance.baz", // doesn't exist in the state constructed above
}
if code := c.Run(args); code != 0 {
t.Fatalf("expected exit status %d, got: %d", 0, code)
}

if msg := ui.OutputWriter.String(); !strings.Contains(msg, "No matching resource instances found") {
t.Fatalf("unexpected output:\n%s", msg)
if code := c.Run(args); code != 1 {
t.Fatalf("expected exit status %d, got: %d", 1, code)
}

}

func TestStateRm_backupExplicit(t *testing.T) {
Expand Down