2021-09-15 07:20:31 +00:00
|
|
|
import { TerraformProviderDatasource } from '../../datasource/terraform-provider';
|
2021-10-20 04:38:49 +00:00
|
|
|
import { regEx } from '../../util/regex';
|
2021-09-15 07:20:31 +00:00
|
|
|
import type { PackageDependency } from '../types';
|
2021-05-11 17:08:02 +00:00
|
|
|
import { TerraformDependencyTypes } from './common';
|
2021-09-15 07:20:31 +00:00
|
|
|
import type { ProviderLock } from './lockfile/types';
|
2020-07-01 15:17:40 +00:00
|
|
|
|
2021-10-20 04:38:49 +00:00
|
|
|
export const keyValueExtractionRegex = regEx(
|
|
|
|
/^\s*(?<key>[^\s]+)\s+=\s+"(?<value>[^"]+)"\s*$/
|
|
|
|
);
|
|
|
|
export const resourceTypeExtractionRegex = regEx(
|
|
|
|
/^\s*resource\s+"(?<type>[^\s]+)"\s+"(?<name>[^"]+)"\s*{/
|
|
|
|
);
|
2020-07-01 15:17:40 +00:00
|
|
|
|
|
|
|
export function getTerraformDependencyType(
|
|
|
|
value: string
|
|
|
|
): TerraformDependencyTypes {
|
|
|
|
switch (value) {
|
|
|
|
case 'module': {
|
|
|
|
return TerraformDependencyTypes.module;
|
|
|
|
}
|
|
|
|
case 'provider': {
|
|
|
|
return TerraformDependencyTypes.provider;
|
|
|
|
}
|
|
|
|
case 'required_providers': {
|
|
|
|
return TerraformDependencyTypes.required_providers;
|
|
|
|
}
|
2020-07-02 17:20:30 +00:00
|
|
|
case 'resource': {
|
|
|
|
return TerraformDependencyTypes.resource;
|
|
|
|
}
|
2021-02-21 16:51:41 +00:00
|
|
|
case 'terraform': {
|
|
|
|
return TerraformDependencyTypes.terraform_version;
|
|
|
|
}
|
2020-07-01 15:17:40 +00:00
|
|
|
default: {
|
|
|
|
return TerraformDependencyTypes.unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function checkFileContainsDependency(
|
|
|
|
content: string,
|
|
|
|
checkList: string[]
|
|
|
|
): boolean {
|
2020-12-11 08:35:56 +00:00
|
|
|
return checkList.some((check) => content.includes(check));
|
2020-07-01 15:17:40 +00:00
|
|
|
}
|
2020-07-02 17:20:30 +00:00
|
|
|
|
2021-10-20 04:38:49 +00:00
|
|
|
const pathStringRegex = regEx(/(.|..)?(\/[^/])+/);
|
2020-07-02 17:20:30 +00:00
|
|
|
export function checkIfStringIsPath(path: string): boolean {
|
|
|
|
const match = pathStringRegex.exec(path);
|
|
|
|
return !!match;
|
|
|
|
}
|
2021-09-15 07:20:31 +00:00
|
|
|
|
|
|
|
export function massageProviderLookupName(dep: PackageDependency): void {
|
|
|
|
if (!dep.lookupName) {
|
|
|
|
dep.lookupName = dep.depName;
|
|
|
|
}
|
|
|
|
if (!dep.lookupName.includes('/')) {
|
|
|
|
dep.lookupName = `hashicorp/${dep.lookupName}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle cases like `Telmate/proxmox`
|
|
|
|
dep.lookupName = dep.lookupName.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLockedVersion(
|
|
|
|
dep: PackageDependency,
|
|
|
|
locks: ProviderLock[]
|
|
|
|
): string {
|
|
|
|
const depRegistryUrl = dep.registryUrls
|
|
|
|
? dep.registryUrls[0]
|
|
|
|
: TerraformProviderDatasource.defaultRegistryUrls[0];
|
|
|
|
const foundLock = locks.find(
|
|
|
|
(lock) =>
|
|
|
|
lock.lookupName === dep.lookupName && lock.registryUrl === depRegistryUrl
|
|
|
|
);
|
|
|
|
if (foundLock) {
|
|
|
|
return foundLock.version;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|