Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new version of logs #169

Merged
merged 15 commits into from
Dec 30, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions src/commands/paas/logs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { cli } from "cli-ux";
import { flags } from "@oclif/command";
import { PaasKommand } from "../../support/PaasKommand";
import chalk, { Chalk, ChalkFunction } from "chalk";
import fs from "fs";

import PaasLogin from "./login";
import { cli } from "cli-ux";
import { PaasKommand } from "../../support/PaasKommand";

class PaasLogs extends PaasKommand {
public static description = "Show logs of the targeted application";
Expand Down Expand Up @@ -34,8 +36,7 @@ class PaasLogs extends PaasKommand {

const user = await this.paas.auth.getCurrentUser();
this.logInfo(
`Logged as "${user._id}" for project "${
this.flags.project || this.getProject()
`Logged as "${user._id}" for project "${this.flags.project || this.getProject()
}"`
);

Expand All @@ -47,7 +48,19 @@ class PaasLogs extends PaasKommand {
applicationId: this.args.application,
});

this.logOk(logs.result.join("\n "));
const separator = " ";
const podNames = Object.keys(logs.result);

for (const podName of podNames) {
const log = logs.result[podName];
const chalkColor = this.getRandomChalkColor();

for (const line of log) {
const spaces = this.getNumberOfSpaces(podNames, podName);
const name = chalkColor(`${line.podName}${separator.repeat(spaces)}`);
this.log(`${name}| ${line.content}`);
}
}
}

async getCredentials() {
Expand All @@ -74,6 +87,39 @@ class PaasLogs extends PaasKommand {

return credentials.apiKey;
}

getNumberOfSpaces(names: string[], currentName: string) {
const end = 10;
let max = { name: '', length: 0 };

for (const name of names) {
if (max.length < name.length) {
max = { name: name, length: name.length };
}
}

return currentName === max.name ? end : end + (max.length - currentName.length);
}

getRandomChalkColor() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this function you can end up with the same color for 2 pods. Maybe it would be better to iterate over pods with an index and use this index to select a color

// Create an array of possible colors
const color = ['green', 'blue', 'yellow', 'cyan'];
const random = Math.floor(Math.random() * color.length);
const key = color[random];

switch (key) {
case 'green':
return chalk.green;
case 'blue':
return chalk.blue;
case 'yellow':
return chalk.yellow;
case 'cyan':
return chalk.cyan;
default:
return chalk.green;
}
}
}

export default PaasLogs;