Skip to content

Commit

Permalink
call Colorize() on CLI output
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenolivia committed Jan 17, 2024
1 parent 8da1c00 commit 6d9b3b2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
13 changes: 7 additions & 6 deletions internal/backend/remote/backend_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,16 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope
case tfe.PolicyHardFailed:
return fmt.Errorf(msgPrefix + " hard failed.")
case tfe.PolicySoftFailed:
runUrl := fmt.Sprintf(runHeader, b.hostname, b.organization, op.Workspace, r.ID)
runURL := fmt.Sprintf(runHeaderErr, b.hostname, b.organization, op.Workspace, r.ID)

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

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

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

Expand Down
9 changes: 7 additions & 2 deletions internal/backend/remote/backend_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,13 @@ Preparing the remote plan...
`

const runHeader = `
[reset][yellow]To view this run in a browser, visit:
https://%s/app/%s/%s/runs/%s[reset]
[reset][yellow]To view this run in a browser, visit:[reset]
[reset][yellow]https://%s/app/%s/%s/runs/%s[reset]
`

const runHeaderErr = `
To view this run in a browser, visit:
https://%s/app/%s/%s/runs/%s
`

// The newline in this error is to make it look good in the CLI!
Expand Down
13 changes: 7 additions & 6 deletions internal/cloud/backend_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,16 +391,16 @@ func (b *Cloud) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Oper
case tfe.PolicyHardFailed:
return fmt.Errorf(msgPrefix + " hard failed.")
case tfe.PolicySoftFailed:
runUrl := fmt.Sprintf(runHeader, b.Hostname, b.Organization, op.Workspace, r.ID)
runURL := fmt.Sprintf(runHeaderErr, b.Hostname, b.Organization, op.Workspace, r.ID)

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

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

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

Expand Down
9 changes: 7 additions & 2 deletions internal/cloud/backend_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,13 @@ Preparing the remote plan...
`

const runHeader = `
[reset][yellow]To view this run in a browser, visit:
https://%s/app/%s/%s/runs/%s[reset]
[reset][yellow]To view this run in a browser, visit:[reset]
[reset][yellow]https://%s/app/%s/%s/runs/%s[reset]
`

const runHeaderErr = `
To view this run in the browser, visit:
https://%s/app/%s/%s/runs/%s
`

// The newline in this error is to make it look good in the CLI!
Expand Down
8 changes: 4 additions & 4 deletions internal/cloud/backend_taskStages.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,22 @@ func (b *Cloud) processStageOverrides(context *IntegrationContext, output Integr
Query: "\nDo you want to override the failed policy check?",
Description: "Only 'override' will be accepted to override.",
}
runUrl := fmt.Sprintf(taskStageHeader, b.Hostname, b.Organization, context.Op.Workspace, context.Run.ID)
runURL := fmt.Sprintf(taskStageHeader, b.Hostname, b.Organization, context.Op.Workspace, context.Run.ID)
err := b.confirm(context.StopContext, context.Op, opts, context.Run, "override")
if err != nil && err != errRunOverridden {
return false, fmt.Errorf(
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runUrl),
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runURL),
)
}

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

0 comments on commit 6d9b3b2

Please sign in to comment.