parse output correctly
This commit is contained in:
41
src/efibootmgr.service.test.ts
Normal file
41
src/efibootmgr.service.test.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { describe, it } from "bun:test";
|
||||||
|
import { EfiBootMgrService } from "./efibootmgr.service";
|
||||||
|
|
||||||
|
const EXAMPLE_EFI_BOOT_MGR_OUTPUT = `BootCurrent: 0001
|
||||||
|
Timeout: 2 seconds
|
||||||
|
BootOrder: 0001,0004,0005,0002
|
||||||
|
Boot0001* Fedora HD(1,GPT,)
|
||||||
|
Boot0002* Windows Boot Manager HD(1,GPT,)
|
||||||
|
Boot0004* Linux Boot Manager HD(1,GPT,)
|
||||||
|
Boot0005* UEFI OS HD(1,GPT,)`;
|
||||||
|
|
||||||
|
const EXAMPLE_EFI_BOOT_MGR_OUTPUT_WITH_BOOT_NEXT = `BootNext: 0002
|
||||||
|
BootCurrent: 0001
|
||||||
|
Timeout: 2 seconds
|
||||||
|
BootOrder: 0001,0004,0005,0002
|
||||||
|
Boot0001* Fedora HD(1,GPT,)
|
||||||
|
Boot0002* Windows Boot Manager HD(1,GPT,)
|
||||||
|
Boot0004* Linux Boot Manager HD(1,GPT,)
|
||||||
|
Boot0005* UEFI OS HD(1,GPT,)`;
|
||||||
|
|
||||||
|
describe("EfiBootMgrService", () => {
|
||||||
|
it("parses the example efibootmgr output and returns boot entries", () => {
|
||||||
|
const efiBootMgrService = new EfiBootMgrService();
|
||||||
|
|
||||||
|
const result = efiBootMgrService.parseEfiBootMgrOutput(
|
||||||
|
EXAMPLE_EFI_BOOT_MGR_OUTPUT,
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses the example efibootmgr output with boot next set and returns boot entries", () => {
|
||||||
|
const efiBootMgrService = new EfiBootMgrService();
|
||||||
|
|
||||||
|
const result = efiBootMgrService.parseEfiBootMgrOutput(
|
||||||
|
EXAMPLE_EFI_BOOT_MGR_OUTPUT_WITH_BOOT_NEXT,
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -11,11 +11,75 @@ export class EfiBootMgrService {
|
|||||||
await $`efibootmgr -n ${bootNum}`;
|
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>> {
|
async listBootEntries(): Promise<z.output<typeof BootEntries>> {
|
||||||
const output = await $`efibootmgr`.text();
|
const output = await $`efibootmgr`.text();
|
||||||
|
|
||||||
console.log(output);
|
return this.parseEfiBootMgrOutput(output);
|
||||||
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user