From 748a3adbccfa3e0d5f0a49c63786a594fd528a61 Mon Sep 17 00:00:00 2001 From: Matt Parides Date: Mon, 4 Jun 2018 10:43:30 -0700 Subject: [PATCH] added max-length flag to topics tail --- commands/topics_tail.js | 6 +++++- test/commands/topics_tail_test.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/commands/topics_tail.js b/commands/topics_tail.js index bd6d354..de27137 100644 --- a/commands/topics_tail.js +++ b/commands/topics_tail.js @@ -60,7 +60,7 @@ function * tail (context, heroku) { cli.log(context.args.TOPIC, partition, m.offset, 0, 'NULL') return } - let length = Math.min(buffer.length, MAX_LENGTH) + let length = Math.min(buffer.length, parseInt(context.flags['max-length']) || MAX_LENGTH) let body = buffer.toString('utf8', 0, length) cli.log(context.args.TOPIC, partition, m.offset, buffer.length, body) }) @@ -81,6 +81,9 @@ let cmd = { { name: 'TOPIC' }, { name: 'CLUSTER', optional: true } ], + flags: [ + { name: 'max-length', description: 'number of characters per message to output', hasValue: true } + ], help: ` Tails a topic in Kafka. Note: kafka:tail is not available in Heroku Private Spaces. @@ -88,6 +91,7 @@ let cmd = { $ heroku kafka:topics:tail page-visits $ heroku kafka:topics:tail page-visits kafka-aerodynamic-32763 + $ heroku kafka:topics:tail page-visits --max-length 200 `, needsApp: true, needsAuth: true, diff --git a/test/commands/topics_tail_test.js b/test/commands/topics_tail_test.js index f7dfc67..0e7a195 100644 --- a/test/commands/topics_tail_test.js +++ b/test/commands/topics_tail_test.js @@ -129,13 +129,38 @@ describe('kafka:topics:tail', () => { tail.process.emit('SIGINT') } - return cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }}) + return cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }, flags: {}}) .then(() => { expect(cli.stdout).to.equal('topic-1 42 1 5 hello\ntopic-1 42 2 5 world\ntopic-1 42 3 0 NULL\n') expect(cli.stderr).to.be.empty }) }) + it('tails a topic and prints the results limited by max-length flag', () => { + api.get('/apps/myapp/config-vars').reply(200, config) + api.get('/apps/myapp/addon-attachments/kafka-1') + .reply(200, { name: 'KAFKA', config_vars: stockConfigVars, app: { name: 'sushi' } }) + + consumer.subscribe = (topic, callback) => { + callback([ + { offset: 1, message: { value: Buffer.from('hello, this is a slightly longer message that shows an output just a litte bit longer than 80') } }, + { offset: 2, message: { value: Buffer.from('Hodor. Hodor hodor... Hodor hodor hodor hodor. Hodor, hodor. Hodor. Hodor, hodor, hodor. Hodor hodor?! Hodor, hodor.') } }, + { offset: 3, message: { value: Buffer.from('world') } }, + { offset: 4, message: { value: null } } + ], undefined, 42) + tail.process.emit('SIGINT') + } + + return cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }, flags: { 'max-length': '100' }}) + .then(() => { + expect(cli.stdout).to.equal('topic-1 42 1 93 hello, this is a slightly longer message that shows an output just a litte bit longer than 80\n' + + 'topic-1 42 2 116 Hodor. Hodor hodor... Hodor hodor hodor hodor. Hodor, hodor. Hodor. Hodor, hodor, hodor. Hodor hodor\n' + + 'topic-1 42 3 5 world\n' + + 'topic-1 42 4 0 NULL\n') + expect(cli.stderr).to.be.empty + }) + }) + it('tails a topic with a prefix and prints the results', () => { let configWithPrefix = Object.assign({ KAFKA_PREFIX: 'nile-1234.' @@ -154,7 +179,7 @@ describe('kafka:topics:tail', () => { tail.process.emit('SIGINT') } - return cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }}) + return cmd.run({app: 'myapp', args: { TOPIC: 'topic-1' }, flags: {}}) .then(() => { expect(cli.stdout).to.equal('topic-1 42 1 5 hello\ntopic-1 42 2 5 world\ntopic-1 42 3 0 NULL\n') expect(cli.stderr).to.be.empty @@ -179,7 +204,7 @@ describe('kafka:topics:tail', () => { tail.process.emit('SIGINT') } - return cmd.run({app: 'myapp', args: { TOPIC: 'nile-1234.topic-1' }}) + return cmd.run({app: 'myapp', args: { TOPIC: 'nile-1234.topic-1' }, flags: {}}) .then(() => { expect(cli.stdout).to.equal('nile-1234.topic-1 42 1 5 hello\nnile-1234.topic-1 42 2 5 world\nnile-1234.topic-1 42 3 0 NULL\n') expect(cli.stderr).to.be.empty