From cbc548eb36adb0f78803cc4146a421a58d5201be Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 9 Oct 2018 14:46:11 -0700 Subject: [PATCH] command: Do CLI init of backend loaded from plan If we don't do this, we can't produce any output when applying a saved plan file. Here we also introduce a check to the local backend's ReportResult function so that it won't panic if CLI init is skipped, although that will no longer happen in the apply-from-file case due to the change described in the previous paragraph. --- backend/local/backend.go | 10 +++++++++- command/meta_backend.go | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/backend/local/backend.go b/backend/local/backend.go index 4badd8961..b9b028d54 100644 --- a/backend/local/backend.go +++ b/backend/local/backend.go @@ -408,7 +408,15 @@ func (b *Local) ReportResult(op *backend.RunningOperation, diags tfdiags.Diagnos } else { op.Result = backend.OperationSuccess } - b.ShowDiagnostics(diags) + if b.ShowDiagnostics != nil { + b.ShowDiagnostics(diags) + } else { + // Shouldn't generally happen, but if it does then we'll at least + // make some noise in the logs to help us spot it. + if len(diags) != 0 { + log.Printf("[ERROR] Local backend needs to report diagnostics but ShowDiagnostics callback is not set: %s", diags.ErrWithWarnings()) + } + } } // Colorize returns the Colorize structure that can be used for colorizing diff --git a/command/meta_backend.go b/command/meta_backend.go index 0691d30bd..7596b45d5 100644 --- a/command/meta_backend.go +++ b/command/meta_backend.go @@ -159,6 +159,19 @@ func (m *Meta) BackendForPlan(settings plans.Backend) (backend.Enhanced, tfdiags configureDiags := b.Configure(configVal) diags = diags.Append(configureDiags) + // If the backend supports CLI initialization, do it. + if cli, ok := b.(backend.CLI); ok { + cliOpts := m.backendCLIOpts() + if err := cli.CLIInit(cliOpts); err != nil { + diags = diags.Append(fmt.Errorf( + "Error initializing backend %T: %s\n\n"+ + "This is a bug; please report it to the backend developer", + b, err, + )) + return nil, diags + } + } + // If the result of loading the backend is an enhanced backend, // then return that as-is. This works even if b == nil (it will be !ok). if enhanced, ok := b.(backend.Enhanced); ok {