Last active
October 25, 2023 00:23
-
-
Save jim80net/f4aa59ffa3e146a954ee8376f392a0e1 to your computer and use it in GitHub Desktop.
Terrible No Good OneLiner to Add / Remove Quotation marks from terraform files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # It should go without saying, but make sure your terraform file is checked into version control before running this. | |
| # If you find a bug, please let me know =) | |
| # Add quotation marks | |
| elixir -e 'Enum.each(Path.wildcard("**/*.tf"), fn filename -> File.read!(filename) |> String.split("\n") |> Enum.map(fn line -> arraylist = line |> String.trim() |> String.split(" ") |> Enum.map(fn s -> to_charlist(s) end); case arraylist do; [a, b, c, [123]] when a in [[114,101,115,111,117,114,99,101], [100,97,116,97], [112,114,111,118,105,100,101,114], [98,97,99,107,101,110,100]] -> [a,32,34,b,34,32,34,c,34,32,123,10]; _ -> line <> "\n"; end; end) |> to_string() |> String.trim() |> (&(File.write!(filename, &1))).() end)' | |
| # Remove quotation marks from "resource", "data", "provider", and "backend" lines | |
| elixir -e 'Enum.each(Path.wildcard("**/*.tf"), fn filename -> File.read!(filename) |> String.split("\n") |> Enum.map(fn line -> arraylist = line |> String.trim() |> String.split(" ") |> Enum.map(fn s -> to_charlist(s) end); case arraylist do; [a, b, c, [123]] when a in [[114,101,115,111,117,114,99,101], [100,97,116,97], [112,114,111,118,105,100,101,114], [98,97,99,107,101,110,100]] -> [a,32,b,32,c,32,123,10] |> List.flatten() |> Enum.reject(fn n -> n == 34 end); _ -> line <> "\n"; end; end) |> to_string() |> String.trim() |> (&(File.write!(filename, &1))).() end)' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env elixir | |
| # terraform_snake_case.exs | |
| # Usage: run in terraform workspace. | |
| # Make sure to perform a git commit before and after running. | |
| # Function to convert kebab-case to snake_case | |
| conv = fn s -> | |
| Regex.replace(~r/-(\w)/, s, fn _, c -> "_" <> c end) | |
| end | |
| # First pass: Build the name map | |
| name_map = Enum.reduce(Path.wildcard("**/*.tf"), %{}, fn filename, acc -> | |
| lines = File.read!(filename) |> String.split("\n") | |
| Enum.reduce(lines, acc, fn line, map -> | |
| arraylist = line |> String.trim() |> String.split(" ") | |
| case arraylist do | |
| [a, resource_type, name | _] when a in ["resource", "data"] -> | |
| stripped_resource_type = String.trim(resource_type, "\"") | |
| stripped_name = String.trim(name, "\"") | |
| Map.put(map, {stripped_resource_type, stripped_name}, conv.(stripped_name)) | |
| ["output", name | _] -> | |
| stripped_name = String.trim(name, "\"") | |
| Map.put(map, {"output", stripped_name}, conv.(stripped_name)) | |
| _ -> map | |
| end | |
| end) | |
| end) | |
| # After constructing name_map | |
| # IO.inspect(name_map, label: "Debug: Contents of name_map") | |
| # Function to replace Terraform-specific references | |
| replace_tf_refs = fn line, map -> | |
| matches = Regex.scan(~r/([\w-]+)\.([\w-]+)\.[\w-]+\b/, line) | |
| Enum.reduce(matches, line, fn match, acc -> | |
| [_, r, n] = match | |
| new_n = Map.get(map, {r, n}, n) # Default to original name if not found in map | |
| # Replace all occurrences of this specific match in the accumulator | |
| String.replace(acc, "#{r}.#{n}.", "#{r}.#{new_n}.") | |
| end) | |
| end | |
| # Second pass: Update the files | |
| Enum.each(Path.wildcard("**/*.tf"), fn filename -> | |
| lines = File.read!(filename) |> String.split("\n") | |
| new_lines = Enum.map(lines, fn line -> | |
| arraylist = line |> String.trim() |> String.split(" ") | |
| case arraylist do | |
| [a, resource_type, name | _] when a in ["resource", "data"] -> | |
| stripped_resource_type = String.trim(resource_type, "\"") | |
| stripped_name = String.trim(name, "\"") | |
| new_name = Map.get(name_map, {stripped_resource_type, stripped_name}, stripped_name) | |
| "#{a} #{resource_type} \"#{new_name}\" {" | |
| ["output", name | _] -> | |
| stripped_name = String.trim(name, "\"") | |
| new_name = Map.get(name_map, {"output", stripped_name}, stripped_name) | |
| "output \"#{new_name}\" {" | |
| _ -> | |
| # Replace other references using the name_map | |
| replace_tf_refs.(line, name_map) | |
| end | |
| end) | |
| # Write the new lines back to the file | |
| File.write!(filename, Enum.join(new_lines, "\n")) | |
| end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment