From 6c42e3a040432aec7b7eeec009fff57e72c86f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikolaj=20Lund=20S=C3=B8rensen?= Date: Wed, 15 Jan 2025 03:21:54 +0100 Subject: [PATCH] feat: Subscription get message by id json output (#1704) * Add an possible JSON output of the get message by id. * rename readMessage struct to not export , modify string output --- pkg/ctl/subscription/get_message_by_id.go | 29 +++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/ctl/subscription/get_message_by_id.go b/pkg/ctl/subscription/get_message_by_id.go index aa4d94c8..f9ad3aed 100644 --- a/pkg/ctl/subscription/get_message_by_id.go +++ b/pkg/ctl/subscription/get_message_by_id.go @@ -29,6 +29,13 @@ import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" ) +type readMessage struct { + Properties map[string]string `json:"properties"` + MessageId utils.MessageID `json:"messageId"` + Payload []byte `json:"payload"` + PayloadAsString string `json:"PayloadString"` +} + func GetMessageByIDCmd(vc *cmdutils.VerbCmd) { var desc cmdutils.LongDescription desc.CommandUsedFor = "This command is used for getting messages by the given ledgerID and entryID" + @@ -85,14 +92,28 @@ func doGetMessageByID(vc *cmdutils.VerbCmd, ledgerID int64, entryID int64) error return fmt.Errorf("no message found with the given ledgerID and entryID") } message := messages[0] + propertiesJSON, err := json.Marshal(message.GetProperties()) if err != nil { return err } - vc.Command.Println(fmt.Sprintf(`Message ID: %s -Properties: %s -Message: %s`, message.GetMessageID(), propertiesJSON, hex.Dump(message.Payload))) + textOutput := fmt.Sprintf( + `Message ID: %s + Properties: %s + Message: + %s`, message.GetMessageID(), propertiesJSON, hex.Dump(message.Payload)) + + oc := cmdutils.NewOutputContent(). + WithObject(&readMessage{ + MessageId: message.GetMessageID(), + Properties: message.GetProperties(), + Payload: message.Payload, + PayloadAsString: string(message.Payload), + }). + WithText(textOutput) + + err = vc.OutputConfig.WriteOutput(vc.Command.OutOrStdout(), oc) - return nil + return err }