Skip to content

Commit

Permalink
JSON output prefix update (#2548)
Browse files Browse the repository at this point in the history
* JSON flag option parsing

* Module prefix update for json output

* Update integration tests to validate json output
  • Loading branch information
denis256 committed Apr 28, 2023
1 parent 208e6b2 commit d185ed9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion cli/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,16 @@ func parseTerragruntOptionsFromArgs(terragruntVersion string, args []string, wri
opts.Debug = true
}

jsonOutput := false
for _, arg := range args {
if strings.EqualFold(arg, "-json") {
jsonOutput = true
break
}
}

includeModulePrefix := parseBooleanArg(args, optTerragruntIncludeModulePrefix, os.Getenv("TERRAGRUNT_INCLUDE_MODULE_PREFIX") == "true" || os.Getenv("TERRAGRUNT_INCLUDE_MODULE_PREFIX") == "1")
if includeModulePrefix {
if includeModulePrefix && !jsonOutput {
opts.IncludeModulePrefix = true
opts.OutputPrefix = fmt.Sprintf("[%s] ", opts.WorkingDir)
}
Expand Down
5 changes: 4 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5285,7 +5285,10 @@ func TestDependencyOutputModulePrefix(t *testing.T) {
t,
runTerragruntCommand(t, fmt.Sprintf("terragrunt output -no-color -json --terragrunt-include-module-prefix --terragrunt-non-interactive --terragrunt-working-dir %s", app3Path), &stdout, &stderr),
)
assert.Contains(t, stdout.String(), "\"value\": 42")
// validate that output is valid json
outputs := map[string]TerraformOutput{}
require.NoError(t, json.Unmarshal([]byte(stdout.String()), &outputs))
assert.Equal(t, int(outputs["z"].Value.(float64)), 42)
}

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

0 comments on commit d185ed9

Please sign in to comment.