104 lines
3.2 KiB
JavaScript
104 lines
3.2 KiB
JavaScript
#!/usr/bin/env node
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
function stripComment(line) {
|
|
let quote = "";
|
|
let escaped = false;
|
|
for (let index = 0; index < line.length; index += 1) {
|
|
const character = line[index];
|
|
if (escaped) {
|
|
escaped = false;
|
|
continue;
|
|
}
|
|
if (quote === '"' && character === "\\") {
|
|
escaped = true;
|
|
continue;
|
|
}
|
|
if (quote) {
|
|
if (character === quote) quote = "";
|
|
continue;
|
|
}
|
|
if (character === '"' || character === "'") {
|
|
quote = character;
|
|
continue;
|
|
}
|
|
if (character === "#") return line.slice(0, index);
|
|
}
|
|
return line;
|
|
}
|
|
|
|
function normalizeValue(rawValue) {
|
|
const value = rawValue.trim();
|
|
if (value.length >= 2 && ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'")))) {
|
|
return value.slice(1, -1);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
export function parseBuildkitToml(content) {
|
|
const sections = new Map();
|
|
let currentSection = "";
|
|
|
|
for (const rawLine of content.split(/\r?\n/)) {
|
|
const line = stripComment(rawLine).trim();
|
|
if (!line) continue;
|
|
|
|
const sectionMatch = line.match(/^\[([^\]]+)\]$/);
|
|
if (sectionMatch) {
|
|
currentSection = sectionMatch[1];
|
|
if (sections.has(currentSection)) throw new Error(`duplicate section ${currentSection}`);
|
|
sections.set(currentSection, new Map());
|
|
continue;
|
|
}
|
|
|
|
const assignment = line.match(/^([A-Za-z0-9_-]+)\s*=\s*(.+)$/);
|
|
if (!assignment || !currentSection) throw new Error("unsupported deploy BuildKit TOML syntax");
|
|
const values = sections.get(currentSection);
|
|
if (values.has(assignment[1])) throw new Error(`duplicate key ${currentSection}.${assignment[1]}`);
|
|
values.set(assignment[1], normalizeValue(assignment[2]));
|
|
}
|
|
|
|
return sections;
|
|
}
|
|
|
|
export function verifyLoadedBuildkitConfig(expectedContent, actualContent) {
|
|
try {
|
|
const expected = parseBuildkitToml(expectedContent);
|
|
const actual = parseBuildkitToml(actualContent);
|
|
|
|
for (const [section, expectedValues] of expected) {
|
|
const actualValues = actual.get(section);
|
|
if (!actualValues || actualValues.size !== expectedValues.size) return false;
|
|
for (const [key, expectedValue] of expectedValues) {
|
|
if (actualValues.get(key) !== expectedValue) return false;
|
|
}
|
|
}
|
|
for (const [section, actualValues] of actual) {
|
|
if (!expected.has(section) && !(section === "worker" && actualValues.size === 0)) return false;
|
|
}
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function main() {
|
|
const expectedPath = process.argv[2];
|
|
if (!expectedPath) {
|
|
console.error("Expected deploy BuildKit config path is required");
|
|
process.exit(2);
|
|
}
|
|
|
|
const expectedContent = fs.readFileSync(path.resolve(expectedPath), "utf8");
|
|
const actualContent = fs.readFileSync(0, "utf8");
|
|
if (!verifyLoadedBuildkitConfig(expectedContent, actualContent)) {
|
|
console.error("Running deploy BuildKit configuration does not match the reviewed repository config");
|
|
process.exit(1);
|
|
}
|
|
console.log("Running deploy BuildKit configuration matches the reviewed repository config");
|
|
}
|
|
|
|
if (path.resolve(process.argv[1] ?? "") === fileURLToPath(import.meta.url)) main();
|