chore: initialize new view in meta backend

Signed-off-by: Bruno Schaatsbergen <git@bschaatsbergen.com>
pull/35929/head
Bruno Schaatsbergen 1 year ago
parent 185867f1ea
commit f9797595e3
No known key found for this signature in database

@ -8,6 +8,7 @@ import (
"github.com/mitchellh/colorstring"
"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/command/views"
"github.com/hashicorp/terraform/internal/terminal"
"github.com/hashicorp/terraform/internal/terraform"
)
@ -60,6 +61,9 @@ type CLIOpts struct {
// for tailoring the output to fit the attached terminal, for example.
Streams *terminal.Streams
//FIXME: something
View *views.View
// StatePath is the local path where state is read from.
//
// StateOutPath is the local path where the state will be written.

@ -65,9 +65,8 @@ type Cloud struct {
// client is the HCP Terraform or Terraform Enterprise API client.
client *tfe.Client
// viewHooks implements functions integrating the tfe.Client with the CLI
// output.
viewHooks views.CloudHooks
//FIXME: something
View *views.View
// Hostname of HCP Terraform or Terraform Enterprise
Hostname string
@ -607,7 +606,8 @@ func cliConfigToken(hostname svchost.Hostname, services *disco.Disco) (string, e
// backend to log any connection issues to prevent data loss.
func (b *Cloud) retryLogHook(attemptNum int, resp *http.Response) {
if b.CLI != nil {
if output := b.viewHooks.RetryLogHook(attemptNum, resp, true); len(output) > 0 {
retryLogHook := views.NewRetryLoghook(b.View)
if output := retryLogHook.RetryLogHook(attemptNum, resp, true); len(output) > 0 {
b.CLI.Output(b.Colorize().Color(output))
}
}

@ -25,6 +25,8 @@ func (b *Cloud) CLIInit(opts *backendrun.CLIOpts) error {
Streams: opts.Streams,
Colorize: opts.CLIColor,
}
//FIXME: something
b.View = opts.View
return nil
}

@ -386,9 +386,11 @@ func (m *Meta) backendCLIOpts() (*backendrun.CLIOpts, error) {
return nil, err
}
return &backendrun.CLIOpts{
CLI: m.Ui,
CLIColor: m.Colorize(),
Streams: m.Streams,
CLI: m.Ui,
CLIColor: m.Colorize(),
Streams: m.Streams,
//FIXME: something
View: views.NewView(m.Streams),
StatePath: m.statePath,
StateOutPath: m.stateOutPath,
StateBackupPath: m.backupPath,

@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package views
import (
@ -30,7 +33,7 @@ func NewRetryLoghook(view *View) *RetryLogHook {
//
// If colorize is true, then the value returned by this function should be
// processed by a colorizer.
func (hooks *RetryLogHook) RetryLogHook(attemptNum int, resp *http.Response, colorize bool) string {
func (hook *RetryLogHook) RetryLogHook(attemptNum int, resp *http.Response, colorize bool) string {
// Ignore the first retry to make sure any delayed output will
// be written to the console before we start logging retries.
//
@ -38,7 +41,7 @@ func (hooks *RetryLogHook) RetryLogHook(attemptNum int, resp *http.Response, col
// requests and server errors, but in the cloud backend we only
// care about server errors so we ignore rate limit (429) errors.
if attemptNum == 0 || (resp != nil && resp.StatusCode == 429) {
hooks.lastRetry = time.Now()
hook.lastRetry = time.Now()
return ""
}
@ -46,13 +49,13 @@ func (hooks *RetryLogHook) RetryLogHook(attemptNum int, resp *http.Response, col
if attemptNum == 1 {
msg = initialRetryError
} else {
msg = fmt.Sprintf(repeatedRetryError, time.Since(hooks.lastRetry).Round(time.Second))
msg = fmt.Sprintf(repeatedRetryError, time.Since(hook.lastRetry).Round(time.Second))
}
if colorize {
return strings.TrimSpace(fmt.Sprintf("[reset][yellow]%s[reset]", msg))
}
return hooks.view.colorize.Color(strings.TrimSpace(msg))
return hook.view.colorize.Color(strings.TrimSpace(msg))
}
// The newline in this error is to make it look good in the CLI!

@ -101,7 +101,7 @@ func NewTest(vt arguments.ViewType, view *View) Test {
}
type TestHuman struct {
CloudHooks
RetryLogHook
view *View
}
@ -371,11 +371,11 @@ func (t *TestHuman) TFCStatusUpdate(status tfe.TestRunStatus, elapsed time.Durat
}
func (t *TestHuman) TFCRetryHook(attemptNum int, resp *http.Response) {
t.view.streams.Println(t.view.colorize.Color(t.RetryLogHook(attemptNum, resp, true)))
t.view.streams.Println(t.view.colorize.Color(t.RetryLogHook.RetryLogHook(attemptNum, resp, true)))
}
type TestJSON struct {
CloudHooks
RetryLogHook
view *JSONView
}
@ -730,7 +730,7 @@ func (t *TestJSON) TFCStatusUpdate(status tfe.TestRunStatus, elapsed time.Durati
func (t *TestJSON) TFCRetryHook(attemptNum int, resp *http.Response) {
t.view.log.Error(
t.RetryLogHook(attemptNum, resp, false),
t.RetryLogHook.RetryLogHook(attemptNum, resp, false),
"type", json.MessageTestRetry,
)
}

Loading…
Cancel
Save