Skip to content

Commit 072dfbd

Browse files
author
Lauren
authored
Merge pull request #34473 from hashicorp/laurenolivia/fix-cloud-colorize
[TF-11813] do not colorize runHeader when returned as err
2 parents 8da1c00 + 6d9b3b2 commit 072dfbd

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

internal/backend/remote/backend_common.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,16 +419,16 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope
419419
case tfe.PolicyHardFailed:
420420
return fmt.Errorf(msgPrefix + " hard failed.")
421421
case tfe.PolicySoftFailed:
422-
runUrl := fmt.Sprintf(runHeader, b.hostname, b.organization, op.Workspace, r.ID)
422+
runURL := fmt.Sprintf(runHeaderErr, b.hostname, b.organization, op.Workspace, r.ID)
423423

424424
if op.Type == backend.OperationTypePlan || op.UIOut == nil || op.UIIn == nil ||
425425
!pc.Actions.IsOverridable || !pc.Permissions.CanOverride {
426-
return fmt.Errorf(msgPrefix + " soft failed.\n" + runUrl)
426+
return fmt.Errorf(msgPrefix + " soft failed.\n" + runURL)
427427
}
428428

429429
if op.AutoApprove {
430430
if _, err = b.client.PolicyChecks.Override(stopCtx, pc.ID); err != nil {
431-
return generalError(fmt.Sprintf("Failed to override policy check.\n%s", runUrl), err)
431+
return generalError(fmt.Sprintf("Failed to override policy check.\n%s", runURL), err)
432432
}
433433
} else {
434434
opts := &terraform.InputOpts{
@@ -439,16 +439,17 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope
439439
err = b.confirm(stopCtx, op, opts, r, "override")
440440
if err != nil && err != errRunOverridden {
441441
return fmt.Errorf(
442-
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runUrl),
442+
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runURL),
443443
)
444444
}
445445

446446
if err != errRunOverridden {
447447
if _, err = b.client.PolicyChecks.Override(stopCtx, pc.ID); err != nil {
448-
return generalError(fmt.Sprintf("Failed to override policy check.\n%s", runUrl), err)
448+
return generalError(fmt.Sprintf("Failed to override policy check.\n%s", runURL), err)
449449
}
450450
} else {
451-
b.CLI.Output(fmt.Sprintf("The run needs to be manually overridden or discarded.\n%s\n", runUrl))
451+
runURL := fmt.Sprintf(runHeader, b.hostname, b.organization, op.Workspace, r.ID)
452+
b.CLI.Output(fmt.Sprintf("The run needs to be manually overridden or discarded.\n%s\n", runURL))
452453
}
453454
}
454455

internal/backend/remote/backend_plan.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,13 @@ Preparing the remote plan...
443443
`
444444

445445
const runHeader = `
446-
[reset][yellow]To view this run in a browser, visit:
447-
https://%s/app/%s/%s/runs/%s[reset]
446+
[reset][yellow]To view this run in a browser, visit:[reset]
447+
[reset][yellow]https://%s/app/%s/%s/runs/%s[reset]
448+
`
449+
450+
const runHeaderErr = `
451+
To view this run in a browser, visit:
452+
https://%s/app/%s/%s/runs/%s
448453
`
449454

450455
// The newline in this error is to make it look good in the CLI!

internal/cloud/backend_common.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,16 @@ func (b *Cloud) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Oper
391391
case tfe.PolicyHardFailed:
392392
return fmt.Errorf(msgPrefix + " hard failed.")
393393
case tfe.PolicySoftFailed:
394-
runUrl := fmt.Sprintf(runHeader, b.Hostname, b.Organization, op.Workspace, r.ID)
394+
runURL := fmt.Sprintf(runHeaderErr, b.Hostname, b.Organization, op.Workspace, r.ID)
395395

396396
if op.Type == backend.OperationTypePlan || op.UIOut == nil || op.UIIn == nil ||
397397
!pc.Actions.IsOverridable || !pc.Permissions.CanOverride {
398-
return fmt.Errorf(msgPrefix + " soft failed.\n" + runUrl)
398+
return fmt.Errorf(msgPrefix + " soft failed.\n" + runURL)
399399
}
400400

401401
if op.AutoApprove {
402402
if _, err = b.client.PolicyChecks.Override(stopCtx, pc.ID); err != nil {
403-
return generalError(fmt.Sprintf("Failed to override policy check.\n%s", runUrl), err)
403+
return generalError(fmt.Sprintf("Failed to override policy check.\n%s", runURL), err)
404404
}
405405
} else if !b.input {
406406
return errPolicyOverrideNeedsUIConfirmation
@@ -413,16 +413,17 @@ func (b *Cloud) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Oper
413413
err = b.confirm(stopCtx, op, opts, r, "override")
414414
if err != nil && err != errRunOverridden {
415415
return fmt.Errorf(
416-
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runUrl),
416+
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runURL),
417417
)
418418
}
419419

420420
if err != errRunOverridden {
421421
if _, err = b.client.PolicyChecks.Override(stopCtx, pc.ID); err != nil {
422-
return generalError(fmt.Sprintf("Failed to override policy check.\n%s", runUrl), err)
422+
return generalError(fmt.Sprintf("Failed to override policy check.\n%s", runURL), err)
423423
}
424424
} else {
425-
b.CLI.Output(fmt.Sprintf("The run needs to be manually overridden or discarded.\n%s\n", runUrl))
425+
runURL := fmt.Sprintf(runHeader, b.Hostname, b.Organization, op.Workspace, r.ID)
426+
b.CLI.Output(fmt.Sprintf("The run needs to be manually overridden or discarded.\n%s\n", runURL))
426427
}
427428
}
428429

internal/cloud/backend_plan.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,13 @@ Preparing the remote plan...
638638
`
639639

640640
const runHeader = `
641-
[reset][yellow]To view this run in a browser, visit:
642-
https://%s/app/%s/%s/runs/%s[reset]
641+
[reset][yellow]To view this run in a browser, visit:[reset]
642+
[reset][yellow]https://%s/app/%s/%s/runs/%s[reset]
643+
`
644+
645+
const runHeaderErr = `
646+
To view this run in the browser, visit:
647+
https://%s/app/%s/%s/runs/%s
643648
`
644649

645650
// The newline in this error is to make it look good in the CLI!

internal/cloud/backend_taskStages.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,22 @@ func (b *Cloud) processStageOverrides(context *IntegrationContext, output Integr
174174
Query: "\nDo you want to override the failed policy check?",
175175
Description: "Only 'override' will be accepted to override.",
176176
}
177-
runUrl := fmt.Sprintf(taskStageHeader, b.Hostname, b.Organization, context.Op.Workspace, context.Run.ID)
177+
runURL := fmt.Sprintf(taskStageHeader, b.Hostname, b.Organization, context.Op.Workspace, context.Run.ID)
178178
err := b.confirm(context.StopContext, context.Op, opts, context.Run, "override")
179179
if err != nil && err != errRunOverridden {
180180
return false, fmt.Errorf(
181-
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runUrl),
181+
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runURL),
182182
)
183183
}
184184

185185
if err != errRunOverridden {
186186
if _, err = b.client.TaskStages.Override(context.StopContext, taskStageID, tfe.TaskStageOverrideOptions{}); err != nil {
187-
return false, generalError(fmt.Sprintf("Failed to override policy check.\n%s", runUrl), err)
187+
return false, generalError(fmt.Sprintf("Failed to override policy check.\n%s", runURL), err)
188188
} else {
189189
return true, nil
190190
}
191191
} else {
192-
output.Output(fmt.Sprintf("The run needs to be manually overridden or discarded.\n%s\n", runUrl))
192+
output.Output(fmt.Sprintf("The run needs to be manually overridden or discarded.\n%s\n", runURL))
193193
}
194194
return false, nil
195195
}

0 commit comments

Comments
 (0)