Browse Source

Adding 'files' and 'time' command line parameters

bodicsek 5 years ago
parent
commit
1fd0405c0b
2 changed files with 36 additions and 7 deletions
  1. 1 1
      clv.sh
  2. 35 6
      clv.ts

+ 1 - 1
clv.sh

@@ -1,4 +1,4 @@
 #!/bin/bash
 
 # the preauthenticated link expires at 2022-01-28
-deno run --allow-read https://objectstorage.eu-frankfurt-1.oraclecloud.com/p/nbuO9wa4aysyCFlT7fh3sO11whXZdo_3hIeh0Tz6qR9QVxIOzd7qYMYxneAZ5SIM/n/fria9wwig1n3/b/canary-log-viewer/o/clv.ts "$@"
+deno run --allow-read --allow-write https://objectstorage.eu-frankfurt-1.oraclecloud.com/p/nbuO9wa4aysyCFlT7fh3sO11whXZdo_3hIeh0Tz6qR9QVxIOzd7qYMYxneAZ5SIM/n/fria9wwig1n3/b/canary-log-viewer/o/clv.ts "$@"

+ 35 - 6
clv.ts

@@ -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();