hashicorp / vscode-terraform

HashiCorp Terraform VSCode extension
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Mozilla Public License 2.0
926 stars 179 forks source link

Feature: remove quotes where not needed #421

Open hcharley opened 4 years ago

hcharley commented 4 years ago

Right now I am doing a find and replace with this in VSCode's search:

"\$\{(([a-z]|\.|_)+)\}"
$1

To replace strings like this:

  project_id = "${google_project.main_project.project_id}"

With:

  project_id = google_project.main_project.project_id
hcharley commented 4 years ago

This doesn't yet work for:

            name       = "${kubernetes_config_map.caddy.metadata.0.name}"

or:

            name       = "${function(kubernetes_config_map.caddy.metadata.name)}"
hcharley commented 4 years ago

This may also cause problems for people:

    "${local.key_name}" = "${base64decode(module.foobar.key)}"

Gets replaced with:

    local.key_name = "${base64decode(module.foobar.key)}"
hcharley commented 4 years ago

A little better:

=(.*)"\$\{(([a-z0-8]|\(|\)|\.|_)+)\}"
=$1$2
hcharley commented 4 years ago

Still, this won't work:

  on_call_contacts = "${file("${path.module}/CONTACTS.md")}"
hcharley commented 4 years ago

This is also tricky if using the equal sign in the beginning of the pattern:

      "kubernetes.io/ingress.global-static-ip-name" : "${google_compute_global_address.static_ip.name}"
paultyng commented 4 years ago

For this type of rewriting we'd prefer to defer to terraform fmt, I'm inquiring with the core team to see if there are plans (or an issue to track) for Terraform to fix unnecessary interpolation. Optionally we could probably add the ability to plugin external formatters, but I don't think this is a behavior we'd want to implement ourselves.

hcharley commented 4 years ago

Thanks @paultyng. I leave it up to the maintainers to close this if they don't want to implement. Just thought some of my regex might help a future fellow-traveller.