Skip to content

Commit

Permalink
Pass path as argument when running 'terraform fmt', add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bensonce committed May 29, 2024
1 parent d6a0653 commit 56e6c70
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions scripts/lib/hcl_generator.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import json
import logging

from pathlib import Path
from typing import List
from typing import List, Union

from lib.aws_config_rule import AwsConfigRule

import yaml

from jinja2 import Environment, PackageLoader, select_autoescape
from python_terraform import Terraform

def generate_variables(rules: List[AwsConfigRule], output_file: Path) -> None:
"""Takes an input file with a list of AWS-managed Config Rules and
generates HCL code from the list."""
print("Creating Terraform variables for AWS-managed Config Rules.")
logging.info("Creating Terraform variables for AWS-managed Config Rules.")
env = Environment(
loader=PackageLoader("index"),
autoescape=select_autoescape())
Expand All @@ -28,7 +31,7 @@ def generate_variables(rules: List[AwsConfigRule], output_file: Path) -> None:

def generate_locals(rules: List[AwsConfigRule], output_file: Path) -> None:
"""Create the managed_rules_locals `locals` block and write it to disk."""
print("Creating Terraform locals block for AWS-managed Config Rules.")
logging.info("Creating Terraform locals block for AWS-managed Config Rules.")
env = Environment(
loader=PackageLoader("index"),
autoescape=select_autoescape())
Expand All @@ -53,7 +56,7 @@ def load_source_file(file_name: Union[Path, str]) -> dict:
raise ValueError(f"Unsupported file extension: {file_name.suffix}")
return data

def format_hcl() -> None:
def format_hcl(path: Union[Path, str]='.') -> None:
"""Format Terraform files with `terraform fmt`."""
t = Terraform()
return_code, stdout, stderr = t.cmd('fmt', '../../.')
return_code, stdout, stderr = t.cmd('fmt', f"{Path(path)}")

0 comments on commit 56e6c70

Please sign in to comment.