-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: iac experimental directory support
- Loading branch information
Showing
9 changed files
with
277 additions
and
147 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,54 @@ | ||
import { makeDirectoryIterator } from '../../../../lib/iac/makeDirectoryIterator'; | ||
import * as fs from 'fs'; | ||
import * as util from 'util'; | ||
import { IacFileData, VALID_FILE_TYPES } from './types'; | ||
import { getFileType } from '../../../../lib/iac/iac-parser'; | ||
import { IacFileTypes } from '../../../../lib/iac/constants'; | ||
import { isLocalFolder } from '../../../../lib/detect'; | ||
|
||
const loadFileContents = util.promisify(fs.readFile); | ||
const DEFAULT_ENCODING = 'utf-8'; | ||
|
||
export async function loadFiles(pathToScan): Promise<IacFileData[]> { | ||
let filePaths = [pathToScan]; | ||
if (isLocalFolder(pathToScan)) { | ||
filePaths = await getFilePathsFromDirectory(pathToScan); | ||
} | ||
|
||
const filesToScan: IacFileData[] = []; | ||
for (const filePath of filePaths) { | ||
const fileData = await tryLoadFileData(filePath); | ||
if (fileData) filesToScan.push(fileData!); | ||
} | ||
|
||
if (filesToScan.length === 0) { | ||
throw Error("Couldn't find valid IaC files"); | ||
} | ||
|
||
return filesToScan; | ||
} | ||
|
||
function getFilePathsFromDirectory(pathToScan: string): Array<string> { | ||
const directoryPaths = makeDirectoryIterator(pathToScan); | ||
|
||
const directoryFilePaths: string[] = []; | ||
for (const filePath of directoryPaths) { | ||
directoryFilePaths.push(filePath); | ||
} | ||
return directoryFilePaths; | ||
} | ||
|
||
async function tryLoadFileData( | ||
pathToScan: string, | ||
): Promise<IacFileData | null> { | ||
const fileType = getFileType(pathToScan); | ||
if (!VALID_FILE_TYPES.includes(fileType)) { | ||
return null; | ||
} | ||
|
||
return { | ||
filePath: pathToScan, | ||
fileType: fileType as IacFileTypes, | ||
fileContent: await loadFileContents(pathToScan, DEFAULT_ENCODING), | ||
}; | ||
} |
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,97 @@ | ||
import * as hclToJson from 'hcl-to-json'; | ||
import * as YAML from 'js-yaml'; | ||
import { | ||
EngineType, | ||
IacFileParsed, | ||
IacFileData, | ||
ParsingResults, | ||
IacFileParseFailure, | ||
} from './types'; | ||
|
||
const REQUIRED_K8S_FIELDS = ['apiVersion', 'kind', 'metadata']; | ||
|
||
export async function parseFiles( | ||
filesData: IacFileData[], | ||
): Promise<ParsingResults> { | ||
const parsedFiles: Array<IacFileParsed> = []; | ||
const failedFiles: Array<IacFileParseFailure> = []; | ||
for (const fileData of filesData) { | ||
try { | ||
parsedFiles.push(...tryParseIacFile(fileData)); | ||
} catch (err) { | ||
if (filesData.length === 1) throw err; | ||
failedFiles.push(generateFailedParsedFile(fileData, err)); | ||
} | ||
} | ||
|
||
return { | ||
parsedFiles, | ||
failedFiles, | ||
}; | ||
} | ||
|
||
function generateFailedParsedFile( | ||
{ fileType, filePath, fileContent }: IacFileData, | ||
err: Error, | ||
) { | ||
return { | ||
err, | ||
failureReason: err.message, | ||
fileType, | ||
filePath, | ||
fileContent, | ||
engineType: null, | ||
jsonContent: null, | ||
}; | ||
} | ||
|
||
function tryParseIacFile(fileData: IacFileData): Array<IacFileParsed> { | ||
switch (fileData.fileType) { | ||
case 'yaml': | ||
case 'yml': | ||
case 'json': | ||
return tryParsingKubernetesFile(fileData); | ||
case 'tf': | ||
return tryParsingTerraformFile(fileData); | ||
default: | ||
throw new Error('Invalid IaC file'); | ||
} | ||
} | ||
|
||
function tryParsingKubernetesFile(fileData: IacFileData): IacFileParsed[] { | ||
const yamlDocuments = YAML.safeLoadAll(fileData.fileContent); | ||
|
||
return yamlDocuments.map((parsedYamlDocument, docId) => { | ||
if ( | ||
REQUIRED_K8S_FIELDS.every((requiredField) => | ||
parsedYamlDocument.hasOwnProperty(requiredField), | ||
) | ||
) { | ||
return { | ||
...fileData, | ||
jsonContent: parsedYamlDocument, | ||
engineType: EngineType.Kubernetes, | ||
docId, | ||
}; | ||
} else { | ||
throw new Error('Invalid K8s File!'); | ||
} | ||
}); | ||
} | ||
|
||
function tryParsingTerraformFile(fileData: IacFileData): Array<IacFileParsed> { | ||
try { | ||
// TODO: This parser does not fail on inavlid Terraform files! it is here temporarily. | ||
// cloud-config team will replace it to a valid parser for the beta release. | ||
const parsedData = hclToJson(fileData.fileContent); | ||
return [ | ||
{ | ||
...fileData, | ||
jsonContent: parsedData, | ||
engineType: EngineType.Terraform, | ||
}, | ||
]; | ||
} catch (err) { | ||
throw new Error('Invalid Terraform File!'); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.