From c218bcbc3c2dad91170546563e5bd229da52d47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikolaj=20Lund=20S=C3=B8rensen?= Date: Fri, 10 Jan 2025 17:09:42 +0100 Subject: [PATCH 1/2] Add an possible JSON output of the get message by id. --- pkg/ctl/subscription/get_message_by_id.go | 25 ++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/ctl/subscription/get_message_by_id.go b/pkg/ctl/subscription/get_message_by_id.go index 2bd8cc4f..d06fce53 100644 --- a/pkg/ctl/subscription/get_message_by_id.go +++ b/pkg/ctl/subscription/get_message_by_id.go @@ -85,14 +85,33 @@ func doGetMessageByID(vc *cmdutils.VerbCmd, legerID int64, entryID int64) error return fmt.Errorf("no message found with the given legerID and entryID") } message := messages[0] + propertiesJSON, err := json.Marshal(message.GetProperties()) if err != nil { return err } - vc.Command.Println(fmt.Sprintf(`Message ID: %s + textOutput := fmt.Sprintf(`Message ID: %s Properties: %s -Message: %s`, message.GetMessageID(), propertiesJSON, hex.Dump(message.Payload))) +Message: %s`, message.GetMessageID(), propertiesJSON, hex.Dump(message.Payload)) + + oc := cmdutils.NewOutputContent(). + WithObject(&ReadMessage{ + MessageId: message.GetMessageID(), + Properties: message.GetProperties(), + Payload: message.Payload, + PayloadString: string(message.Payload), + }). + WithText(textOutput) + + err = vc.OutputConfig.WriteOutput(vc.Command.OutOrStdout(), oc) + + return err +} - return nil +type ReadMessage struct { + Properties map[string]string `json:"properties"` + MessageId utils.MessageID `json:"messageId"` + Payload []byte `json:"payload"` + PayloadString string `json:"PayloadString"` } From 1121caea539d2923932b45c64d2c087a75706c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikolaj=20Lund=20S=C3=B8rensen?= Date: Sun, 12 Jan 2025 20:29:00 +0100 Subject: [PATCH 2/2] rename readMessage struct to not export , modify string output --- pkg/ctl/subscription/get_message_by_id.go | 32 ++++++++++++----------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/pkg/ctl/subscription/get_message_by_id.go b/pkg/ctl/subscription/get_message_by_id.go index d06fce53..60ede004 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 legerID and entryID" + @@ -91,16 +98,18 @@ func doGetMessageByID(vc *cmdutils.VerbCmd, legerID int64, entryID int64) error return err } - textOutput := 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, - PayloadString: string(message.Payload), + WithObject(&readMessage{ + MessageId: message.GetMessageID(), + Properties: message.GetProperties(), + Payload: message.Payload, + PayloadAsString: string(message.Payload), }). WithText(textOutput) @@ -108,10 +117,3 @@ Message: %s`, message.GetMessageID(), propertiesJSON, hex.Dump(message.Payload)) return err } - -type ReadMessage struct { - Properties map[string]string `json:"properties"` - MessageId utils.MessageID `json:"messageId"` - Payload []byte `json:"payload"` - PayloadString string `json:"PayloadString"` -}