-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
terraform init -json #34886
terraform init -json #34886
Conversation
internal/command/init.go
Outdated
@@ -63,6 +65,8 @@ func (c *InitCommand) Run(args []string) int { | |||
cmdFlags.StringVar(&flagLockfile, "lockfile", "", "Set a dependency lockfile mode") | |||
cmdFlags.BoolVar(&c.Meta.ignoreRemoteVersion, "ignore-remote-version", false, "continue even if remote and local Terraform versions are incompatible") | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean that no json output will get printed in those cases, only the colorized error, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, however I am now revising this to display exclusively json or human format depending on the view type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work so far, diagnostics render as expected ✅ A few thoughts for discussion as I'm smoke testing the changes:
- Specifying the
-json
flag 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_summary
message 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 -json
is attempting to perform a state migration. Similarlyapply -json
will 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 -json
NOTE: 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