| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import * as path from "https://deno.land/std@0.87.0/path/mod.ts";
- import { readLines } from "https://deno.land/std@0.87.0/io/mod.ts";
- import { parse } from "https://deno.land/std@0.87.0/flags/mod.ts";
- import { Base64 } from "https://deno.land/x/bb64@1.1.0/mod.ts";
- interface LogEntry {
- log: string;
- stream: "stdout";
- time: Date;
- }
- const encoder = new TextEncoder();
- const options = parse(Deno.args, { boolean: true });
- for (const filename of options._ as string[]) {
- let imageReading = false;
- let imageBase64Buffer = "";
- let imageFilename = "";
- const file = await Deno.open(filename);
- for await (const line of readLines(file)) {
- if (line) {
- const parsedLine = JSON.parse(
- line,
- (key, value) => key === "time" ? new Date(value) : value,
- ) as LogEntry;
- if (options.files) {
- const startOfFileMatches = parsedLine.log.match(/>>>> file:(.+)/);
- if (startOfFileMatches) {
- imageReading = true;
- imageFilename = startOfFileMatches[1];
- console.log("File found:", imageFilename);
- continue;
- }
- const endOfFileMatches = parsedLine.log.match(/(.+)<<<</);
- if (endOfFileMatches) {
- imageReading = false;
- const toFilename = `${path.dirname(filename)}/${path.basename(filename)}.${imageFilename}`;
- console.log("Saving file to:", toFilename);
- try {
- imageBase64Buffer = imageBase64Buffer.concat(endOfFileMatches[1] ?? []);
- Base64.fromBase64String(imageBase64Buffer).toFile(toFilename);
- } catch {
- console.log(imageBase64Buffer);
- }
- imageBase64Buffer = "";
- continue;
- }
- if (imageReading && !parsedLine.log.startsWith("cat") && !parsedLine.log.startsWith("/etc/run.sh")) {
- imageBase64Buffer = imageBase64Buffer.concat(parsedLine.log);
- continue;
- }
- } else {
- await Deno.stdout.write(
- encoder.encode(`${options.time ? parsedLine.time.toISOString() + " " : ""}${parsedLine.log}`),
- );
- }
- } else if (imageReading) {
- console.log(imageBase64Buffer);
- }
- }
- file.close();
- }
|