From a567b77978b853e629aabde841a0e72f63fdde92 Mon Sep 17 00:00:00 2001 From: James Kwon <96548424+hongil0316@users.noreply.github.com> Date: Wed, 18 Sep 2024 18:48:41 -0400 Subject: [PATCH] fix: cleaning the output received from terragrunt - remove info line --- go.mod | 2 +- go.sum | 4 ++-- modules/ssh/ssh.go | 3 +-- modules/terraform/output.go | 25 +++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 202a8abc4b..59f57faad9 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index feef308f2a..a636e39b22 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index 8776e341d4..1473635500 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -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 } @@ -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) diff --git a/modules/terraform/output.go b/modules/terraform/output.go index 6e294e7a2b..e1588adbf6 100644 --- a/modules/terraform/output.go +++ b/modules/terraform/output.go @@ -6,6 +6,7 @@ import ( "fmt" "reflect" "strconv" + "strings" "github.com/gruntwork-io/terratest/modules/testing" "github.com/stretchr/testify/require" @@ -300,10 +301,33 @@ 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) { @@ -311,6 +335,7 @@ func OutputForKeysE(t testing.TestingT, options *Options, keys []string) (map[st if err != nil { return nil, err } + out = cleanOutput(out) outputMap := map[string]map[string]interface{}{} if err := json.Unmarshal([]byte(out), &outputMap); err != nil {