From cf213d7413afcc1bdce62502ac652f7199c08444 Mon Sep 17 00:00:00 2001 From: Red-Asuka Date: Wed, 29 Jun 2022 16:30:08 +0800 Subject: [PATCH] chore(cli): set default client id --- cli/src/index.ts | 5 +++-- cli/src/utils/generator.ts | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 cli/src/utils/generator.ts diff --git a/cli/src/index.ts b/cli/src/index.ts index 5707c1095..a14d35d0e 100755 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -1,4 +1,5 @@ import { Command } from 'commander' +import { getClientId } from './utils/generator' import { parseNumber, parseProtocol } from './utils/parse' import pub from './lib/pub' import sub from './lib/sub' @@ -19,7 +20,7 @@ export class Commander { .description('Publish a message to a topic.') .option('-h, --hostname ', 'the broker host') .option('-p, --port ', 'the broker port', parseNumber) - .option('-i, --client-id ', 'the client id') + .option('-i, --client-id ', 'the client id', getClientId()) .option('-q, --qos <0/1/2>', 'the QoS of the message', parseNumber, 0) .requiredOption('-t, --topic ', 'the message topic') .option('-m, --message ', 'the message body', 'Hello From MQTT X CLI') @@ -44,7 +45,7 @@ export class Commander { .description('Subscribes to a topic.') .option('-h, --hostname ', 'the broker host', 'localhost') .option('-p, --port ', 'the broker port', parseNumber) - .option('-i, --client-id ', 'the client id') + .option('-i, --client-id ', 'the client id', getClientId()) .option('-q, --qos <0/1/2>', 'the QoS of the message', parseNumber, 0) .option('--clean', 'discard any pending message for the given id', true) .requiredOption('-t, --topic ', 'the message topic') diff --git a/cli/src/utils/generator.ts b/cli/src/utils/generator.ts new file mode 100644 index 000000000..63f1e9fe4 --- /dev/null +++ b/cli/src/utils/generator.ts @@ -0,0 +1,3 @@ +const getClientId = () => `mqttx_${Math.random().toString(16).substring(2, 10)}` + +export { getClientId }