Skip to content

Commit 7cd3a7d

Browse files
committed
fix: Make all init backend messages JSON-compatible
Fixes #37911 by converting all backend configuration messages to use the View abstraction, which properly formats output as JSON when the -json flag is used. Previously, certain messages were output directly using the legacy Ui abstraction, bypassing JSON formatting. Changes: - Added 11 new message codes to init.go MessageRegistry with both human and JSON formatted values - Replaced 11 direct m.Ui.Output() calls in meta_backend.go with View abstraction calls - Fixed output in backend_C_r_s(), backend_C_r_S_changed(), backend_c_r_S(), and stateStore_c_S() functions All init tests pass successfully.
1 parent d884031 commit 7cd3a7d

File tree

2 files changed

+113
-20
lines changed

2 files changed

+113
-20
lines changed

internal/command/meta_backend.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,10 +1132,11 @@ func (m *Meta) backend_c_r_S(
11321132
// Get the backend type for output
11331133
backendType := s.Backend.Type
11341134

1135+
view := views.NewInit(vt, m.View)
11351136
if cloudMode == cloud.ConfigMigrationOut {
1136-
m.Ui.Output("Migrating from HCP Terraform or Terraform Enterprise to local state.")
1137+
view.Output(views.BackendCloudMigrateLocalMessage)
11371138
} else {
1138-
m.Ui.Output(fmt.Sprintf(strings.TrimSpace(outputBackendMigrateLocal), s.Backend.Type))
1139+
view.Output(views.BackendMigrateLocalMessage, s.Backend.Type)
11391140
}
11401141

11411142
// Grab a purely local backend to get the local state if it exists
@@ -1177,9 +1178,7 @@ func (m *Meta) backend_c_r_S(
11771178
}
11781179

11791180
if output {
1180-
m.Ui.Output(m.Colorize().Color(fmt.Sprintf(
1181-
"[reset][green]\n\n"+
1182-
strings.TrimSpace(successBackendUnset), backendType)))
1181+
view.Output(views.BackendConfiguredUnsetMessage, backendType)
11831182
}
11841183

11851184
// Return no backend
@@ -1348,8 +1347,8 @@ func (m *Meta) backend_C_r_s(c *configs.Backend, cHash int, sMgr *clistate.Local
13481347
// By now the backend is successfully configured. If using HCP Terraform, the success
13491348
// message is handled as part of the final init message
13501349
if _, ok := b.(*cloud.Cloud); !ok {
1351-
m.Ui.Output(m.Colorize().Color(fmt.Sprintf(
1352-
"[reset][green]\n"+strings.TrimSpace(successBackendSet), s.Backend.Type)))
1350+
view := views.NewInit(vt, m.View)
1351+
view.Output(views.BackendConfiguredSuccessMessage, s.Backend.Type)
13531352
}
13541353

13551354
return b, diags
@@ -1377,23 +1376,19 @@ func (m *Meta) backend_C_r_S_changed(c *configs.Backend, cHash int, sMgr *clista
13771376

13781377
if output {
13791378
// Notify the user
1379+
view := views.NewInit(vt, m.View)
13801380
switch cloudMode {
13811381
case cloud.ConfigChangeInPlace:
1382-
m.Ui.Output("HCP Terraform configuration has changed.")
1382+
view.Output(views.BackendCloudChangeInPlaceMessage)
13831383
case cloud.ConfigMigrationIn:
1384-
m.Ui.Output(fmt.Sprintf("Migrating from backend %q to HCP Terraform.", s.Backend.Type))
1384+
view.Output(views.BackendMigrateToCloudMessage, s.Backend.Type)
13851385
case cloud.ConfigMigrationOut:
1386-
m.Ui.Output(fmt.Sprintf("Migrating from HCP Terraform to backend %q.", c.Type))
1386+
view.Output(views.BackendMigrateFromCloudMessage, c.Type)
13871387
default:
13881388
if s.Backend.Type != c.Type {
1389-
output := fmt.Sprintf(outputBackendMigrateChange, s.Backend.Type, c.Type)
1390-
m.Ui.Output(m.Colorize().Color(fmt.Sprintf(
1391-
"[reset]%s\n",
1392-
strings.TrimSpace(output))))
1389+
view.Output(views.BackendMigrateTypeChangeMessage, s.Backend.Type, c.Type)
13931390
} else {
1394-
m.Ui.Output(m.Colorize().Color(fmt.Sprintf(
1395-
"[reset]%s\n",
1396-
strings.TrimSpace(outputBackendReconfigure))))
1391+
view.Output(views.BackendReconfigureMessage)
13971392
}
13981393
}
13991394
}
@@ -1479,8 +1474,8 @@ func (m *Meta) backend_C_r_S_changed(c *configs.Backend, cHash int, sMgr *clista
14791474
// By now the backend is successfully configured. If using HCP Terraform, the success
14801475
// message is handled as part of the final init message
14811476
if _, ok := b.(*cloud.Cloud); !ok {
1482-
m.Ui.Output(m.Colorize().Color(fmt.Sprintf(
1483-
"[reset][green]\n"+strings.TrimSpace(successBackendSet), s.Backend.Type)))
1477+
view := views.NewInit(vt, m.View)
1478+
view.Output(views.BackendConfiguredSuccessMessage, s.Backend.Type)
14841479
}
14851480
}
14861481

@@ -1866,7 +1861,8 @@ func (m *Meta) stateStore_c_S(ssSMgr *clistate.LocalState, viewType arguments.Vi
18661861
s := ssSMgr.State()
18671862
stateStoreType := s.StateStore.Type
18681863

1869-
m.Ui.Output(fmt.Sprintf(strings.TrimSpace(outputStateStoreMigrateLocal), stateStoreType))
1864+
view := views.NewInit(viewType, m.View)
1865+
view.Output(views.StateMigrateLocalMessage, stateStoreType)
18701866

18711867
// Grab a purely local backend to get the local state if it exists
18721868
localB, moreDiags := m.Backend(&BackendOpts{ForceLocal: true, Init: true})

internal/command/views/init.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,46 @@ var MessageRegistry map[InitMessageCode]InitMessage = map[InitMessageCode]InitMe
262262
HumanValue: "[reset][green]\n\nSuccessfully unset the state store %q. Terraform will now operate locally.",
263263
JSONValue: "Successfully unset the state store %q. Terraform will now operate locally.",
264264
},
265+
"backend_configured_success": {
266+
HumanValue: backendConfiguredSuccessHuman,
267+
JSONValue: backendConfiguredSuccessJSON,
268+
},
269+
"backend_configured_unset": {
270+
HumanValue: backendConfiguredUnsetHuman,
271+
JSONValue: backendConfiguredUnsetJSON,
272+
},
273+
"backend_migrate_to_cloud": {
274+
HumanValue: "Migrating from backend %q to HCP Terraform.",
275+
JSONValue: "Migrating from backend %q to HCP Terraform.",
276+
},
277+
"backend_migrate_from_cloud": {
278+
HumanValue: "Migrating from HCP Terraform to backend %q.",
279+
JSONValue: "Migrating from HCP Terraform to backend %q.",
280+
},
281+
"backend_cloud_change_in_place": {
282+
HumanValue: "HCP Terraform configuration has changed.",
283+
JSONValue: "HCP Terraform configuration has changed.",
284+
},
285+
"backend_migrate_type_change": {
286+
HumanValue: backendMigrateTypeChangeHuman,
287+
JSONValue: backendMigrateTypeChangeJSON,
288+
},
289+
"backend_reconfigure": {
290+
HumanValue: backendReconfigureHuman,
291+
JSONValue: backendReconfigureJSON,
292+
},
293+
"backend_migrate_local": {
294+
HumanValue: backendMigrateLocalHuman,
295+
JSONValue: backendMigrateLocalJSON,
296+
},
297+
"backend_cloud_migrate_local": {
298+
HumanValue: "Migrating from HCP Terraform or Terraform Enterprise to local state.",
299+
JSONValue: "Migrating from HCP Terraform or Terraform Enterprise to local state.",
300+
},
301+
"state_store_migrate_local": {
302+
HumanValue: stateMigrateLocalHuman,
303+
JSONValue: stateMigrateLocalJSON,
304+
},
265305
"empty_message": {
266306
HumanValue: "",
267307
JSONValue: "",
@@ -302,6 +342,26 @@ const (
302342

303343
// InitConfigError indicates problems encountered during initialisation
304344
InitConfigError InitMessageCode = "init_config_error"
345+
// BackendConfiguredSuccessMessage indicates successful backend configuration
346+
BackendConfiguredSuccessMessage InitMessageCode = "backend_configured_success"
347+
// BackendConfiguredUnsetMessage indicates successful backend unsetting
348+
BackendConfiguredUnsetMessage InitMessageCode = "backend_configured_unset"
349+
// BackendMigrateToCloudMessage indicates migration to HCP Terraform
350+
BackendMigrateToCloudMessage InitMessageCode = "backend_migrate_to_cloud"
351+
// BackendMigrateFromCloudMessage indicates migration from HCP Terraform
352+
BackendMigrateFromCloudMessage InitMessageCode = "backend_migrate_from_cloud"
353+
// BackendCloudChangeInPlaceMessage indicates HCP Terraform configuration change
354+
BackendCloudChangeInPlaceMessage InitMessageCode = "backend_cloud_change_in_place"
355+
// BackendMigrateTypeChangeMessage indicates backend type change
356+
BackendMigrateTypeChangeMessage InitMessageCode = "backend_migrate_type_change"
357+
// BackendReconfigureMessage indicates backend reconfiguration
358+
BackendReconfigureMessage InitMessageCode = "backend_reconfigure"
359+
// BackendMigrateLocalMessage indicates migration to local backend
360+
BackendMigrateLocalMessage InitMessageCode = "backend_migrate_local"
361+
// BackendCloudMigrateLocalMessage indicates migration from cloud to local
362+
BackendCloudMigrateLocalMessage InitMessageCode = "backend_cloud_migrate_local"
363+
// StateMigrateLocalMessage indicates migration from state store to local
364+
StateMigrateLocalMessage InitMessageCode = "state_store_migrate_local"
305365
// FindingMatchingVersionMessage indicates that Terraform is looking for a provider version that matches the constraint during installation
306366
FindingMatchingVersionMessage InitMessageCode = "finding_matching_version_message"
307367
// InstalledProviderVersionInfo describes a successfully installed provider along with its version
@@ -433,3 +493,40 @@ with the configuration, described below.
433493
The Terraform configuration must be valid before initialization so that
434494
Terraform can determine which modules and providers need to be installed.
435495
`
496+
497+
const backendConfiguredSuccessHuman = `[reset][green]
498+
Successfully configured the backend %q! Terraform will automatically
499+
use this backend unless the backend configuration changes.`
500+
501+
const backendConfiguredSuccessJSON = `Successfully configured the backend %q! Terraform will automatically
502+
use this backend unless the backend configuration changes.`
503+
504+
const backendConfiguredUnsetHuman = `[reset][green]
505+
506+
Successfully unset the backend %q. Terraform will now operate locally.`
507+
508+
const backendConfiguredUnsetJSON = `Successfully unset the backend %q. Terraform will now operate locally.`
509+
510+
const backendMigrateTypeChangeHuman = `[reset]Terraform detected that the backend type changed from %q to %q.
511+
`
512+
513+
const backendMigrateTypeChangeJSON = `Terraform detected that the backend type changed from %q to %q.`
514+
515+
const backendReconfigureHuman = `[reset][bold]Backend configuration changed![reset]
516+
517+
Terraform has detected that the configuration specified for the backend
518+
has changed. Terraform will now check for existing state in the backends.
519+
`
520+
521+
const backendReconfigureJSON = `Backend configuration changed!
522+
523+
Terraform has detected that the configuration specified for the backend
524+
has changed. Terraform will now check for existing state in the backends.`
525+
526+
const backendMigrateLocalHuman = `Terraform has detected you're unconfiguring your previously set %q backend.`
527+
528+
const backendMigrateLocalJSON = `Terraform has detected you're unconfiguring your previously set %q backend.`
529+
530+
const stateMigrateLocalHuman = `Terraform has detected you're unconfiguring your previously set %q state store.`
531+
532+
const stateMigrateLocalJSON = `Terraform has detected you're unconfiguring your previously set %q state store.`

0 commit comments

Comments
 (0)