Skip to content

Commit 3ee2429

Browse files
authored
Merge pull request #236 from DefangLabs/lio/prep-interpol
Detect interpolated config
2 parents 1aed8d1 + 9c8de14 commit 3ee2429

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

scripts/prep-samples.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const YAML = require('yaml');
44

55
const samplesDir = process.argv[2];
66

7+
// Inspired by https://github.com/compose-spec/compose-go/blob/main/template/template.go
8+
const interpolationRegex =
9+
/(?<!\$)\$(?:{([_a-z][_a-z0-9]*)}|([_a-z][_a-z0-9]*))/gi; // [1] = ${var}, [2] = $var
10+
11+
function interpolatedVars(str) {
12+
return Array.from(str.matchAll(interpolationRegex), (match) => match[1] || match[2]);
13+
}
14+
715
// categories are directories in the current directory (i.e. we're running in samples/ and we might have a samples/ruby/ directory)
816
const directories = fs.readdirSync(samplesDir).filter(file => fs.statSync(path.join(samplesDir, file)).isDirectory());
917

@@ -31,7 +39,7 @@ directories.forEach((sample) => {
3139
const tags = readme.match(/Tags: (.*)/)[1].split(',').map(tag => tag.trim());
3240
const languages = readme.match(/Languages: (.*)/)[1].split(',').map(language => language.trim());
3341

34-
let configs = [];
42+
let configs = new Set();
3543
try {
3644
composeFile = fs.readFileSync(path.join(samplesDir, sample, 'compose.yaml'), 'utf8');
3745
compose = YAML.parse(composeFile);
@@ -41,22 +49,30 @@ directories.forEach((sample) => {
4149
if (Array.isArray(service.environment)) {
4250
service.environment.forEach(env => {
4351
if (!env.includes("=")) {
44-
configs.push(env);
52+
configs.add(env);
4553
}
54+
interpolatedVars(env).forEach(v => {
55+
configs.add(v);
56+
});
4657
});
4758
} else {
4859
for (var name in service.environment) {
4960
value = service.environment[name];
5061
if (value === null || value === undefined || value === "") {
51-
configs.push(name);
62+
configs.add(name);
63+
}
64+
if (typeof value === 'string') {
65+
interpolatedVars(value).forEach(v => {
66+
configs.add(v);
67+
});
5268
}
5369
}
5470
}
5571
}
5672
} catch (error) {
5773
// Ignore if the sample doesn't have a compose file
5874
if (error.code != 'ENOENT') {
59-
console.log(`failed to parese compose for configs for sample`, sample, error);
75+
console.log(`failed to parse compose for configs for sample`, sample, error);
6076
}
6177
}
6278

@@ -70,8 +86,8 @@ directories.forEach((sample) => {
7086
tags,
7187
languages,
7288
};
73-
if (configs.length > 0) {
74-
sampleSummary.configs = configs;
89+
if (configs.size > 0) {
90+
sampleSummary.configs = Array.from(configs);
7591
}
7692
jsonArray.push(sampleSummary);
7793

0 commit comments

Comments
 (0)