This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
generated from JupiterOne-Archives/integration-template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(INT-6599): create document permissions script
- Loading branch information
Gaston Yelmini
authored and
Gaston Yelmini
committed
Jan 16, 2023
1 parent
bbd0a2f
commit e9d3858
Showing
37 changed files
with
316 additions
and
121 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { promises as fs } from 'fs'; | ||
import * as path from 'path'; | ||
import { Command } from 'commander'; | ||
import { invocationConfig } from '../src'; | ||
import { GoogleCloudIntegrationStep } from '../src/types'; | ||
|
||
const table = require('markdown-table'); | ||
|
||
const documentPermissionsCommand = new Command(); | ||
|
||
interface DocumentCommandArgs { | ||
outputFile: string; | ||
} | ||
|
||
const J1_PERMISSIONS_DOCUMENTATION_MARKER_START = | ||
'<!-- {J1_PERMISSIONS_DOCUMENTATION_MARKER_START} -->'; | ||
const J1_PERMISSIONS_DOCUMENTATION_MARKER_END = | ||
'<!-- {J1_PERMISSIONS_DOCUMENTATION_MARKER_END} -->'; | ||
|
||
documentPermissionsCommand | ||
.command('documentPermissions') | ||
.description('Generate GCP permissions list') | ||
.option( | ||
'-o, --output-file <path>', | ||
'project relative path to generated Markdown file', | ||
path.join('docs', 'jupiterone.md'), | ||
) | ||
.action(executeDocumentPermissionsAction); | ||
|
||
documentPermissionsCommand.parse(); | ||
|
||
async function executeDocumentPermissionsAction(options: DocumentCommandArgs) { | ||
const { outputFile } = options; | ||
const documentationFilePath = path.join(process.cwd(), outputFile); | ||
const oldDocumentationFile = await getDocumentationFile( | ||
documentationFilePath, | ||
); | ||
|
||
const newGeneratedDocumentationSection = getNewDocumentationVersion(); | ||
|
||
if (!newGeneratedDocumentationSection) return; | ||
|
||
const newDocumentationFile = replaceBetweenDocumentMarkers( | ||
oldDocumentationFile, | ||
newGeneratedDocumentationSection, | ||
); | ||
|
||
await fs.writeFile(documentationFilePath, newDocumentationFile, { | ||
encoding: 'utf-8', | ||
}); | ||
} | ||
|
||
function getDocumentationFile(documentationFilePath: string): Promise<string> { | ||
return fs.readFile(documentationFilePath, { | ||
encoding: 'utf-8', | ||
}); | ||
} | ||
|
||
function getNewDocumentationVersion(): string | undefined { | ||
const { integrationSteps } = invocationConfig; | ||
|
||
const permissionsList = integrationSteps.reduce( | ||
(accumulatedPermissions, step) => { | ||
const googleCloudIntegrationStep = step as GoogleCloudIntegrationStep; | ||
return googleCloudIntegrationStep.permissions | ||
? [...accumulatedPermissions, ...googleCloudIntegrationStep.permissions] | ||
: accumulatedPermissions; | ||
}, | ||
[] as string[], | ||
); | ||
|
||
const tableMarkdown = getTableMarkdown(permissionsList); | ||
|
||
return `${J1_PERMISSIONS_DOCUMENTATION_MARKER_START}\n${tableMarkdown}\n${J1_PERMISSIONS_DOCUMENTATION_MARKER_END}`; | ||
} | ||
|
||
function getTableMarkdown(permissionsList: string[]): string { | ||
return table([ | ||
['Permissions List'], | ||
...permissionsList.map((permission) => [`\`${permission}\``]), | ||
]); | ||
} | ||
|
||
function replaceBetweenDocumentMarkers( | ||
oldDocumentationFile: string, | ||
newGeneratedDocumentationSection: string, | ||
): string { | ||
const startIndex = oldDocumentationFile.indexOf( | ||
J1_PERMISSIONS_DOCUMENTATION_MARKER_START, | ||
); | ||
|
||
if (startIndex === -1) { | ||
return `${oldDocumentationFile}\n\n${newGeneratedDocumentationSection}`; | ||
} | ||
|
||
const endIndex = oldDocumentationFile.indexOf( | ||
J1_PERMISSIONS_DOCUMENTATION_MARKER_END, | ||
); | ||
|
||
return ( | ||
oldDocumentationFile.substring(0, startIndex) + | ||
newGeneratedDocumentationSection + | ||
oldDocumentationFile.substring( | ||
endIndex + J1_PERMISSIONS_DOCUMENTATION_MARKER_END.length, | ||
) | ||
); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.