parse output correctly

This commit is contained in:
2025-11-16 16:32:11 +01:00
parent c8a6daf0d8
commit cb1c85c2b7
2 changed files with 108 additions and 3 deletions

View File

@@ -11,11 +11,75 @@ export class EfiBootMgrService {
await $`efibootmgr -n ${bootNum}`;
}
parseEfiBootMgrOutput(output: string): z.output<typeof BootEntries> {
const lines = output.split("\n");
const isBootNextSet = output.includes("BootNext:");
if (lines.length < 4) {
throw new Error("Invalid output!");
}
let outputValues: {
bootNext?: string;
bootCurrent: string;
timeout: string;
bootOrder: string;
entries: Array<string>;
};
if (isBootNextSet) {
const [bootNext, bootCurrent, timeout, bootOrder, ...entries] = lines;
if (!bootNext || !bootCurrent || !timeout || !bootOrder) {
throw new Error("Invalid output!");
}
outputValues = {
bootNext,
bootCurrent,
timeout,
bootOrder,
entries,
};
} else {
const [bootCurrent, timeout, bootOrder, ...entries] = lines;
if (!bootCurrent || !timeout || !bootOrder) {
throw new Error("Invalid output!");
}
outputValues = {
bootCurrent,
timeout,
bootOrder,
entries,
};
}
return outputValues.entries.map((v) => {
const [bootStr, labelStr] = v.split("* ");
if (!bootStr || !labelStr) throw new Error("Invalid output!");
const digits = bootStr.match(/\d/g);
if (digits === null) throw new Error("invalid output!");
const [osName] = labelStr.split("\t");
if (!osName) throw new Error("Invalid output!");
return {
label: osName,
number: Number.parseInt(digits.join(""), 10),
};
});
}
async listBootEntries(): Promise<z.output<typeof BootEntries>> {
const output = await $`efibootmgr`.text();
console.log(output);
return [];
return this.parseEfiBootMgrOutput(output);
}
}