-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9bb2153
Terragrunt log level handling
denis256 baad51e
Added tests for fetching tg output
denis256 ddc10c3
Added test for unicode characters
denis256 57fc39b
Terragrunt output tests
denis256 071224f
Arguments cleanup
denis256 515b60c
Default log level update
denis256 4ea7ce8
Formatting update
denis256 d6ea8db
updated default path
denis256 fd22040
Fixed logs cleaning
denis256 a13ce84
Merge remote-tracking branch 'origin/main' into bug/tg-output-handlin…
denis256 a294bb8
Custom log format
denis256 44a5616
version update
denis256 b59ca00
Merge branch 'main' into bug/tg-output-handling-1453
denis256 47d7b3d
Merge branch 'main' into bug/tg-output-handling-1453
denis256 6b1772c
PR comments
denis256 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,23 @@ import ( | |
"errors" | ||
"fmt" | ||
"reflect" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gruntwork-io/terratest/modules/testing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const skipJsonLogLine = " msg=" | ||
|
||
var ( | ||
// ansiLineRegex matches lines starting with ANSI escape codes for text formatting (e.g., colors, styles). | ||
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 matches log lines containing fields for time, level, prefix, binary, and message, each with non-whitespace values. | ||
tgLogLevel = regexp.MustCompile(`.*time=\S+ level=\S+ prefix=\S+ binary=\S+ msg=.*`) | ||
) | ||
|
||
// 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 +290,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 +363,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you explain what this does? It could be great to add some comments explanining the default behaviour here and below.
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.
Added description