Skip to content

Commit

Permalink
Merge pull request #1445 from gruntwork-io/fix/json-output-error
Browse files Browse the repository at this point in the history
fix: cleaning the output received from terragrunt - remove info line
  • Loading branch information
james03160927 authored Sep 24, 2024
2 parents fc19428 + bb1a781 commit cf38360
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions modules/terraform/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -300,17 +301,35 @@ func OutputStructE(t testing.TestingT, options *Options, key string, v interface
if err != nil {
return err
}
out = cleanOutput(out)

return json.Unmarshal([]byte(out), &v)
}

// cleanOutput removes lines containing "INFO" and non-printable ASCII characters from the output.
func cleanOutput(out string) string {
var result []rune
for _, line := range strings.Split(out, "\n") {
if strings.Contains(line, "INFO") {
continue
}
for _, r := range line {
if r >= 32 && r < 127 { // Keep printable ASCII characters only
result = append(result, r)
}
}
}
return string(result)
}

// OutputForKeysE calls terraform output for the given key list and returns values as a map.
// The returned values are of type interface{} and need to be type casted as necessary. Refer to output_test.go
func OutputForKeysE(t testing.TestingT, options *Options, keys []string) (map[string]interface{}, error) {
out, err := OutputJsonE(t, options, "")
if err != nil {
return nil, err
}
out = cleanOutput(out)

outputMap := map[string]map[string]interface{}{}
if err := json.Unmarshal([]byte(out), &outputMap); err != nil {
Expand Down

0 comments on commit cf38360

Please sign in to comment.