2022-03-03 09:35:26 +00:00
|
|
|
import { logger } from '../../../logger';
|
|
|
|
import { regEx } from '../../../util/regex';
|
2022-05-26 04:47:10 +00:00
|
|
|
import { GithubReleasesDatasource } from '../../datasource/github-releases';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { PackageDependency } from '../types';
|
2021-05-11 17:08:02 +00:00
|
|
|
import { TerraformDependencyTypes } from './common';
|
2022-04-20 04:19:59 +00:00
|
|
|
import type { ExtractionResult, TerraformManagerData } from './types';
|
2021-05-11 17:08:02 +00:00
|
|
|
import { keyValueExtractionRegex } from './util';
|
2021-02-21 16:51:41 +00:00
|
|
|
|
|
|
|
export function extractTerraformRequiredVersion(
|
|
|
|
startingLine: number,
|
|
|
|
lines: string[]
|
2022-04-20 04:19:59 +00:00
|
|
|
): ExtractionResult | null {
|
|
|
|
const deps: PackageDependency<TerraformManagerData>[] = [];
|
2021-02-21 16:51:41 +00:00
|
|
|
let lineNumber = startingLine;
|
|
|
|
let braceCounter = 0;
|
|
|
|
do {
|
|
|
|
// istanbul ignore if
|
|
|
|
if (lineNumber > lines.length - 1) {
|
|
|
|
logger.debug(`Malformed Terraform file detected.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const line = lines[lineNumber];
|
|
|
|
// `{` will be counted wit +1 and `}` with -1. Therefore if we reach braceCounter == 0. We have found the end of the terraform block
|
2022-06-21 12:02:49 +00:00
|
|
|
const openBrackets = (line.match(regEx(/\{/g)) ?? []).length;
|
|
|
|
const closedBrackets = (line.match(regEx(/\}/g)) ?? []).length;
|
2021-02-21 16:51:41 +00:00
|
|
|
braceCounter = braceCounter + openBrackets - closedBrackets;
|
|
|
|
|
|
|
|
const kvMatch = keyValueExtractionRegex.exec(line);
|
2022-04-20 04:19:59 +00:00
|
|
|
if (kvMatch?.groups && kvMatch.groups.key === 'required_version') {
|
|
|
|
const dep: PackageDependency<TerraformManagerData> = {
|
2021-02-21 16:51:41 +00:00
|
|
|
currentValue: kvMatch.groups.value,
|
|
|
|
lineNumber,
|
|
|
|
managerData: {
|
|
|
|
terraformDependencyType: TerraformDependencyTypes.terraform_version,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
deps.push(dep);
|
|
|
|
// returning starting line as required_providers are also in the terraform block
|
|
|
|
// if we would return the position of the required_version line we would potentially skip the providers
|
|
|
|
return { lineNumber: startingLine, dependencies: deps };
|
|
|
|
}
|
|
|
|
|
|
|
|
lineNumber += 1;
|
|
|
|
} while (braceCounter !== 0);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function analyseTerraformVersion(dep: PackageDependency): void {
|
2021-04-04 12:48:52 +00:00
|
|
|
dep.depType = 'required_version';
|
2022-05-26 04:47:10 +00:00
|
|
|
dep.datasource = GithubReleasesDatasource.id;
|
2021-02-21 16:51:41 +00:00
|
|
|
dep.depName = 'hashicorp/terraform';
|
|
|
|
dep.extractVersion = 'v(?<version>.*)$';
|
|
|
|
}
|