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

Allow partition id as a parameter for publish command. #2515

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 15 additions & 4 deletions packages/cli-tools/bin/streamr-stream-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import { wait } from '@streamr/utils'
import es from 'event-stream'
import { createClientCommand, Options as BaseOptions } from '../src/command'
import { createFnParseInt } from '../src/common'

interface Options extends BaseOptions {
partitionKeyField?: string
partitionKeyField?: string,

Check failure on line 11 in packages/cli-tools/bin/streamr-stream-publish.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (,)
partition?: string
}

const publishStream = (
stream: string,
partitionKeyField: string | undefined,
client: StreamrClient
partition: string | undefined,
client: StreamrClient,
): Writable => {
const parser = createFnParseInt('partition')
const writable = new Writable({
objectMode: true,
write: (data: any, _: any, done: any) => {
Expand All @@ -32,7 +36,8 @@
return
}
const partitionKey = (partitionKeyField !== undefined) ? json[partitionKeyField] : undefined
client.publish(stream, json, { partitionKey }).then(
const streamOptions = (partition !== undefined) ? {id: stream, partition: parser(partition)} : stream

Check failure on line 39 in packages/cli-tools/bin/streamr-stream-publish.ts

View workflow job for this annotation

GitHub Actions / lint

A space is required after '{'

Check failure on line 39 in packages/cli-tools/bin/streamr-stream-publish.ts

View workflow job for this annotation

GitHub Actions / lint

A space is required before '}'
client.publish(streamOptions, json, { partitionKey }).then(
() => done(),
(err) => done(err)
)
Expand All @@ -42,7 +47,12 @@
}

createClientCommand(async (client: StreamrClient, streamId: string, options: Options) => {
const ps = publishStream(streamId, options.partitionKeyField, client)
if (options.partitionKeyField !== undefined && options.partition !== undefined) {
console.error('Partition key and partition id are mutually exclusive. Use only one of these options.')
process.exit()
}

const ps = publishStream(streamId, options.partitionKeyField, options.partition, client)
return new Promise((resolve, reject) => {
process.stdin
.pipe(es.split())
Expand All @@ -63,4 +73,5 @@
.arguments('<streamId>')
.description('publish to a stream by reading JSON messages from stdin line-by-line')
.option('-k, --partition-key-field <string>', 'field name in each message to use for assigning the message to a stream partition')
.option('-p, --partition [partition]', 'partition')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the use of createFnParseInthere. See streamr-stream-subscribe.ts for example.

.parseAsync()
2 changes: 1 addition & 1 deletion packages/cli-tools/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
}
return n
}
}
}

Check failure on line 25 in packages/cli-tools/src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Newline required at end of file but not found
Loading