Conversation
internal/command/init.go
Outdated
| cmdFlags.StringVar(&testsDirectory, "test-directory", "tests", "test-directory") | ||
| cmdFlags.BoolVar(&flagJson, "json", false, "json") | ||
|
|
||
| cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } |
There was a problem hiding this comment.
Notice that few instances of c.Ui.Error still exists in peculiar cases, e.g
- where errors are caught before the view struct is even instantiated, (lines#70)
- in cases where an error is appended to diagnostics & also printed with colourize
Any thoughts on this?
There was a problem hiding this comment.
You mean that no json output will get printed in those cases, only the colorized error, right?
There was a problem hiding this comment.
yes, however I am now revising this to display exclusively json or human format depending on the view type
sebasslash
left a comment
There was a problem hiding this comment.
Great work so far, diagnostics render as expected ✅ A few thoughts for discussion as I'm smoke testing the changes:
- Specifying the
-jsonflag still renders human readable output, which seems like a gotcha if some process is expecting to be able to parse output as JSON. Should we consider mappinginit's output to JSON structs underviews/json? This might be a tall ask given just how much machinery operates underterraform init. One example I can think of would be aninit_summarymessage type:
{
"@level":"info",
"@message":"Terraform Cloud has been successfully initialized!",
"@module":"terraform.ui",
"@timestamp":"2024-04-01T10:24:56.597166-04:00",
// maybe include some metadata about the backend
"cloud": {
"organization": "my_org",
"hostname": "app.terraform.io",
"workspace": {
"name": "my_workspace"
},
},
"type":"init_summary"
}- We should error when
init -jsonis attempting to perform a state migration. Similarlyapply -jsonwill prevent an apply from starting unless the user specifies-auto-approve:
{"@level":"error","@message":"Error: Plan file or auto-approve required","@module":"terraform.ui","@timestamp":"2024-04-01T10:30:35.770211-04:00","diagnostic":{"severity":"error","summary":"Plan file or auto-approve required","detail":"Terraform cannot ask for interactive approval when -json is set. You can either apply a saved plan file, or enable the -auto-approve option."},"type":"diagnostic"}
We currently don't support non-interactive state migrations AFAIK.
|
@Uk1288 I don't think you were able to upload the screenshot you intended to. But , anyways, I also had added both cloud and backend config initializers in the same main.tf file, and yet I got a different output than yours 😄 . That's why I was confused. |
alisdair
left a comment
There was a problem hiding this comment.
Thanks for working on this! I think the approach you're taking is solid. I have a few inline comments about improving the JSON output slightly, and some other minor tweaks, but otherwise this looks good to me.
| {"@level":"info","@message":"Terraform 1.9.0-dev","@module":"terraform.ui","terraform":"1.9.0-dev","type":"version","ui":"1.2"} | ||
| {"@level":"info","@message":"Initializing the backend...","@module":"terraform.ui","type":"init_output"} | ||
| {"@level":"info","@message":"Initializing modules...","@module":"terraform.ui","type":"init_output"} | ||
| {"@level":"info","@message":"- foo in foo","@module":"terraform.ui","type":"log"} |
There was a problem hiding this comment.
It would be great to have more useful semantic output for module and provider installations, so that clients could build useful UI on top of it. Not necessary for this PR, but if you can sketch out how that might work in the PR description it'd help make sure we're not painting ourselves into a corner.
alisdair
left a comment
There was a problem hiding this comment.
Looking good to me! 🎉 I'm interested to see if you can easily get more semantic information out of the few remaining log calls from the module/provider installers, but even if not this is a big improvement to machine readability of init output.
…h -migrate-state and -json options
…e for rendering the errors
|
Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch. |
|
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |


This PR makes changes to allow the support of json output for terraform init cmd. The output would be similar to that generated by terraform plan -json.
New command option:
terraform init -jsonNOTE: since terraform cannot ask for interactive approval when -json is set, an error is returned if both -migrate-state and -json options are set
Target Release
1.8.x
Draft CHANGELOG entry
NEW FEATURES
terraform init -json: Displays machine readable format for terraform init command.Human View (
terraform init) - successJSON View (
terraform init -json) - successHuman View (
terraform init) - with errorJSON View (
terraform init -json) - with errorterraform init -help