vscode-terraform icon indicating copy to clipboard operation
vscode-terraform copied to clipboard

Feature: remove quotes where not needed

Open hcharley opened this issue 4 years ago • 7 comments

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 avatar Jun 26 '20 17:06 hcharley

This doesn't yet work for:

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

or:

            name       = "${function(kubernetes_config_map.caddy.metadata.name)}"

hcharley avatar Jun 26 '20 18:06 hcharley

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 avatar Jun 26 '20 18:06 hcharley

A little better:

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

hcharley avatar Jun 26 '20 18:06 hcharley

Still, this won't work:

  on_call_contacts = "${file("${path.module}/CONTACTS.md")}"

hcharley avatar Jun 26 '20 18:06 hcharley

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}"

hcharley avatar Jun 26 '20 18:06 hcharley

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.

paultyng avatar Jun 26 '20 18:06 paultyng

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.

hcharley avatar Jun 26 '20 21:06 hcharley