|
|
@@ -1,4 +1,8 @@
|
|
|
-import { readLines } from "https://deno.land/std@0.83.0/io/mod.ts";
|
|
|
+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;
|
|
|
@@ -8,8 +12,11 @@ interface LogEntry {
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
-const filenames = Deno.args;
|
|
|
-for (const filename of filenames) {
|
|
|
+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) {
|
|
|
@@ -17,9 +24,31 @@ for (const filename of filenames) {
|
|
|
line,
|
|
|
(key, value) => key === "time" ? new Date(value) : value,
|
|
|
) as LogEntry;
|
|
|
- await Deno.stdout.write(
|
|
|
- encoder.encode(`${parsedLine.time.toISOString()} ${parsedLine.log}`),
|
|
|
- );
|
|
|
+ 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();
|