@eighty4/c2
Version:
Cross platform cloud config tooling for cloud-init
35 lines (34 loc) • 1.29 kB
JavaScript
import { evalTemplateExpressions } from "./expression.js";
import { readDirListing, readToString } from "./fs.js";
export async function collectAttachments(dir) {
const filenames = await readDirListing(dir);
const attachments = await Promise.all(filenames.map(async (filename) => {
const path = `${dir}/${filename}`;
const source = await readToString(path);
const type = resolveAttachmentType(filename, source);
const content = await evalTemplateExpressions(source);
return { content, filename, path, type, source };
}));
return attachments.sort(compareAttachmentFilenames);
}
function compareAttachmentFilenames(a1, a2) {
if (a1.filename < a2.filename)
return -1;
if (a1.filename > a2.filename)
return 1;
return 0;
}
export function resolveAttachmentType(filename, source) {
if (filename.endsWith('.yml') || filename.endsWith('.yaml')) {
if (!source.trim().startsWith('#cloud-config')) {
throw new Error('YAML cloud config must start with a #cloud-config comment');
}
return 'cloud-config';
}
else if (filename.endsWith('.sh')) {
return 'x-shellscript';
}
else {
throw new Error(`${filename} is an unsupported file type`);
}
}