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

fix(cli): highlight sub info on sub output #1700

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 15 additions & 7 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ import { deserializeBufferToProtobuf } from '../utils/protobuf'
import isSupportedBinaryFormatForMQTT from '../utils/binaryFormats'
import * as Debug from 'debug'

/**
*
* Pipeline for processing incoming messages, following two potential steps:
* 1. Protobuf Deserialization --> Utilized if both protobuf path and message name are defined, otherwise message passes as is.
* 2. Format Conversion --> Engaged if a format is defined, converting the message accordingly; if not defined, message passes unchanged.
* Flow:
* payload -> [Protobuf Deserialization] -> [Format Conversion] -> Processed Message
* @param payload - The message payload to be processed.
* @param protobufPath - The path to the Protobuf definition file.
* @param protobufMessageName - The name of the Protobuf message.
* @param format - The format of the payload.
* @returns The processed message as a string or Buffer.
*/
const processReceivedMessage = (
payload: Buffer,
protobufPath?: string,
protobufMessageName?: string,
format?: FormatType,
): string | Buffer => {
let message: string | Buffer = payload
/*
* Pipeline for processing incoming messages, following two potential steps:
* 1. Protobuf Deserialization --> Utilized if both protobuf path and message name are defined, otherwise message passes as is.
* 2. Format Conversion --> Engaged if a format is defined, converting the message accordingly; if not defined, message passes unchanged.
*/
const pipeline = [
(msg: Buffer) =>
protobufPath && protobufMessageName
Expand Down Expand Up @@ -133,7 +141,7 @@ const sub = (options: SubscribeOptions) => {
client.on('message', (topic, payload, packet) => {
const { format, protobufPath, protobufMessageName, fileSave, fileWrite, delimiter } = options

const msgData: Record<string, unknown>[] = []
const msgData: MsgItem[] = []

const receivedMessage = processReceivedMessage(payload, protobufPath, protobufMessageName, format)

Expand All @@ -152,7 +160,7 @@ const sub = (options: SubscribeOptions) => {

if (savePath) {
const successMessage = fileSave ? 'Saved to file' : 'Appended to file'
msgData.push({ label: 'payload', value: `${successMessage}: ${savePath}` })
msgData.push({ label: 'payload', value: `${receivedMessage}\n${successMessage}: ${savePath}` })
} else {
msgData.push({ label: 'payload', value: receivedMessage })
}
Expand Down
5 changes: 5 additions & 0 deletions cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ declare global {
password?: string
}
}

interface MsgItem {
label: string
value: any
}
}

export {}
11 changes: 5 additions & 6 deletions cli/src/utils/logWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ const logWrapper = {

const formatValue = (value: any) => (typeof value === 'object' ? inspect(value, false, null, true) : value)

const msgLog = (msg: Record<string, any>[]) => {
const msgLog = (msg: MsgItem[]) => {
const payloadItems = msg.filter((item) => item.label === 'payload')
const restItems = msg.filter((item) => item.label !== 'payload')
const subInfoItems = msg.filter((item) => item.label !== 'payload')

const payloadStrings = payloadItems.map((item) => formatValue(item.value))
const otherStrings = restItems.map((item) => `${chalk.green(item.label)}: ${chalk.gray(formatValue(item.value))}`)
const chalkString = `${otherStrings.join(', ')}\n${payloadStrings.join('\n')}`

signale.log(chalkString)
const subInfoStrings = subInfoItems.map((item) => `${chalk.green(item.label)}: ${formatValue(item.value)}`)
const outputString = `${subInfoStrings.join(', ')}\n${payloadStrings.join('\n')}`
signale.log(isLogFormat ? outputString : `${outputString}\n`)
}

const basicLog = {
Expand Down
Loading