command/fmt: Restore some opinionated behaviors
In Terraform 0.11 and earlier, the "terraform fmt" command was very
opinionated in the interests of consistency. While that remains its goal,
for pragmatic reasons Terraform 0.12 significantly reduced the number
of formatting behaviors in the fmt command. We've held off on introducing
0.12-and-later-flavored cleanups out of concern it would make it harder
to maintain modules that are cross-compatible with both Terraform 0.11
and 0.12, but with this aimed to land in 0.14 -- two major releases
later -- our new goal is to help those who find older Terraform language
examples learn about the more modern idiom.
More rules may follow later, now that the implementation is set up to
allow modifications to tokens as well as modifications to whitespace, but
for this initial pass the command will now apply the following formatting
conventions:
- 0.11-style quoted variable type constraints will be replaced with their
0.12 syntax equivalents. For example, "string" becomes just string.
(This change quiets a deprecation warning.)
- Collection type constraints that don't specify an element type will
be rewritten to specify the "any" element type explicitly, so
list becomes list(any).
- Arguments whose expressions consist of a quoted string template with
only a single interpolation sequence inside will be "unwrapped" to be
the naked expression instead, which is functionally equivalent.
(This change quiets a deprecation warning.)
- Block labels are given in quotes.
Two of the rules above are coming from a secondary motivation of
continuing down the deprecation path for two existing warnings, so authors
can have two active deprecation warnings quieted automatically by
"terraform fmt", without the need to run any third-party tools.
All of these rules match with current documented idiom as shown in the
Terraform documentation, so anyone who follows the documented style should
see no changes as a result of this. Those who have adopted other local
style will see their configuration files rewritten to the standard
Terraform style, but it should not make any changes that affect the
functionality of the configuration.
There are some further similar rewriting rules that could be added in
future, such as removing 0.11-style quotes around various keyword or
static reference arguments, but this initial pass focused only on some
rules that have been proven out in the third-party tool
terraform-clean-syntax, from which much of this commit is a direct port.
For now this doesn't attempt to re-introduce any rules about vertical
whitespace, even though the 0.11 "terraform fmt" would previously apply
such changes. We'll be more cautious about those because the results of
those rules in Terraform 0.11 were often sub-optimal and so we'd prefer
to re-introduce those with some care to the implications for those who
may be using vertical formatting differences for some semantic purpose,
like grouping together related arguments.
2020-09-26 02:16:26 +02:00
|
|
|
# This test case is intended to cover many of the main formatting
|
|
|
|
# rules of "terraform fmt" at once. It's fine to add new stuff in
|
|
|
|
# here, but you can also add other _in.tf/_out.tf pairs in the
|
|
|
|
# same directory if you want to test something complicated that,
|
|
|
|
# for example, requires specific nested context.
|
|
|
|
#
|
|
|
|
# The input file of this test intentionally has strange whitespace
|
|
|
|
# alignment, because the goal is to see the fmt command fix it.
|
|
|
|
# If you're applying batch formatting to all .tf files in the
|
|
|
|
# repository (or similar), be sure to skip this one to avoid
|
|
|
|
# invalidating the test.
|
|
|
|
|
|
|
|
terraform {
|
|
|
|
required_providers {
|
|
|
|
foo = { version = "1.0.0" }
|
|
|
|
barbaz = {
|
|
|
|
version = "2.0.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
variable instance_type {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
resource foo_instance foo {
|
|
|
|
instance_type = "${var.instance_type}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource foo_instance "bar" {
|
|
|
|
instance_type = "${var.instance_type}-2"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "foo_instance" /* ... */ "baz" {
|
|
|
|
instance_type = "${var.instance_type}${var.instance_type}"
|
|
|
|
|
|
|
|
beep boop {}
|
|
|
|
beep blep {
|
|
|
|
thingy = "${var.instance_type}"
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 17:59:10 +01:00
|
|
|
|
|
|
|
provider "" {
|
|
|
|
}
|
2021-03-25 20:40:54 +01:00
|
|
|
|
|
|
|
locals {
|
|
|
|
name = "${contains(["foo"], var.my_var) ? "${var.my_var}-bar" :
|
|
|
|
contains(["baz"], var.my_var) ? "baz-${var.my_var}" :
|
|
|
|
file("ERROR: unsupported type ${var.my_var}")}"
|
|
|
|
wrapped = "${(var.my_var == null ? 1 :
|
|
|
|
var.your_var == null ? 2 :
|
|
|
|
3)}"
|
|
|
|
}
|