Skip to content

Commit d0ffd6e

Browse files
committed
feat: retrieve the PaaS logs using a streamed query
1 parent 481c202 commit d0ffd6e

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

src/commands/paas/logs.ts

+60-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
import fs from "fs";
2+
import * as readline from "readline";
3+
14
import { cli } from "cli-ux";
25
import { flags } from "@oclif/command";
36
import chalk from "chalk";
4-
import fs from "fs";
57

68
import PaasLogin from "./login";
79
import { PaasKommand } from "../../support/PaasKommand";
810

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+
936
class PaasLogs extends PaasKommand {
1037
public static description = "Show logs of the targeted application";
1138

@@ -40,26 +67,48 @@ class PaasLogs extends PaasKommand {
4067
}"`
4168
);
4269

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({
4474
controller: "application",
4575
action: "logs",
4676
environmentId: this.args.environment,
4777
projectId: this.flags.project || this.getProject(),
4878
applicationId: this.args.application,
4979
});
5080

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+
});
5387

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

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;
6299
}
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}`);
63112
}
64113
}
65114

0 commit comments

Comments
 (0)