clv.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 startOfFileMatches = parsedLine.log.match(/>>>> file:(.+)/);
  25. if (startOfFileMatches) {
  26. imageReading = true;
  27. imageFilename = startOfFileMatches[1];
  28. console.log("File found:", imageFilename);
  29. continue;
  30. }
  31. const endOfFileMatches = parsedLine.log.match(/(.+)<<<</);
  32. if (endOfFileMatches) {
  33. imageReading = false;
  34. const toFilename = `${path.dirname(filename)}/${path.basename(filename)}.${imageFilename}`;
  35. console.log("Saving file to:", toFilename);
  36. try {
  37. imageBase64Buffer = imageBase64Buffer.concat(endOfFileMatches[1] ?? []);
  38. Base64.fromBase64String(imageBase64Buffer).toFile(toFilename);
  39. } catch {
  40. console.log(imageBase64Buffer);
  41. }
  42. imageBase64Buffer = "";
  43. continue;
  44. }
  45. if (imageReading && !parsedLine.log.startsWith("cat") && !parsedLine.log.startsWith("/etc/run.sh")) {
  46. imageBase64Buffer = imageBase64Buffer.concat(parsedLine.log);
  47. continue;
  48. }
  49. } else {
  50. await Deno.stdout.write(
  51. encoder.encode(`${options.time ? parsedLine.time.toISOString() + " " : ""}${parsedLine.log}`),
  52. );
  53. }
  54. } else if (imageReading) {
  55. console.log(imageBase64Buffer);
  56. }
  57. }
  58. file.close();
  59. }