Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions terracumber/tfvars_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@

def to_hcl(obj, indent_level=0):
"""
Recursively converts a Python object to an HCL string.
Recursively converts a Python object to an HCL string using structural pattern matching.
"""
indent = " " * indent_level
if isinstance(obj, dict):
lines = []
for key, value in obj.items():
# Recursively format the value
formatted_value = to_hcl(value, indent_level + 1)
match obj:

Copilot AI Feb 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of structural pattern matching (match-case) requires Python 3.10 or later. Consider adding a python_requires='>=3.10' constraint in setup.py to prevent installation on incompatible Python versions, as the codebase now depends on this feature.

Copilot uses AI. Check for mistakes.
case dict():
lines = []
for key, value in obj.items():
formatted_value = to_hcl(value, indent_level + 1)

# If the value is a dictionary, we format it as a block: key = { ... }
if isinstance(value, dict):
lines.append(f"{indent}{key} = {{\n{formatted_value}\n{indent}}}")
else:
lines.append(f"{indent}{key} = {formatted_value}")
return "\n".join(lines)
elif isinstance(obj, list):
items = [to_hcl(item, 0) for item in obj]
return f"[{', '.join(items)}]"
elif isinstance(obj, str):
safe_str = obj.replace('"', '\\"')
return f'"{safe_str}"'
elif isinstance(obj, bool):
return str(obj).lower() # True -> true
elif obj is None:
return "null"
else:
return str(obj)
if isinstance(value, dict):
lines.append(f"{indent}{key} = {{\n{formatted_value}\n{indent}}}")
else:
lines.append(f"{indent}{key} = {formatted_value}")
Comment on lines +20 to +23

Copilot AI Feb 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The helpful comment explaining the dict block formatting logic was removed during refactoring. Consider preserving the comment 'If the value is a dictionary, we format it as a block: key = { ... }' as it explains the formatting decision, which may not be immediately obvious to future maintainers.

Copilot uses AI. Check for mistakes.
return "\n".join(lines)
case list():
items = [to_hcl(item, 0) for item in obj]
return f"[{', '.join(items)}]"
case str():
safe_str = obj.replace('"', '\\"')
return f'"{safe_str}"'
case bool():
return str(obj).lower()
case None:
return "null"
case _:
# This is the 'else' or wildcard case
return str(obj)

def get_default_keep_list(env_config, delete_all):
"""
Expand Down
Loading