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.
This commit is contained in:
Martin Atkins 2018-10-09 14:46:11 -07:00
parent 2b80df0163
commit cbc548eb36
2 changed files with 22 additions and 1 deletions

View File

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

View File

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