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 matches = parsedLine.log.match(/>>>> file:(.+)/); if (matches) { imageReading = true; imageFilename = matches[1]; console.log("File found:", imageFilename); continue; } if (parsedLine.log.startsWith("<<<<")) { imageReading = false; const toFilename = `${path.dirname(filename)}/${path.basename(filename)}.${imageFilename}`; console.log("Saving file to:", toFilename); Base64.fromBase64String(imageBase64Buffer).toFile(toFilename); imageBase64Buffer = ""; continue; } if (imageReading && !parsedLine.log.startsWith("cat")) { imageBase64Buffer = imageBase64Buffer.concat(parsedLine.log); continue; } } else { await Deno.stdout.write( encoder.encode(`${options.time ? parsedLine.time.toISOString() : ""} ${parsedLine.log}`), ); } } } file.close(); }