-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
JSON output fetching fixes #1462
Changes from 9 commits
9bb2153
baad51e
ddc10c3
57fc39b
071224f
515b60c
4ea7ce8
d6ea8db
fd22040
a13ce84
a294bb8
44a5616
b59ca00
47d7b3d
6b1772c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,21 @@ import ( | |
"errors" | ||
"fmt" | ||
"reflect" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gruntwork-io/terratest/modules/testing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const skipJsonLogLine = " msg=" | ||
|
||
var ( | ||
ansiLineRegex = regexp.MustCompile(`(?m)^\x1b\[[0-9;]*m.*`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can we add comments on what these regex mean in plain English? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
tgLogLevel = regexp.MustCompile(`.*time=\S+ level=\S+ prefix=\S+ binary=\S+ msg=.*`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use *But only if you use the latest version that supports custom log format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Output still has lines like:
which requires cleaning when parsing output |
||
) | ||
|
||
// Output calls terraform output for the given variable and return its string value representation. | ||
// It only designed to work with primitive terraform types: string, number and bool. | ||
// Please use OutputStruct for anything else. | ||
|
@@ -279,7 +288,11 @@ func OutputJsonE(t testing.TestingT, options *Options, key string) (string, erro | |
args = append(args, key) | ||
} | ||
|
||
return RunTerraformCommandAndGetStdoutE(t, options, args...) | ||
rawJson, err := RunTerraformCommandAndGetStdoutE(t, options, args...) | ||
if err != nil { | ||
return rawJson, err | ||
} | ||
return cleanJson(rawJson) | ||
} | ||
|
||
// OutputStruct calls terraform output for the given variable and stores the | ||
|
@@ -348,3 +361,33 @@ func OutputAll(t testing.TestingT, options *Options) map[string]interface{} { | |
func OutputAllE(t testing.TestingT, options *Options) (map[string]interface{}, error) { | ||
return OutputForKeysE(t, options, nil) | ||
} | ||
|
||
// clean the ANSI characters from the JSON and update formating | ||
func cleanJson(input string) (string, error) { | ||
// Remove ANSI escape codes | ||
cleaned := ansiLineRegex.ReplaceAllString(input, "") | ||
cleaned = tgLogLevel.ReplaceAllString(cleaned, "") | ||
|
||
lines := strings.Split(cleaned, "\n") | ||
var result []string | ||
for _, line := range lines { | ||
trimmed := strings.TrimSpace(line) | ||
if trimmed != "" && !strings.Contains(trimmed, skipJsonLogLine) { | ||
result = append(result, trimmed) | ||
} | ||
} | ||
ansiClean := strings.Join(result, "\n") | ||
|
||
var jsonObj interface{} | ||
if err := json.Unmarshal([]byte(ansiClean), &jsonObj); err != nil { | ||
return "", err | ||
} | ||
|
||
// Format JSON output with indentation | ||
normalized, err := json.MarshalIndent(jsonObj, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(normalized), nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,6 @@ output "not_a_map" { | |
value = "This is not a map." | ||
} | ||
|
||
output "not_a_map_unicode" { | ||
value = "söme chäräcter" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,7 @@ output "number" { | |
output "number1" { | ||
value = 3 | ||
} | ||
|
||
output "unicode_string" { | ||
value = "söme chäräcter" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This env var is deprecated, please use
TERRAGRUNT_LOG_FORMAT=key-value
*But only if you use the latest version that supports log format.