|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/Shopify/sarama" |
| 11 | + "github.com/fgrosse/cli" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/spf13/viper" |
| 14 | +) |
| 15 | + |
| 16 | +func (cmd *Kafkactl) GetMessageCmd() *cobra.Command { |
| 17 | + getMessageCmd := &cobra.Command{ |
| 18 | + Use: "message --topic=foo --offset=123", |
| 19 | + Args: cobra.NoArgs, |
| 20 | + Short: "Consume messages from a Kafka cluster", |
| 21 | + Example: ` |
| 22 | +# Print message with offset 81041238 from topic my-fancy-topic |
| 23 | +kafkactl get message --topic=my-fancy-topic --offset=81041238 |
| 24 | +
|
| 25 | +# Read offsets from std in and print all corresponding messages |
| 26 | +kubectl logs -l app=my-app | jq 'select(…) | .offset' | kafkactl get message --offset=- --topic=my-fancy-topic |
| 27 | +`, |
| 28 | + RunE: func(_ *cobra.Command, args []string) error { |
| 29 | + ctx := cli.Context() |
| 30 | + offset := viper.GetString("offset") |
| 31 | + topic := viper.GetString("topic") |
| 32 | + partition := viper.GetInt32("partition") |
| 33 | + encoding := viper.GetString("output") |
| 34 | + return cmd.getMessage(ctx, offset, topic, partition, encoding) |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + flags := getMessageCmd.Flags() |
| 39 | + flags.String("offset", "", `The Kafka offset that should be fetched. Can either be a number or the string "-" to read numbers from stdin (newline delimited)`) |
| 40 | + flags.String("topic", "", "Kafka topic") |
| 41 | + flags.Int32("partition", 0, "Kafka topic partition") |
| 42 | + |
| 43 | + // change default for --output flag |
| 44 | + flags.StringP("output", "o", "json", "Output format. One of json|raw") |
| 45 | + |
| 46 | + _ = getMessageCmd.MarkFlagRequired("offset") |
| 47 | + _ = getMessageCmd.MarkFlagRequired("topic") |
| 48 | + |
| 49 | + return getMessageCmd |
| 50 | +} |
| 51 | + |
| 52 | +func (cmd *Kafkactl) getMessage(ctx context.Context, offset, topic string, partition int32, encoding string) error { |
| 53 | + if encoding != "json" && encoding != "raw" { |
| 54 | + return errors.New("only JSON and raw output are supported by this sub command") |
| 55 | + } |
| 56 | + |
| 57 | + if topic == "" { |
| 58 | + return errors.New("empty topic flag") |
| 59 | + } |
| 60 | + |
| 61 | + dec, err := cmd.topicDecoder(topic) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + printMessage := func(offsetStr string) error { |
| 67 | + offset, err := strconv.Atoi(offsetStr) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to parse offset from stdin: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + msg, err := cmd.fetchMessageForOffset(topic, partition, int64(offset)) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if encoding == "raw" { |
| 78 | + _, err := os.Stdout.Write(msg.Value) |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + decoded, err := dec.Decode(msg) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + return cli.Print(encoding, decoded) |
| 88 | + } |
| 89 | + |
| 90 | + if offset == "-" { |
| 91 | + for line := range cli.ReadLines(ctx) { |
| 92 | + err := printMessage(line) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + } |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + return printMessage(offset) |
| 101 | +} |
| 102 | + |
| 103 | +func (cmd *Kafkactl) fetchMessageForOffset(topic string, partition int32, offset int64) (*sarama.ConsumerMessage, error) { |
| 104 | + conf := cmd.saramaConfig() |
| 105 | + conf.Metadata.Full = false // we are only interested in very specific topics |
| 106 | + conf.Producer.Return.Successes = true |
| 107 | + |
| 108 | + client, err := cmd.connectClientWithConfig(conf) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + defer client.Close() |
| 114 | + |
| 115 | + broker, err := client.Leader(topic, partition) |
| 116 | + if err != nil { |
| 117 | + return nil, fmt.Errorf("failed to determine leader for partition %d of topic %q: %w", partition, topic, err) |
| 118 | + } |
| 119 | + |
| 120 | + return cmd.FetchMessage(broker, topic, partition, offset) |
| 121 | +} |
0 commit comments