forked from renovatebot/renovate
/
generic-docker-image-ref.ts
105 lines (97 loc) · 3.32 KB
/
generic-docker-image-ref.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import is from '@sindresorhus/is';
import { getDep } from '../../../dockerfile/extract';
import type { ExtractConfig, PackageDependency } from '../../../types';
import { DependencyExtractor } from '../../base';
import type { TerraformDefinitionFile } from '../../hcl/types';
import type { ProviderLock } from '../../lockfile/types';
import { generic_image_resource } from './utils';
export class GenericDockerImageRefExtractor extends DependencyExtractor {
getCheckList(): string[] {
return generic_image_resource.map((value) => `"${value.type}"`);
}
extract(
hclMap: TerraformDefinitionFile,
_locks: ProviderLock[],
config: ExtractConfig
): PackageDependency[] {
const resourceTypMap = hclMap.resource;
if (is.nullOrUndefined(resourceTypMap)) {
return [];
}
const dependencies = [];
for (const image_resource_def of generic_image_resource) {
const { type, path } = image_resource_def;
const resourceInstancesMap = resourceTypMap[type];
// is there a resource with current looked at type ( `image_resource_def` )
if (!is.nonEmptyObject(resourceInstancesMap)) {
continue;
}
// loop over instances of a resource type
for (const instance of Object.values(resourceInstancesMap).flat()) {
dependencies.push(
...this.walkPath({ depType: type }, instance, path, config)
);
}
}
return dependencies;
}
/**
* Recursively follow the path to find elements on the path.
* If a path element is '*' the parentElement will be interpreted as a list
* and each element will be followed
* @param abstractDep dependency which will used as basis for adding attributes
* @param parentElement element from which the next element will be extracted
* @param leftPath path elements left to walk down
*/
private walkPath(
abstractDep: PackageDependency,
parentElement: unknown,
leftPath: string[],
config: ExtractConfig
): PackageDependency[] {
const dependencies: PackageDependency[] = [];
// if there are no path elements left, we have reached the end of the path
if (leftPath.length === 0) {
// istanbul ignore if
if (!is.nonEmptyString(parentElement)) {
return [
{
...abstractDep,
skipReason: 'invalid-dependency-specification',
},
];
}
const test = getDep(parentElement, true, config.registryAliases);
const dep: PackageDependency = {
...abstractDep,
...test,
};
return [dep];
}
// is this a list iterator
const pathElement = leftPath[0];
// get sub element
const element = is.nonEmptyObject(parentElement)
? parentElement[pathElement]
: null;
if (is.nullOrUndefined(element)) {
return leftPath.length === 1 // if this is the last element assume a false defined dependency
? [
{
...abstractDep,
skipReason: 'invalid-dependency-specification',
},
]
: [];
}
if (is.array(element)) {
for (const arrayElement of element) {
dependencies.push(
...this.walkPath(abstractDep, arrayElement, leftPath.slice(1), config)
);
}
return dependencies;
}
return this.walkPath(abstractDep, element, leftPath.slice(1), config);
}
}