From 2860172cf15525b4a38d75ebc4c25a998de774c5 Mon Sep 17 00:00:00 2001 From: Willi Carlsen Date: Wed, 27 Apr 2022 14:12:22 +0200 Subject: [PATCH] Alter list topics to utilize json output --- .envrc | 1 + server/makefile | 2 +- .../server/wrapper/connected/CCloudTopics.ts | 25 +++++++++++++------ server/src/server/wrapper/model/topics.ts | 9 +++++++ 4 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 .envrc create mode 100644 server/src/server/wrapper/model/topics.ts diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..40448e6 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +dotenv \ No newline at end of file diff --git a/server/makefile b/server/makefile index 387b9c6..f988745 100644 --- a/server/makefile +++ b/server/makefile @@ -5,7 +5,7 @@ build: docker build -t $(IMAGE_NAME) . run: - docker run -it -p 3000:3000 --rm $(IMAGE_NAME) + docker run -it -p 3000:3000 --rm --env-file ../.env $(IMAGE_NAME) release: build chmod +x ../scripts/push_container_image.sh && ../scripts/push_container_image.sh $(IMAGE_NAME) $(BUILD_NUMBER) diff --git a/server/src/server/wrapper/connected/CCloudTopics.ts b/server/src/server/wrapper/connected/CCloudTopics.ts index e5c2c35..d874c4d 100644 --- a/server/src/server/wrapper/connected/CCloudTopics.ts +++ b/server/src/server/wrapper/connected/CCloudTopics.ts @@ -2,24 +2,33 @@ import { parse, parseTopicDescription } from "./../parser"; import { executeCli } from "./executeCli"; import { TopicAlreadyExistsException } from "../model/error"; import { GetConfig } from "../../config"; +import { Deserializer, ConcatOutput } from "../utils"; +import ListTopics from "../model/topics"; + export class CcloudTopics implements Topics { async getTopics(): Promise { let config = GetConfig(); - let result = await executeCli(["kafka", "topic", "list", "--cluster", config.clusterId, "--environment", config.environmentId]); - result = - parse(result) - .filter(t => t.Name.startsWith("_confluent") === false) - .map(t => t.Name); - - return result; + let result = await executeCli(["kafka", "topic", "list", "--cluster", config.clusterId, "--environment", config.environmentId, "--output", "json"]); + + let combinedResult = ConcatOutput(result); + let deserializedResult : ListTopics; + try { + deserializedResult = Deserializer(combinedResult); + } catch (error) { + return error; + } + + return deserializedResult + .filter(t => t.name.startsWith("_confluent") === false) + .map(t => t.name); } async describeTopic(name: string): Promise { let config = GetConfig(); - let consoleLines = await executeCli(["kafka", "topic", "describe", name, "--cluster", config.clusterId, "--environment", config.environmentId]); + let consoleLines = await executeCli(["kafka", "topic", "describe", name, "--cluster", config.clusterId, "--environment", config.environmentId, "--output", "json"]); var topic = parseTopicDescription(consoleLines); diff --git a/server/src/server/wrapper/model/topics.ts b/server/src/server/wrapper/model/topics.ts new file mode 100644 index 0000000..d6c5235 --- /dev/null +++ b/server/src/server/wrapper/model/topics.ts @@ -0,0 +1,9 @@ +export class ListTopic { + name: string +} + +type ListTopics = Array; + +export default ListTopics; + +