Skip to content
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

Skip log lines on json output commands. #474

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion tfexec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,38 @@ func (tf *Terraform) runTerraformCmdJSON(ctx context.Context, cmd *exec.Cmd, v i
return err
}

dec := json.NewDecoder(&outbuf)
// Skip backend log lines
jsonData := SkipLogLines(outbuf)

dec := json.NewDecoder(&jsonData)
dec.UseNumber()
return dec.Decode(v)
}

func SkipLogLines(buf bytes.Buffer) bytes.Buffer {
// Initialize a scanner to read line by line
scanner := bufio.NewScanner(&buf)
var jsonData bytes.Buffer
inJsonBlock := false

// Scan each line and accumulate the JSON content
for scanner.Scan() {
line := scanner.Text()
trimmed := strings.TrimSpace(line)

// Start accumulating once we encounter the start of a JSON object
if strings.HasPrefix(trimmed, "{") {
inJsonBlock = true
}

// Accumulate lines if inside a JSON block
if inJsonBlock {
jsonData.WriteString(line + "\n")
}
}
return jsonData
}

// mergeUserAgent does some minor deduplication to ensure we aren't
// just using the same append string over and over.
func mergeUserAgent(uas ...string) string {
Expand Down