|
| 1 | +import fs from "fs"; |
| 2 | +import * as readline from "readline"; |
| 3 | + |
1 | 4 | import { cli } from "cli-ux";
|
2 | 5 | import { flags } from "@oclif/command";
|
3 | 6 | import chalk from "chalk";
|
4 |
| -import fs from "fs"; |
5 | 7 |
|
6 | 8 | import PaasLogin from "./login";
|
7 | 9 | import { PaasKommand } from "../../support/PaasKommand";
|
8 | 10 |
|
| 11 | +/** |
| 12 | + * Data contained a PaaS log, as given by the API. |
| 13 | + */ |
| 14 | +type PaasLogData = { |
| 15 | + /** |
| 16 | + * Contents of the log. |
| 17 | + */ |
| 18 | + content: string; |
| 19 | + |
| 20 | + /** |
| 21 | + * Timestamp of the log. |
| 22 | + */ |
| 23 | + timeStamp: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * Whether this is the last log of the stream. |
| 27 | + */ |
| 28 | + last: boolean; |
| 29 | + |
| 30 | + /** |
| 31 | + * Name of the pod that generated the log. |
| 32 | + */ |
| 33 | + podName: string; |
| 34 | +}; |
| 35 | + |
9 | 36 | class PaasLogs extends PaasKommand {
|
10 | 37 | public static description = "Show logs of the targeted application";
|
11 | 38 |
|
@@ -40,26 +67,48 @@ class PaasLogs extends PaasKommand {
|
40 | 67 | }"`
|
41 | 68 | );
|
42 | 69 |
|
43 |
| - const logs: any = await this.paas.query({ |
| 70 | + const separator = "\t"; |
| 71 | + |
| 72 | + // Perform the streamed request |
| 73 | + const incomingMessage = await this.paas.queryHttpStream({ |
44 | 74 | controller: "application",
|
45 | 75 | action: "logs",
|
46 | 76 | environmentId: this.args.environment,
|
47 | 77 | projectId: this.flags.project || this.getProject(),
|
48 | 78 | applicationId: this.args.application,
|
49 | 79 | });
|
50 | 80 |
|
51 |
| - const separator = " "; |
52 |
| - const podNames = Object.keys(logs.result); |
| 81 | + // Read the response line by line |
| 82 | + const lineStream = readline.createInterface({ |
| 83 | + input: incomingMessage, |
| 84 | + crlfDelay: Infinity, |
| 85 | + terminal: false, |
| 86 | + }); |
53 | 87 |
|
54 |
| - for (const podName of podNames) { |
55 |
| - const log = logs.result[podName]; |
56 |
| - const chalkColor = this.getRandomChalkColor(); |
| 88 | + // Remember the pods color |
| 89 | + const podsColor = new Map<string, any>(); |
57 | 90 |
|
58 |
| - for (const line of log) { |
59 |
| - const spaces = this.getNumberOfSpaces(podNames, podName); |
60 |
| - const name = chalkColor(`${line.podName}${separator.repeat(spaces)}`); |
61 |
| - this.log(`${name}| ${line.content}`); |
| 91 | + // Display the response |
| 92 | + for await (const line of lineStream) { |
| 93 | + // Parse the data |
| 94 | + const data: PaasLogData = JSON.parse(line); |
| 95 | + |
| 96 | + // Exclude logs that are empty or that are not from a pod |
| 97 | + if (!data.content || !data.podName) { |
| 98 | + continue; |
62 | 99 | }
|
| 100 | + |
| 101 | + // Get the pod name and color |
| 102 | + let podColor = podsColor.get(data.podName); |
| 103 | + |
| 104 | + if (!podColor) { |
| 105 | + podColor = this.getRandomChalkColor(); |
| 106 | + podsColor.set(data.podName, podColor); |
| 107 | + } |
| 108 | + |
| 109 | + // Display the log |
| 110 | + const name = podColor(`${data.podName}${separator}`); |
| 111 | + this.log(`${name}| ${data.content}`); |
63 | 112 | }
|
64 | 113 | }
|
65 | 114 |
|
|
0 commit comments