Created
November 2, 2023 17:31
-
-
Save Tanver-Hasan/1e9009b293ac7e129fa94e7d7427af39 to your computer and use it in GitHub Desktop.
Slipt auth0 cli terraform generated file into files by resource catagory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RUN
node script.js