From 56e6c700a0e655eb1d9919fda77730e3460f4bf7 Mon Sep 17 00:00:00 2001 From: Carl Benson Date: Wed, 29 May 2024 19:26:14 -0400 Subject: [PATCH] Pass path as argument when running 'terraform fmt', add logging --- scripts/lib/hcl_generator.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/lib/hcl_generator.py b/scripts/lib/hcl_generator.py index 6f94257..fa218c5 100644 --- a/scripts/lib/hcl_generator.py +++ b/scripts/lib/hcl_generator.py @@ -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()) @@ -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()) @@ -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)}")