2020-09-28 06:17:17 +00:00
|
|
|
const { readFileSync } = require('fs');
|
|
|
|
const { execSync } = require('child_process');
|
|
|
|
|
|
|
|
const findLastFromLineIndex = (lines) => lines.reduce((acc, line, index) => (
|
|
|
|
line.startsWith('FROM') ? index : acc
|
|
|
|
), 0);
|
|
|
|
|
|
|
|
const getDockerfileImage = (file) => {
|
|
|
|
const lines = readFileSync(file, { encoding: 'utf8' })
|
|
|
|
.split('\n');
|
|
|
|
|
|
|
|
const lastFromLine = lines[findLastFromLineIndex(lines)];
|
|
|
|
return lastFromLine.split(' ')[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
const getImageManifest = (image) => execSync(`docker manifest inspect ${image}`, {
|
|
|
|
encoding: 'utf8',
|
|
|
|
});
|
|
|
|
|
|
|
|
const translateManifestPlatform = ({ os, architecture, variant }) => (variant
|
|
|
|
? `${os}/${architecture}/${variant}`
|
|
|
|
: `${os}/${architecture}`);
|
|
|
|
|
|
|
|
const getImagePlatforms = (image) => JSON.parse(getImageManifest(image)).manifests
|
|
|
|
.map(({ platform }) => translateManifestPlatform(platform));
|
|
|
|
|
|
|
|
const getDockerfileArches = (file, supportedPlatformsStr) => {
|
|
|
|
const basePlatforms = getImagePlatforms(getDockerfileImage(file));
|
2020-09-28 07:28:26 +00:00
|
|
|
console.log('Base platforms: ', basePlatforms.join(', '));
|
2020-09-28 06:17:17 +00:00
|
|
|
const supportedPlatforms = supportedPlatformsStr.split(',');
|
2020-10-03 02:03:25 +00:00
|
|
|
return basePlatforms.filter(
|
|
|
|
(platform) => supportedPlatforms.some((supPlatform) => platform.startsWith(supPlatform)),
|
|
|
|
).join(',');
|
2020-09-28 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getDockerfileArches;
|