Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(cli): add delimiter option for sub file write #1645

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ export class Commander {
.argParser(parseFileSave)
.conflicts('fileWrite'),
)
.option('--delimiter [CHARACTER]', 'append a delimiter to the end of each message, default is "\\n"')
.option(
'-Pp, --protobuf-path <PATH>',
'the path to the .proto file that defines the message format for Protocol Buffers (protobuf)',
Expand Down
4 changes: 2 additions & 2 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const sub = (options: SubscribeOptions) => {
})

client.on('message', (topic, payload, packet) => {
const { format, protobufPath, protobufMessageName, fileSave, fileWrite } = options
const { format, protobufPath, protobufMessageName, fileSave, fileWrite, delimiter } = options

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

Expand All @@ -117,7 +117,7 @@ const sub = (options: SubscribeOptions) => {
const savePath = fileSave ? createNextNumberedFileName(fileSave) : fileWrite
if (savePath) {
fileSave && writeFile(savePath, receivedMessage)
fileWrite && appendFile(savePath, receivedMessage)
fileWrite && appendFile(savePath, receivedMessage, delimiter)
}

options.verbose && msgData.push({ label: 'mqtt-packet', value: packet })
Expand Down
10 changes: 6 additions & 4 deletions cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ declare global {
interface SubscribeOptions extends ConnectOptions {
topic: string[]
qos?: QoS[]
// properties of MQTT 5.0
fileWrite?: string
fileSave?: string
// Start properties of MQTT 5.0
no_local?: boolean[]
retainAsPublished?: boolean[]
retainHandling?: QoS[]
subscriptionIdentifier?: number[]
connUserProperties?: Record<string, string | string[]>
// End properties of MQTT 5.0
fileWrite?: string
fileSave?: string
delimiter?: string
format?: FormatType
outputMode?: OutputMode
verbose: boolean
connUserProperties?: Record<string, string | string[]>
protobufPath?: string
protobufMessageName?: string
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/utils/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const writeFile = (filePath: string, data: string | Buffer): void => {
}
}

const appendFile = (filePath: string, data: string | Buffer): void => {
const appendFile = (filePath: string, data: string | Buffer, delimiter = '\n'): void => {
try {
fs.appendFileSync(filePath, `${data}\n`)
fs.appendFileSync(filePath, `${data}${delimiter}`)
} catch (error) {
signale.error(error)
process.exit(1)
Expand Down
Loading