From e16c53b561d3e524e5e53c6a40ac86f76e8bca8e Mon Sep 17 00:00:00 2001 From: Barrett Clark Date: Thu, 21 Oct 2021 14:29:12 -0500 Subject: [PATCH] Simplify/Consolidate the cloud conditionals --- internal/command/meta_backend.go | 36 +++++++++++++------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/internal/command/meta_backend.go b/internal/command/meta_backend.go index a521289b5..d72a56c1d 100644 --- a/internal/command/meta_backend.go +++ b/internal/command/meta_backend.go @@ -592,31 +592,25 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, tfdiags.Di return m.backend_c_r_S(c, cHash, sMgr, true) - // Configuring Terraform Cloud for the first time. - // NOTE: There may be an implicit local backend with state that is not visible to this block. - case c != nil && c.Type == "cloud" && s.Backend.Empty(): - log.Printf("[TRACE] Meta.Backend: moving from default local state only to Terraform Cloud") - if !opts.Init { - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - "Terraform Cloud has been configured but needs to be initialized.", - strings.TrimSpace(errBackendInitCloud), - )) - return nil, diags - } - - return m.backend_C_r_s(c, cHash, sMgr) - // Configuring a backend for the first time. case c != nil && s.Backend.Empty(): log.Printf("[TRACE] Meta.Backend: moving from default local state only to %q backend", c.Type) if !opts.Init { - initReason := fmt.Sprintf("Initial configuration of the requested backend %q", c.Type) - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - "Backend initialization required, please run \"terraform init\"", - fmt.Sprintf(strings.TrimSpace(errBackendInit), initReason), - )) + if c.Type == "cloud" { + // NOTE: There may be an implicit local backend with state that is not visible to this block. + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Terraform Cloud has been configured but needs to be initialized.", + strings.TrimSpace(errBackendInitCloud), + )) + } else { + initReason := fmt.Sprintf("Initial configuration of the requested backend %q", c.Type) + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Backend initialization required, please run \"terraform init\"", + fmt.Sprintf(strings.TrimSpace(errBackendInit), initReason), + )) + } return nil, diags }