Skip to content

Commit

Permalink
feat: Subscription get message by id json output (#1704)
Browse files Browse the repository at this point in the history
* Add an possible JSON output of the get message by id.

* rename readMessage struct to not export , modify string output

(cherry picked from commit 6c42e3a)
  • Loading branch information
Nikolajls authored and zymap committed Jan 15, 2025
1 parent 9926010 commit 75b0160
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions pkg/ctl/subscription/get_message_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down Expand Up @@ -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
}

0 comments on commit 75b0160

Please sign in to comment.