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.
This commit is contained in:
Sander van Harmelen 2019-04-25 09:51:52 +02:00
parent cc38debace
commit 7cf744241a
2 changed files with 27 additions and 13 deletions

View File

@ -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:

View File

@ -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
}
}