-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
extract.ts
60 lines (52 loc) · 1.75 KB
/
extract.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
import yaml from 'js-yaml';
import { logger } from '../../logger';
import { getDep } from '../dockerfile/extract';
import { PackageFile, PackageDependency } from '../common';
import {
matchesHelmValuesDockerHeuristic,
HelmDockerImageDependency,
} from './util';
/**
* Recursively find all supported dependencies in the yaml object.
*
* @param parsedContent
*/
function findDependencies(
parsedContent: object | HelmDockerImageDependency,
packageDependencies: Array<PackageDependency>
): Array<PackageDependency> {
if (!parsedContent || typeof parsedContent !== 'object') {
return packageDependencies;
}
Object.keys(parsedContent).forEach(key => {
if (matchesHelmValuesDockerHeuristic(key, parsedContent[key])) {
const currentItem = parsedContent[key];
const registry = currentItem.registry ? `${currentItem.registry}/` : '';
packageDependencies.push(
getDep(`${registry}${currentItem.repository}:${currentItem.tag}`)
);
} else {
findDependencies(parsedContent[key], packageDependencies);
}
});
return packageDependencies;
}
export function extractPackageFile(content: string): PackageFile {
try {
// a parser that allows extracting line numbers would be preferable, with
// the current approach we need to match anything we find again during the update
const parsedContent = yaml.safeLoad(content, { json: true });
logger.debug(
{ parsedContent },
'Trying to find dependencies in helm-values'
);
const deps = findDependencies(parsedContent, []);
if (deps.length) {
logger.debug({ deps }, 'Found dependencies in helm-values');
return { deps };
}
} catch (err) {
logger.error({ err }, 'Failed to parse helm-values file');
}
return null;
}