def extract_iam_metadata()

in cicd-deployers/iam_metadata_extractor.py [0:0]


def extract_iam_metadata(file_content):
    """Extracts IAM metadata from file content and formats it as JSON.

    Args:
    file_content: The content of the .sqlx file.

    Returns:
    A JSON string containing the extracted IAM metadata, or None if no
    metadata is found.
    """
    # Find the iam_metadata block within the comments using regex
    match = re.search(r"//iam_metadata:\s*{(.*?)}", file_content, re.DOTALL)
    if match:
        metadata_block = match.group(1)
        # Remove comments and extra whitespace
        metadata_block = re.sub(r"//|\s", "", metadata_block)
        # Find the table name
        table_name_match = re.search(r"name:\s*\"([^\"]+)\"", file_content)
        table_name = table_name_match.group(1) if table_name_match else None

        # Construct the JSON output
        try:
            print(str(table_name))
            print(str(metadata_block))
            json_output = {
                "table": table_name,
                "iam_metadata": json.loads(metadata_block)
            }
            return json.dumps(json_output, indent=2)
        except json.JSONDecodeError as e:
            logging.error(f"Error decoding iam metadata JSON from .sqlx file: {e}")
            return None
    else:
        return None