From b96ff228e78c8acd844485dd190def56e888cbaf Mon Sep 17 00:00:00 2001 From: Nikos Kontakis Date: Thu, 31 Aug 2023 15:49:08 +0300 Subject: [PATCH] Add LogType 'text' in order to provide different way of logging (#1282) --- README.md | 1 + docs/src/cli/index.md | 1 + javascript/README.md | 1 + javascript/packages/cli/README.md | 1 + javascript/packages/cli/src/actions/spawn.ts | 3 +- javascript/packages/cli/src/actions/test.ts | 4 +- javascript/packages/cli/src/cli.ts | 7 ++- javascript/packages/orchestrator/README.md | 1 + .../packages/orchestrator/src/orchestrator.ts | 16 ++++-- .../packages/orchestrator/src/spawner.ts | 5 +- .../orchestrator/src/test-runner/index.ts | 9 ++-- javascript/packages/utils/src/tableCli.ts | 49 ++++++++++++++++--- 12 files changed, 77 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ff473fe8f..d1dbdc658 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ Options: -c, --spawn-concurrency Number of concurrent spawning process to launch, default is 1 -p, --provider Override provider to use (choices: "podman", "kubernetes", "native") default: kubernetes + -l, --logType " Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent") -d, --dir Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet) -f, --force Force override all prompt commands -m, --monitor Start as monitor, do not auto cleanup network diff --git a/docs/src/cli/index.md b/docs/src/cli/index.md index d39ea9efa..4afa036c4 100644 --- a/docs/src/cli/index.md +++ b/docs/src/cli/index.md @@ -10,6 +10,7 @@ Options: -c, --spawn-concurrency Number of concurrent spawning process to launch, default is 1 -p, --provider Override provider to use (choices: "podman","kubernetes", "native", default: kubernetes) -d, --dir Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet) + -l, --logType " Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent") -f, --force Force override all prompt commands -m, --monitor Start as monitor, do not auto cleanup network -h, --help display help for command diff --git a/javascript/README.md b/javascript/README.md index a55478609..33f97601a 100644 --- a/javascript/README.md +++ b/javascript/README.md @@ -127,6 +127,7 @@ Options: -p, --provider Override provider to use (choices: "podman", "kubernetes", "native") default: kubernetes -d, --dir Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet) + -l, --logType " Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent") -f, --force Force override all prompt commands -m, --monitor Start as monitor, do not auto cleanup network -h, --help display help for command diff --git a/javascript/packages/cli/README.md b/javascript/packages/cli/README.md index e05acdfbb..1b5ebfc33 100644 --- a/javascript/packages/cli/README.md +++ b/javascript/packages/cli/README.md @@ -127,6 +127,7 @@ Options: -p, --provider Override provider to use (choices: "podman", "kubernetes", "native") default: kubernetes -d, --dir Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet) + -l, --logType " Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent") -f, --force Force override all prompt commands -m, --monitor Start as monitor, do not auto cleanup network -h, --help display help for command diff --git a/javascript/packages/cli/src/actions/spawn.ts b/javascript/packages/cli/src/actions/spawn.ts index 5ecd49f1b..fe68f22c6 100644 --- a/javascript/packages/cli/src/actions/spawn.ts +++ b/javascript/packages/cli/src/actions/spawn.ts @@ -4,6 +4,7 @@ import { decorators, getCredsFilePath, readNetworkConfig, + getLogType, } from "@zombienet/utils"; import fs from "fs"; import { resolve } from "path"; @@ -95,7 +96,7 @@ export async function spawn( dir, force, inCI, - silent: false, + logType: getLogType(opts.logType), setGlobalNetwork, }; const network = await start(creds, config, options); diff --git a/javascript/packages/cli/src/actions/test.ts b/javascript/packages/cli/src/actions/test.ts index 54be786b1..3fc3b351a 100644 --- a/javascript/packages/cli/src/actions/test.ts +++ b/javascript/packages/cli/src/actions/test.ts @@ -1,7 +1,7 @@ import parser from "@zombienet/dsl-parser-wrapper"; import type { TestDefinition } from "@zombienet/orchestrator"; import { run } from "@zombienet/orchestrator"; -import { decorators, RelativeLoader } from "@zombienet/utils"; +import { decorators, RelativeLoader, getLogType } from "@zombienet/utils"; import fs from "fs"; import { Environment } from "nunjucks"; import path from "path"; @@ -68,7 +68,7 @@ export async function test( providerToUse, inCI, opts.spawnConcurrency, - false, + getLogType(opts.logType), runningNetworkSpec, dir, ); diff --git a/javascript/packages/cli/src/cli.ts b/javascript/packages/cli/src/cli.ts index 7760d5709..b5185fe49 100644 --- a/javascript/packages/cli/src/cli.ts +++ b/javascript/packages/cli/src/cli.ts @@ -85,7 +85,12 @@ program ["podman", "kubernetes", "native"], ), ) - + .addOption( + new Option( + "-l, --logType ", + "Type of logging - defaults to 'table'", + ).choices(["table", "text", "silent"]), + ) .addOption( new Option( "-d, --dir ", diff --git a/javascript/packages/orchestrator/README.md b/javascript/packages/orchestrator/README.md index 3616c84b5..9408c62c8 100644 --- a/javascript/packages/orchestrator/README.md +++ b/javascript/packages/orchestrator/README.md @@ -127,6 +127,7 @@ Options: -p, --provider Override provider to use (choices: "podman", "kubernetes", "native") default: kubernetes -d, --dir Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet) + -l, --logType " Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent") -f, --force Force override all prompt commands -m, --monitor Start as monitor, do not auto cleanup network -h, --help display help for command diff --git a/javascript/packages/orchestrator/src/orchestrator.ts b/javascript/packages/orchestrator/src/orchestrator.ts index c810e7fe0..9758c6163 100644 --- a/javascript/packages/orchestrator/src/orchestrator.ts +++ b/javascript/packages/orchestrator/src/orchestrator.ts @@ -1,3 +1,4 @@ +import type { LogType } from "@zombienet/utils"; import { CreateLogTable, PARACHAIN_NOT_FOUND, @@ -11,7 +12,7 @@ import { loadTypeDef, makeDir, series, - setSilent, + setLogType, sleep, } from "@zombienet/utils"; import fs from "fs"; @@ -64,7 +65,7 @@ export interface OrcOptionsInterface { inCI?: boolean; dir?: string; force?: boolean; - silent?: boolean; // Mute logging output + logType?: LogType; // Set the logging output setGlobalNetwork?: (network: Network) => void; } @@ -74,11 +75,16 @@ export async function start( options?: OrcOptionsInterface, ) { const opts = { - ...{ monitor: false, spawnConcurrency: 1, inCI: false, silent: true }, + ...{ + monitor: false, + spawnConcurrency: 1, + inCI: false, + logType: "silent" as LogType, + }, ...options, }; - setSilent(opts.silent); + setLogType(opts.logType as LogType); let network: Network | undefined; let cronInterval = undefined; @@ -362,7 +368,7 @@ export async function start( } const spawnOpts = { - silent: opts.silent, + logType: opts.logType as LogType, inCI: opts.inCI, monitorIsAvailable, userDefinedTypes, diff --git a/javascript/packages/orchestrator/src/spawner.ts b/javascript/packages/orchestrator/src/spawner.ts index d604edf22..f631ef0de 100644 --- a/javascript/packages/orchestrator/src/spawner.ts +++ b/javascript/packages/orchestrator/src/spawner.ts @@ -1,3 +1,4 @@ +import type { LogType } from "@zombienet/utils"; import { CreateLogTable, decorators, @@ -30,7 +31,7 @@ export const spawnNode = async ( bootnodes: string[], filesToCopy: fileMap[], opts: { - silent: boolean; + logType: LogType; inCI: boolean; monitorIsAvailable: boolean; userDefinedTypes?: any; @@ -231,7 +232,7 @@ export const spawnNode = async ( ]); logTable.print(); - if (!opts.silent) + if (opts.logType !== "silent") console.log(client.getLogsCommand(podDef.metadata.name) + "\n\n"); } diff --git a/javascript/packages/orchestrator/src/test-runner/index.ts b/javascript/packages/orchestrator/src/test-runner/index.ts index 44956ec03..185d86501 100644 --- a/javascript/packages/orchestrator/src/test-runner/index.ts +++ b/javascript/packages/orchestrator/src/test-runner/index.ts @@ -2,8 +2,9 @@ import { decorators, getLokiUrl, readNetworkConfig, - setSilent, + setLogType, sleep, + LogType, } from "@zombienet/utils"; import fs from "fs"; import Mocha from "mocha"; @@ -34,11 +35,11 @@ export async function run( provider: string, inCI = false, concurrency = 1, - silent = false, + logType: LogType = "table", runningNetworkSpecPath: string | undefined, dir: string | undefined, ) { - setSilent(silent); + logType && setLogType(logType); let network: Network; const backchannelMap: BackchannelMap = {}; @@ -92,7 +93,7 @@ export async function run( network = await start(creds!, config, { spawnConcurrency: concurrency, inCI, - silent, + logType, dir, }); } else { diff --git a/javascript/packages/utils/src/tableCli.ts b/javascript/packages/utils/src/tableCli.ts index f393a935d..418d2bc09 100644 --- a/javascript/packages/utils/src/tableCli.ts +++ b/javascript/packages/utils/src/tableCli.ts @@ -4,6 +4,7 @@ import Table, { HorizontalTableRow, VerticalTableRow, } from "cli-table3"; +import { decorators } from "./colors"; type CharsObj = { [key in @@ -54,15 +55,37 @@ interface TableCreationProps { wordWrap?: boolean; } +export type LogType = "text" | "table" | "silent"; + // Module level config. -let silent = true; -export function setSilent(value: boolean) { - silent = value; -} +let logType: LogType = "table"; +const logTypeValues = ["text", "table", "silent"]; + +export const getLogType = (logType: LogType): LogType => { + if (logTypeValues.includes(logType)) { + return logType; + } else { + logType && + console.error( + `${decorators.red(` + Argument 'logType' provided ('${logType}') is not one of the accepted params; Falling back to 'table'. + Possible values: ${logTypeValues.join( + ", ", + )} - Defaults to 'table'.\n\n`)}`, + ); + return "table"; + } +}; + +export const setLogType = (value: LogType) => { + logType = value; +}; + export class CreateLogTable { table: CreatedTable | undefined; colWidths: number[]; wordWrap: boolean; + text: string[]; constructor({ head, colWidths, doubleBorder, wordWrap }: TableCreationProps) { this.wordWrap = wordWrap || false; @@ -74,6 +97,7 @@ export class CreateLogTable { if (doubleBorder) { params.chars = chars; } + this.text = []; this.table = new Table(params); } @@ -96,12 +120,25 @@ export class CreateLogTable { input[index] = inp; } }); - this.table!.push(input); + if (logType === "text") { + if (input[0] === "\x1B[36mCommand\x1B[0m") { + input[1] = input[1].replace(/\n/g, " "); + } + // if input has a JSON - that means a merged cell + if (input[0]?.content) { + input[0] = input[0]?.content; + } + console.log(input.join(" : ")); + } else if (logType === "silent") { + return; + } else if (logType === "table") { + this.table!.push(input); + } }); }; print = () => { - if (!silent) console.log(this.table!.toString()); + if (logType === "table") console.log(this.table!.toString()); }; // This function makes the process of creating a table, pushing data and printing it faster