Skip to content

Commit

Permalink
Merge pull request hashicorp#28125 from hashicorp/backport/alisdair/v…
Browse files Browse the repository at this point in the history
…alidate-json-format-version/only-holy-coral

Backport of cli: Add format version to validate -json output into v0.15
  • Loading branch information
alisdair authored Mar 17, 2021
2 parents 705d3b5 + 7af405a commit f40d9ed
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 6,
"warning_count": 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 2,
"warning_count": 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": true,
"error_count": 0,
"warning_count": 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 1,
"warning_count": 0,
Expand Down
1 change: 1 addition & 0 deletions command/testdata/validate-invalid/missing_var/output.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 1,
"warning_count": 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 2,
"warning_count": 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 1,
"warning_count": 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 1,
"warning_count": 0,
Expand Down
1 change: 1 addition & 0 deletions command/testdata/validate-invalid/output.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 1,
"warning_count": 0,
Expand Down
1 change: 1 addition & 0 deletions command/testdata/validate-invalid/outputs/output.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": false,
"error_count": 2,
"warning_count": 0,
Expand Down
1 change: 1 addition & 0 deletions command/testdata/validate-valid/output.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format_version": "0.1",
"valid": true,
"error_count": 0,
"warning_count": 0,
Expand Down
13 changes: 11 additions & 2 deletions command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ func (c *ValidateCommand) validate(dir string) tfdiags.Diagnostics {
func (c *ValidateCommand) showResults(diags tfdiags.Diagnostics, jsonOutput bool) int {
switch {
case jsonOutput:
// FormatVersion represents the version of the json format and will be
// incremented for any change to this format that requires changes to a
// consuming parser.
const FormatVersion = "0.1"

type Output struct {
FormatVersion string `json:"format_version"`

// We include some summary information that is actually redundant
// with the detailed diagnostics, but avoids the need for callers
// to re-implement our logic for deciding these.
Expand All @@ -129,8 +136,10 @@ func (c *ValidateCommand) showResults(diags tfdiags.Diagnostics, jsonOutput bool
Diagnostics []*viewsjson.Diagnostic `json:"diagnostics"`
}

var output Output
output.Valid = true // until proven otherwise
output := Output{
FormatVersion: FormatVersion,
Valid: true, // until proven otherwise
}
configSources := c.configSources()
for _, diag := range diags {
output.Diagnostics = append(output.Diagnostics, viewsjson.NewDiagnostic(diag, configSources))
Expand Down
6 changes: 6 additions & 0 deletions website/docs/cli/commands/validate.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ to the JSON output setting. For that reason, external software consuming
Terraform's output should be prepared to find data on stdout that _isn't_ valid
JSON, which it should then treat as a generic error case.

**Note:** The output includes a `format_version` key, which currently has major
version zero to indicate that the format is experimental and subject to change.
A future version will assign a non-zero major version and make stronger
promises about compatibility. We do not anticipate any significant breaking
changes to the format before its first major version, however.

In the normal case, Terraform will print a JSON object to the standard output
stream. The top-level JSON object will have the following properties:

Expand Down

0 comments on commit f40d9ed

Please sign in to comment.