From 7cf744241a048c24956bf930ce7e40194cfa5cb1 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Thu, 25 Apr 2019 09:51:52 +0200 Subject: [PATCH] Do not use a scanner to read the logs Using a scanner can cause issues when reading long lines. Also make sure we return the error correctly while planning. --- backend/remote/backend_common.go | 38 ++++++++++++++++++++++---------- backend/remote/backend_plan.go | 2 +- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/backend/remote/backend_common.go b/backend/remote/backend_common.go index d53b0a791..836ce85b3 100644 --- a/backend/remote/backend_common.go +++ b/backend/remote/backend_common.go @@ -228,37 +228,51 @@ func (b *Remote) parseVariableValues(op *backend.Operation) (terraform.InputValu } func (b *Remote) costEstimation(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run) error { + if r.CostEstimation != nil { + return nil + } + if b.CLI != nil { b.CLI.Output("\n------------------------------------------------------------------------\n") } - var ce = r.CostEstimation - logs, err := b.client.CostEstimations.Logs(stopCtx, ce.ID) + logs, err := b.client.CostEstimations.Logs(stopCtx, r.CostEstimation.ID) if err != nil { return generalError("Failed to retrieve cost estimation logs", err) } - scanner := bufio.NewScanner(logs) + reader := bufio.NewReaderSize(logs, 64*1024) // Retrieve the cost estimation to get its current status. - ce, err = b.client.CostEstimations.Read(stopCtx, ce.ID) + ce, err := b.client.CostEstimations.Read(stopCtx, r.CostEstimation.ID) if err != nil { return generalError("Failed to retrieve cost estimation", err) } - var msgPrefix = "Cost estimation" - + msgPrefix := "Cost estimation" if b.CLI != nil { b.CLI.Output(b.Colorize().Color(msgPrefix + ":\n")) } - for scanner.Scan() { - if b.CLI != nil { - b.CLI.Output(b.Colorize().Color(scanner.Text())) + if b.CLI != nil { + for next := true; next; { + var l, line []byte + + for isPrefix := true; isPrefix; { + l, isPrefix, err = reader.ReadLine() + if err != nil { + if err != io.EOF { + return generalError("Failed to read logs", err) + } + next = false + } + line = append(line, l...) + } + + if next || len(line) > 0 { + b.CLI.Output(b.Colorize().Color(string(line))) + } } } - if err := scanner.Err(); err != nil { - return generalError("Failed to read logs", err) - } switch ce.Status { case tfe.CostEstimationFinished: diff --git a/backend/remote/backend_plan.go b/backend/remote/backend_plan.go index 662e28ef3..72508c639 100644 --- a/backend/remote/backend_plan.go +++ b/backend/remote/backend_plan.go @@ -294,7 +294,7 @@ func (b *Remote) plan(stopCtx, cancelCtx context.Context, op *backend.Operation, if r.CostEstimation != nil { err = b.costEstimation(stopCtx, cancelCtx, op, r) if err != nil { - generalError("Cost Estimation error", err) + return r, err } }