feat: Add `init_output` log type for use when parsing a subset of logs from the `init` command (#181)
diff --git a/logging_init.go b/logging_init.go
new file mode 100644
index 0000000..402a86f
--- /dev/null
+++ b/logging_init.go
@@ -0,0 +1,18 @@
+// Copyright IBM Corp. 2019, 2026
+// SPDX-License-Identifier: MPL-2.0
+
+package tfjson
+
+const (
+ InitOutput LogMessageType = "init_output"
+)
+
+// InitOutputMessage represents messages of type "init_output"
+// Note that:
+// - This is a subset of the full set of messages emitted by terraform init. Other messages are logged with type "log".
+// - The message_code field is a string that identifies the specific type of "init_output" message it is
+// - In an ideal world the "type" field would be set to this value, as that's how it is intended to be used.
+type InitOutputMessage struct {
+ baseLogMessage
+ MessageCode string `json:"message_code"`
+}
diff --git a/logging_test.go b/logging_test.go
index 4c563c7..7f9f6d5 100644
--- a/logging_test.go
+++ b/logging_test.go
@@ -244,12 +244,13 @@
},
{
`{"@level":"info","@message":"Initializing provider plugins found in the configuration...","@module":"terraform.ui","@timestamp":"2025-11-17T17:18:04.314Z","message_code":"initializing_provider_plugin_from_config_message","type":"init_output"}`,
- UnknownLogMessage{
+ InitOutputMessage{
baseLogMessage: baseLogMessage{
Lvl: Info,
Msg: "Initializing provider plugins found in the configuration...",
Time: time.Date(2025, 11, 17, 17, 18, 04, 314000000, time.UTC),
},
+ MessageCode: "initializing_provider_plugin_from_config_message",
},
},
{
@@ -284,12 +285,13 @@
},
{
`{"@level":"info","@message":"Initializing the backend...","@module":"terraform.ui","@timestamp":"2025-11-17T17:18:52.256Z","message_code":"initializing_backend_message","type":"init_output"}`,
- UnknownLogMessage{
+ InitOutputMessage{
baseLogMessage: baseLogMessage{
Lvl: Info,
Msg: "Initializing the backend...",
Time: time.Date(2025, 11, 17, 17, 18, 52, 256000000, time.UTC),
},
+ MessageCode: "initializing_backend_message",
},
},
// At this point in an init command's output there is a log message that isn't presented in JSON format:
@@ -301,32 +303,35 @@
// See this GitHub issue: https://github.com/hashicorp/terraform/issues/37911
{
`{"@level":"info","@message":"Terraform has created a lock file .terraform.lock.hcl to record the provider\nselections it made above. Include this file in your version control repository\nso that Terraform can guarantee to make the same selections by default when\nyou run \"terraform init\" in the future.","@module":"terraform.ui","@timestamp":"2025-11-17T17:19:06.698Z","message_code":"lock_info","type":"init_output"}`,
- UnknownLogMessage{
+ InitOutputMessage{
baseLogMessage: baseLogMessage{
Lvl: Info,
Msg: "Terraform has created a lock file .terraform.lock.hcl to record the provider\nselections it made above. Include this file in your version control repository\nso that Terraform can guarantee to make the same selections by default when\nyou run \"terraform init\" in the future.",
Time: time.Date(2025, 11, 17, 17, 19, 06, 698000000, time.UTC),
},
+ MessageCode: "lock_info",
},
},
{
`{"@level":"info","@message":"Terraform has been successfully initialized!","@module":"terraform.ui","@timestamp":"2025-11-17T17:19:09.915Z","message_code":"output_init_success_message","type":"init_output"}`,
- UnknownLogMessage{
+ InitOutputMessage{
baseLogMessage: baseLogMessage{
Lvl: Info,
Msg: "Terraform has been successfully initialized!",
Time: time.Date(2025, 11, 17, 17, 19, 9, 915000000, time.UTC),
},
+ MessageCode: "output_init_success_message",
},
},
{
`{"@level":"info","@message":"You may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.","@module":"terraform.ui","@timestamp":"2025-11-17T17:19:10.553Z","message_code":"output_init_success_cli_message","type":"init_output"}`,
- UnknownLogMessage{
+ InitOutputMessage{
baseLogMessage: baseLogMessage{
Lvl: Info,
Msg: "You may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.",
Time: time.Date(2025, 11, 17, 17, 19, 10, 553000000, time.UTC),
},
+ MessageCode: "output_init_success_cli_message",
},
},
}
diff --git a/logging_types.go b/logging_types.go
index f24ff3f..da149ba 100644
--- a/logging_types.go
+++ b/logging_types.go
@@ -23,6 +23,9 @@
DiagnosticLogMessage{},
UnknownLogMessage{},
+ // init
+ InitOutputMessage{},
+
// query
ListStartMessage{},
ListResourceFoundMessage{},
@@ -48,6 +51,11 @@
v := DiagnosticLogMessage{}
return v, d.Decode(&v)
+ // init
+ case InitOutput:
+ v := InitOutputMessage{}
+ return v, json.Unmarshal(b, &v)
+
// query
case MessageListStart:
v := ListStartMessage{}