Skip to content

Commit

Permalink
Merge pull request #1031 from scramjetorg/fix/topic-list-cmd
Browse files Browse the repository at this point in the history
fix topic list command
  • Loading branch information
karoltylenda authored Feb 9, 2024
2 parents 7189833 + 2b607fa commit e140747
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions packages/cli/src/lib/commands/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { profileManager } from "../config";
import { displayEntity, displayStream } from "../output";
import { Option, Argument } from "commander";
import { initPlatform } from "../platform";
import { CommandCompleterDetails, CompleterDetailsEvent } from "../../events/completerDetails";
import {
CommandCompleterDetails,
CompleterDetailsEvent,
} from "../../events/completerDetails";

const validateTopicName = (topicName: string) => {
if (topicName.match(/^[\\a-zA-Z0-9_+-]+$/)) {
Expand All @@ -22,9 +25,19 @@ const validateTopicName = (topicName: string) => {
*/
export const topic: CommandDefinition = (program) => {
const format = profileManager.getProfileConfig().format;
const topicNameArgument = new Argument("<topic-name>").argParser(validateTopicName);
const contentTypeOption = new Option("-t, --content-type [content-type]", "Specifies type of data in topic")
.choices(["text/x-ndjson", "application/x-ndjson", "text/plain", "application/octet-stream"])
const topicNameArgument = new Argument("<topic-name>").argParser(
validateTopicName
);
const contentTypeOption = new Option(
"-t, --content-type [content-type]",
"Specifies type of data in topic"
)
.choices([
"text/x-ndjson",
"application/x-ndjson",
"text/plain",
"application/octet-stream",
])
.default("application/x-ndjson");

const topicCmd = program
Expand All @@ -41,44 +54,57 @@ export const topic: CommandDefinition = (program) => {
.addOption(contentTypeOption)
.description("Create topic")
.action(async (topicName, { contentType }) =>
displayEntity(getHostClient().createTopic(topicName, contentType), format)
displayEntity(
getHostClient().createTopic(topicName, contentType),
format
)
);

topicCmd
.command("delete")
.alias("rm")
.addArgument(topicNameArgument)
.description("Delete topic")
.action(async (topicName) => displayEntity(getHostClient().deleteTopic(topicName), format));
.action(async (topicName) =>
displayEntity(getHostClient().deleteTopic(topicName), format)
);

topicCmd
.command("get")
.addArgument(topicNameArgument)
.addOption(contentTypeOption)
.description("Get data from topic")
.action(async (topicName, { contentType }) =>
displayStream(getHostClient().getNamedData(topicName, {}, contentType))
displayStream(
getHostClient().getNamedData(topicName, {}, contentType)
)
);

topicCmd
.command("send")
.addArgument(topicNameArgument)
.argument("[file]")
.addOption(contentTypeOption)
.description("Send data on topic from file, directory or directly through the console")
.description(
"Send data on topic from file, directory or directly through the console"
)
.on(CompleterDetailsEvent, (complDetails: CommandCompleterDetails) => {
complDetails.file = "filenames";
})
.action(async (topicName, filename, { contentType }) => {
await getHostClient().sendTopic(
topicName,
filename ? await getReadStreamFromFile(filename) : process.stdin,
filename
? await getReadStreamFromFile(filename)
: process.stdin,
{},
contentType
);
});

topicCmd.command("ls")
topicCmd
.command("list")
.alias("ls")
.description("List information about topics")
.action(async () => displayEntity(getHostClient().getTopics(), format));
};

0 comments on commit e140747

Please sign in to comment.