8
8
9
9
kafkaflagutil "github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
10
10
11
- "strings"
12
-
13
11
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/kafkacmdutil"
14
12
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
15
13
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
@@ -26,6 +24,7 @@ const (
26
24
DefaultOffset = ""
27
25
DefaultLimit = 20
28
26
DefaultTimestamp = ""
27
+ DefaultPartition = - 1
29
28
FormatKeyValue = "key-value"
30
29
)
31
30
@@ -84,7 +83,7 @@ func NewConsumeTopicCommand(f *factory.Factory) *cobra.Command {
84
83
flags := kafkaflagutil .NewFlagSet (cmd , f .Localizer )
85
84
86
85
flags .StringVar (& opts .topicName , "name" , "" , f .Localizer .MustLocalize ("kafka.topic.common.flag.name.description" ))
87
- flags .Int32Var (& opts .partition , "partition" , 0 , f .Localizer .MustLocalize ("kafka.topic.consume.flag.partition.description" ))
86
+ flags .Int32Var (& opts .partition , "partition" , DefaultPartition , f .Localizer .MustLocalize ("kafka.topic.consume.flag.partition.description" ))
88
87
flags .StringVar (& opts .date , "from-date" , DefaultTimestamp , f .Localizer .MustLocalize ("kafka.topic.consume.flag.date.description" ))
89
88
flags .StringVar (& opts .timestamp , "from-timestamp" , DefaultTimestamp , f .Localizer .MustLocalize ("kafka.topic.consume.flag.timestamp.description" ))
90
89
flags .BoolVar (& opts .wait , "wait" , false , f .Localizer .MustLocalize ("kafka.topic.consume.flag.wait.description" ))
@@ -191,8 +190,11 @@ func consumeAndWait(opts *options, api *kafkainstanceclient.APIClient, kafkaInst
191
190
192
191
func consume (opts * options , api * kafkainstanceclient.APIClient , kafkaInstance * kafkamgmtclient.KafkaRequest ) (* kafkainstanceclient.RecordList , error ) {
193
192
194
- request := api .RecordsApi .ConsumeRecords (opts .f .Context , opts .topicName ).Limit (opts .limit ).Partition (opts .partition )
195
-
193
+ request := api .RecordsApi .ConsumeRecords (opts .f .Context , opts .topicName ).Limit (opts .limit )
194
+ if opts .partition != DefaultPartition {
195
+ opts .f .Logger .Info (opts .f .Localizer .MustLocalize ("kafka.topic.consume.partition.value" , localize .NewEntry ("Partition" , opts .partition )))
196
+ request = request .Partition (opts .partition )
197
+ }
196
198
if opts .offset != DefaultOffset {
197
199
intOffset , err := strconv .ParseInt (opts .offset , 10 , 64 )
198
200
if err != nil {
@@ -284,6 +286,10 @@ func outputRecords(opts *options, records *kafkainstanceclient.RecordList) {
284
286
} else {
285
287
opts .f .Logger .Info (fmt .Sprintf ("Key: %v\n Message: %v" , row .Key , row .Value ))
286
288
}
289
+ opts .f .Logger .Info (fmt .Sprintf ("Offset: %v" , row .Offset ))
290
+ if opts .partition == DefaultPartition {
291
+ opts .f .Logger .Info (fmt .Sprintf ("Partition: %v" , row .Partition ))
292
+ }
287
293
} else {
288
294
_ = dump .Formatted (opts .f .IOStreams .Out , format , row )
289
295
opts .f .Logger .Info ("" )
@@ -299,10 +305,10 @@ func mapRecordsToRows(topic string, records *[]kafkainstanceclient.Record) []kaf
299
305
record := & (* records )[i ]
300
306
row := kafkaRow {
301
307
Topic : topic ,
302
- Key : * record .Key ,
303
- Value : strings . TrimSuffix ( record .Value , " \n " ), // trailing new line gives weird printing of table
304
- Partition : * record .Partition ,
305
- Offset : * record .Offset ,
308
+ Key : record .GetKey () ,
309
+ Value : record .Value ,
310
+ Partition : record .GetPartition () ,
311
+ Offset : record .GetOffset () ,
306
312
}
307
313
308
314
rows [i ] = row
0 commit comments