clv.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import * as path from "https://deno.land/std@0.87.0/path/mod.ts";
  2. import { readLines } from "https://deno.land/std@0.87.0/io/mod.ts";
  3. import { parse } from "https://deno.land/std@0.87.0/flags/mod.ts";
  4. import { Base64 } from "https://deno.land/x/bb64@1.1.0/mod.ts";
  5. interface LogEntry {
  6. log: string;
  7. stream: "stdout";
  8. time: Date;
  9. }
  10. const encoder = new TextEncoder();
  11. const options = parse(Deno.args, { boolean: true });
  12. for (const filename of options._ as string[]) {
  13. let imageReading = false;
  14. let imageBase64Buffer = "";
  15. let imageFilename = "";
  16. const file = await Deno.open(filename);
  17. for await (const line of readLines(file)) {
  18. if (line) {
  19. const parsedLine = JSON.parse(
  20. line,
  21. (key, value) => key === "time" ? new Date(value) : value,
  22. ) as LogEntry;
  23. if (options.files) {
  24. const matches = parsedLine.log.match(/>>>> file:(.+)/);
  25. if (matches) {
  26. imageReading = true;
  27. imageFilename = matches[1];
  28. console.log("File found:", imageFilename);
  29. continue;
  30. }
  31. if (parsedLine.log.startsWith("<<<<")) {
  32. imageReading = false;
  33. const toFilename = `${path.dirname(filename)}/${path.basename(filename)}.${imageFilename}`;
  34. console.log("Saving file to:", toFilename);
  35. Base64.fromBase64String(imageBase64Buffer).toFile(toFilename);
  36. imageBase64Buffer = "";
  37. continue;
  38. }
  39. if (imageReading && !parsedLine.log.startsWith("cat")) {
  40. imageBase64Buffer = imageBase64Buffer.concat(parsedLine.log);
  41. continue;
  42. }
  43. } else {
  44. await Deno.stdout.write(
  45. encoder.encode(`${options.time ? parsedLine.time.toISOString() : ""} ${parsedLine.log}`),
  46. );
  47. }
  48. }
  49. }
  50. file.close();
  51. }