Skip to content

Commit

Permalink
fix: cleaning the output received from terragrunt - remove info line
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 committed Sep 18, 2024
1 parent fc19428 commit a567b77
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/google/go-containerregistry v0.6.0
github.com/google/uuid v1.3.0
github.com/gruntwork-io/go-commons v0.8.0
github.com/hashicorp/go-getter v1.7.6
github.com/hashicorp/go-getter v1.7.5
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl/v2 v2.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,8 @@ github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-getter v1.7.6 h1:5jHuM+aH373XNtXl9TNTUH5Qd69Trve11tHIrB+6yj4=
github.com/hashicorp/go-getter v1.7.6/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-getter v1.7.5 h1:dT58k9hQ/vbxNMwoI5+xFYAJuv6152UNvdHokfI5wE4=
github.com/hashicorp/go-getter v1.7.5/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v0.0.0-20161216184304-ed905158d874/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I=
Expand Down
3 changes: 1 addition & 2 deletions modules/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ func listFileInRemoteDir(t testing.TestingT, sshSession *SshSession, options Scp

// Added based on code: https://github.com/bramvdbogaerde/go-scp/pull/6/files
func copyFileFromRemote(t testing.TestingT, sshSession *SshSession, file *os.File, remotePath string, useSudo bool) error {
logger.Logf(t, "Running command %s on %s@%s", sshSession.Options.Command, sshSession.Options.Username, sshSession.Options.Address)
if err := setUpSSHClient(sshSession); err != nil {
return err
}
Expand All @@ -454,8 +455,6 @@ func copyFileFromRemote(t testing.TestingT, sshSession *SshSession, file *os.Fil
command = fmt.Sprintf("sudo %s", command)
}

logger.Logf(t, "Running command %s on %s@%s", command, sshSession.Options.Username, sshSession.Options.Address)

r, err := sshSession.Session.Output(command)
if err != nil {
fmt.Printf("error reading from remote stdout: %s", err)
Expand Down
25 changes: 25 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,41 @@ 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 processes a string output by removing lines containing "INFO" and filtering out non-printable ASCII characters.
//
// This function is useful for sanitizing command execution results, removing non-printable characters.
//
// IMPORTANT: Currently, the code only removes the INFO message line from the output. If there are future changes, this part of the code may need to be updated.
func cleanOutput(out string) string {
var result []rune
for _, line := range strings.Split(string(out), "\n") {
if !strings.Contains(line, "INFO") {
for _, b := range line {
// Keep printable ASCII characters only
if b >= 32 && b < 127 {
result = append(result, rune(b))
}
}
}
}

out = string(result)
return out
}

// 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 a567b77

Please sign in to comment.