Skip to content

Commit

Permalink
Add functionality for loading both YAML and JSON source files
Browse files Browse the repository at this point in the history
  • Loading branch information
bensonce committed May 29, 2024
1 parent 79d279e commit d6a0653
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions scripts/lib/hcl_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ def generate_locals(rules: List[AwsConfigRule], output_file: Path) -> None:

format_hcl()

def load_source_file(file_name: str) -> dict:
def load_source_file(file_name: Union[Path, str]) -> dict:
"""Return a map of rule definitions."""
# Load using JSON or YAML based on the file extension.
with Path(file_name).open() as f:
data = json.loads(f.read())
if file_name.suffix in ('.yml', '.yaml',):
data = yaml.safe_load(f)
elif file_name.suffix == '.json':
data = json.loads(f.read())
else:
raise ValueError(f"Unsupported file extension: {file_name.suffix}")
return data

def format_hcl() -> None:
Expand Down

0 comments on commit d6a0653

Please sign in to comment.