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

feat(cli): support output mode for the sub command #1227

Merged
merged 1 commit into from
Feb 6, 2023
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
7 changes: 7 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
parseVariadicOfBooleanType,
parsePubTopic,
parseFormat,
parseOutputMode,
} from './utils/parse'
import { conn, benchConn } from './lib/conn'
import { pub, benchPub } from './lib/pub'
Expand Down Expand Up @@ -211,6 +212,12 @@ export class Commander {
)
.option('-f, --format <TYPE>', 'format the message body, support base64, json, hex', parseFormat)
.option('-v, --verbose', 'print the topic before the message')
.option(
'--output-mode <default/clean>',
'choose between the default format and the clean mode, the clean mode outputs the complete packet data, allowing users to pipe the output freely',
parseOutputMode,
'default',
)
// connect options
.option('-V, --mqtt-version <5/3.1.1/3.1>', 'the MQTT version', parseMQTTVersion, 5)
.option('-h, --hostname <HOST>', 'the broker host', 'localhost')
Expand Down
28 changes: 16 additions & 12 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ const sub = (options: SubscribeOptions) => {

const client = mqtt.connect(connOpts)

const { maximunReconnectTimes } = options
const { outputMode, maximunReconnectTimes } = options

const outputModeClean = outputMode === 'clean'

let retryTimes = 0

basicLog.connecting(config, connOpts.hostname!, connOpts.port, options.topic.join(', '))
!outputModeClean && basicLog.connecting(config, connOpts.hostname!, connOpts.port, options.topic.join(', '))

client.on('connect', () => {
basicLog.connected()
!outputModeClean && basicLog.connected()

retryTimes = 0

Expand All @@ -36,19 +38,19 @@ const sub = (options: SubscribeOptions) => {
topic.forEach((t: string, index: number) => {
const subOpts = subOptsArray[index]

basicLog.subscribing(t)
!outputModeClean && basicLog.subscribing(t)

client.subscribe(t, subOpts, (err, result) => {
if (err) {
basicLog.error(err)
!outputModeClean && basicLog.error(err)
process.exit(1)
} else {
basicLog.subscribed(t)
!outputModeClean && basicLog.subscribed(t)
}

result.forEach((sub) => {
if (sub.qos > 2) {
basicLog.subscriptionNegated(sub)
!outputModeClean && basicLog.subscriptionNegated(sub)
process.exit(1)
}
})
Expand Down Expand Up @@ -81,27 +83,29 @@ const sub = (options: SubscribeOptions) => {
msgData.push({ label: 'userProperties', value: up })
}

msgLog(msgData)
!outputModeClean
? msgLog(msgData)
: console.log(JSON.stringify({ topic, payload: convertPayload(payload, format), packet }, null, 2))
})

client.on('error', (err) => {
basicLog.error(err)
!outputModeClean && basicLog.error(err)
client.end()
})

client.on('reconnect', () => {
retryTimes += 1
if (retryTimes > maximunReconnectTimes) {
client.end(false, {}, () => {
basicLog.reconnectTimesLimit()
!outputModeClean && basicLog.reconnectTimesLimit()
})
} else {
basicLog.reconnecting()
!outputModeClean && basicLog.reconnecting()
}
})

client.on('close', () => {
basicLog.close()
!outputModeClean && basicLog.close()
})
}

Expand Down
5 changes: 4 additions & 1 deletion cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ declare global {

type FormatType = 'base64' | 'json' | 'hex'

type OutputMode = 'clean' | 'default'

interface ConnectOptions {
mqttVersion: MQTTVersion
hostname: string
Expand Down Expand Up @@ -78,6 +80,7 @@ declare global {
retainHandling?: QoS[]
subscriptionIdentifier?: number[]
format?: FormatType
outputMode?: OutputMode
verbose: boolean
connUserProperties?: Record<string, string | string[]>
}
Expand All @@ -96,7 +99,7 @@ declare global {
verbose: boolean
}

type OmitSubscribeOptions = Omit<SubscribeOptions, 'format'>
type OmitSubscribeOptions = Omit<SubscribeOptions, 'format' | 'outputMode'>

interface BenchSubscribeOptions extends OmitSubscribeOptions {
count: number
Expand Down
9 changes: 9 additions & 0 deletions cli/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ const parseFormat = (value: string) => {
return value
}

const parseOutputMode = (value: string) => {
if (!['clean', 'default'].includes(value)) {
signale.error('Not a valid output mode.')
process.exit(1)
}
return value
}

const parseConnectOptions = (
options: ConnectOptions | PublishOptions | SubscribeOptions,
commandType?: CommandType,
Expand Down Expand Up @@ -326,6 +334,7 @@ export {
checkTopicExists,
parsePubTopic,
parseFormat,
parseOutputMode,
parseConnectOptions,
parsePublishOptions,
parseSubscribeOptions,
Expand Down