From 2323e9370a08e74cc3afd4d10776715afb224024 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Fri, 28 Aug 2020 11:05:46 -0400 Subject: [PATCH] command: Warn instead of error for empty output When the output subcommand is called with no arguments, and there are no outputs to show, we previously rendered an error message but returned a non-error status code. This is confusing. This commit changes the text UI to use a warning diagnostic, which makes it clearer that this is a non-error situation. We do not change the exit code or the text of the warning, so hopefully this is not considered a breaking change. --- command/output.go | 18 +++++++++++------- command/output_test.go | 5 ++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/command/output.go b/command/output.go index 9d37c09db..a9d4441be 100644 --- a/command/output.go +++ b/command/output.go @@ -113,13 +113,17 @@ func (c *OutputCommand) Run(args []string) int { } if !jsonOutput && (state.Empty() || len(mod.OutputValues) == 0) { - c.Ui.Error( - "The state file either has no outputs defined, or all the defined\n" + - "outputs are empty. Please define an output in your configuration\n" + - "with the `output` keyword and run `terraform refresh` for it to\n" + - "become available. If you are using interpolation, please verify\n" + - "the interpolated value is not empty. You can use the \n" + - "`terraform console` command to assist.") + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Warning, + "No outputs found", + "The state file either has no outputs defined, or all the defined "+ + "outputs are empty. Please define an output in your configuration "+ + "with the `output` keyword and run `terraform refresh` for it to "+ + "become available. If you are using interpolation, please verify "+ + "the interpolated value is not empty. You can use the "+ + "`terraform console` command to assist.", + )) + c.showDiagnostics(diags) return 0 } diff --git a/command/output_test.go b/command/output_test.go index cbb03ddf8..d8e4b862f 100644 --- a/command/output_test.go +++ b/command/output_test.go @@ -120,7 +120,7 @@ func TestOutput_json(t *testing.T) { } } -func TestOutput_emptyOutputsErr(t *testing.T) { +func TestOutput_emptyOutputs(t *testing.T) { originalState := states.NewState() statePath := testStateFile(t, originalState) @@ -139,6 +139,9 @@ func TestOutput_emptyOutputsErr(t *testing.T) { if code := c.Run(args); code != 0 { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } + if got, want := ui.ErrorWriter.String(), "Warning: No outputs found"; !strings.Contains(got, want) { + t.Fatalf("bad output: expected to contain %q, got:\n%s", want, got) + } } func TestOutput_jsonEmptyOutputs(t *testing.T) {