Skip to content

Instantly share code, notes, and snippets.

@Tanver-Hasan
Created November 2, 2023 17:31
Show Gist options
  • Save Tanver-Hasan/1e9009b293ac7e129fa94e7d7427af39 to your computer and use it in GitHub Desktop.
Save Tanver-Hasan/1e9009b293ac7e129fa94e7d7427af39 to your computer and use it in GitHub Desktop.
Slipt auth0 cli terraform generated file into files by resource catagory
const fs = require('fs');
const path = require('path');
// This function reads the main.tf file and splits it into sections based on resources
function splitTerraformFile(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
// Matches all resource blocks
const resourceRegex = /resource\s+"([^"]+)"\s+"([^"]+)"\s+\{[\s\S]+?\n\}/g;
let match;
const resources = {};
// Find all resources and categorize them
while ((match = resourceRegex.exec(content)) !== null) {
const [fullMatch, type, name] = match;
if (!resources[type]) {
resources[type] = [];
}
resources[type].push(fullMatch);
}
// Write resources of each type to a separate file
for (const [type, blocks] of Object.entries(resources)) {
const typeFileName = `resource_${type}.tf`;
const typeContent = blocks.join('\n\n');
fs.writeFileSync(typeFileName, typeContent, 'utf-8');
console.log(`Wrote ${blocks.length} resources of type ${type} to ${typeFileName}`);
}
}
// Replace 'main.tf' with the path to your main Terraform file
const mainTerraformFilePath = 'auth0_generated.tf';
splitTerraformFile(mainTerraformFilePath);
@Tanver-Hasan
Copy link
Author

RUN node script.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment