Skip to content

Commit 2e9bf34

Browse files
Nikolajlszymap
authored andcommitted
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 (cherry picked from commit 6c42e3a)
1 parent 0b9710e commit 2e9bf34

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

pkg/ctl/subscription/get_message_by_id.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ import (
2929
"github.com/streamnative/pulsarctl/pkg/cmdutils"
3030
)
3131

32+
type readMessage struct {
33+
Properties map[string]string `json:"properties"`
34+
MessageId utils.MessageID `json:"messageId"`
35+
Payload []byte `json:"payload"`
36+
PayloadAsString string `json:"PayloadString"`
37+
}
38+
3239
func GetMessageByIDCmd(vc *cmdutils.VerbCmd) {
3340
var desc cmdutils.LongDescription
3441
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
8592
return fmt.Errorf("no message found with the given ledgerID and entryID")
8693
}
8794
message := messages[0]
95+
8896
propertiesJSON, err := json.Marshal(message.GetProperties())
8997
if err != nil {
9098
return err
9199
}
92100

93-
vc.Command.Println(fmt.Sprintf(`Message ID: %s
94-
Properties: %s
95-
Message: %s`, message.GetMessageID(), propertiesJSON, hex.Dump(message.Payload)))
101+
textOutput := fmt.Sprintf(
102+
`Message ID: %s
103+
Properties: %s
104+
Message:
105+
%s`, message.GetMessageID(), propertiesJSON, hex.Dump(message.Payload))
106+
107+
oc := cmdutils.NewOutputContent().
108+
WithObject(&readMessage{
109+
MessageId: message.GetMessageID(),
110+
Properties: message.GetProperties(),
111+
Payload: message.Payload,
112+
PayloadAsString: string(message.Payload),
113+
}).
114+
WithText(textOutput)
115+
116+
err = vc.OutputConfig.WriteOutput(vc.Command.OutOrStdout(), oc)
96117

97-
return nil
118+
return err
98119
}

0 commit comments

Comments
 (0)