Skip to content

Commit

Permalink
feat: allow reading messages with no encoding (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth authored Jan 26, 2025
1 parent bf3033e commit 4d7b434
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ kplay
records
cosign.key
cosign.pub
screenshots
demo.gif
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Allow dynamic parsing of protobuf encoded messages

### Changed

- Message metadata and value are now shown in a single viewport
- The command line interface; most of the configuration is now taken from the
config file

### Removed

- Keymaps to maximize message metadata or value viewport

## [v0.1.0] - Mar 6, 2024

### Added

- A TUI that allows pulling messages from a kafka topic on demand, and viewing
their metadata and value
- Allow persisting messages to the local filesystem
- Allow skipping messages

[unreleased]: https://github.com/dhth/kplay/compare/v0.1.0...HEAD
[v0.1.0]: https://github.com/dhth/kplay/commits/v0.1.0
10 changes: 9 additions & 1 deletion cmd/assets/sample-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ profiles:
consumerGroup: kplay-consumer-group-1

- name: proto-encoded
authentication: none
authentication: aws_msk_iam
encodingFormat: protobuf
protoConfig:
descriptorSetFile: path/to/descriptor/set/file.pb
Expand All @@ -17,3 +17,11 @@ profiles:
- 127.0.0.1:9092
topic: kplay-test-2
consumerGroup: kplay-consumer-group-1

- name: raw
authentication: none
encodingFormat: raw
brokers:
- 127.0.0.1:9092
topic: kplay-test-3
consumerGroup: kplay-consumer-group-1
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewRootCommand() (*cobra.Command, error) {
rootCmd := &cobra.Command{
Use: "kplay <PROFILE>",
Short: "kplay lets you inspect messages in a Kafka topic in a simple and deliberate manner.",
Long: `kplay ("kafka playground") lets you inspect messages in a Kafka topic in a simple and deliberate manner.",
Long: `kplay ("kafka playground") lets you inspect messages in a Kafka topic in a simple and deliberate manner.
kplay relies on a configuration file that contains profiles for various Kafka topics, each with its own details related
to brokers, message encoding, authentication, etc.
Expand Down Expand Up @@ -105,8 +105,8 @@ Behaviours
`,
config.Topic,
config.ConsumerGroup,
config.AuthenticationValue(),
config.EncodingValue(),
config.AuthenticationDisplay(),
config.EncodingDisplay(),
config.Brokers,
behaviours.PersistMessages,
behaviours.SkipMessages)
Expand Down
25 changes: 25 additions & 0 deletions internal/config/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import "fmt"

const (
awsMSKIAM = "aws_msk_iam"
)

type AuthType uint

const (
NoAuth AuthType = iota
AWSMSKIAM
)

func ValidateAuthValue(value string) (AuthType, error) {
switch value {
case "none":
return NoAuth, nil
case awsMSKIAM:
return AWSMSKIAM, nil
default:
return NoAuth, fmt.Errorf("auth value is missing/incorrect; possible values: [none, %s]", awsMSKIAM)
}
}
58 changes: 9 additions & 49 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,9 @@
package config

import (
"fmt"

"google.golang.org/protobuf/reflect/protoreflect"
)

const (
awsMSKIAM = "aws_msk_iam"
)

type EncodingFormat uint

const (
JSON EncodingFormat = iota
Protobuf
)

func ValidateEncodingFmtValue(value string) (EncodingFormat, error) {
switch value {
case "json":
return JSON, nil
case "protobuf":
return Protobuf, nil
default:
return JSON, fmt.Errorf("encoding format is missing/incorrect; possible values: [json, protobuf]")
}
}

type AuthType uint

const (
NoAuth AuthType = iota
AWSMSKIAM
)

func ValidateAuthValue(value string) (AuthType, error) {
switch value {
case "none":
return NoAuth, nil
case awsMSKIAM:
return AWSMSKIAM, nil
default:
return NoAuth, fmt.Errorf("auth value is missing/incorrect; possible values: [none, %s]", awsMSKIAM)
}
}

type Config struct {
Name string
Authentication AuthType
Expand All @@ -56,12 +14,7 @@ type Config struct {
ProtoMsgDescriptor *protoreflect.MessageDescriptor
}

type Behaviours struct {
PersistMessages bool
SkipMessages bool
}

func (c Config) AuthenticationValue() string {
func (c Config) AuthenticationDisplay() string {
switch c.Authentication {
case NoAuth:
return "none"
Expand All @@ -72,13 +25,20 @@ func (c Config) AuthenticationValue() string {
}
}

func (c Config) EncodingValue() string {
func (c Config) EncodingDisplay() string {
switch c.Encoding {
case JSON:
return "json"
case Protobuf:
return "protobuf"
case Raw:
return "raw"
default:
return "unknown"
}
}

type Behaviours struct {
PersistMessages bool
SkipMessages bool
}
24 changes: 24 additions & 0 deletions internal/config/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import "fmt"

type EncodingFormat uint

const (
JSON EncodingFormat = iota
Protobuf
Raw
)

func ValidateEncodingFmtValue(value string) (EncodingFormat, error) {
switch value {
case "json":
return JSON, nil
case "protobuf":
return Protobuf, nil
case "raw":
return Raw, nil
default:
return JSON, fmt.Errorf("encoding format is missing/incorrect; possible values: [json, protobuf, raw]")
}
}
8 changes: 8 additions & 0 deletions tests/cli/assets/config-raw-encoding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
profiles:
- name: local
authentication: none
encodingFormat: raw
brokers:
- 127.0.0.1:9092
topic: kplay-test-1
consumerGroup: kplay-consumer-group-1
15 changes: 15 additions & 0 deletions tests/cli/root_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestCLI(t *testing.T) {
}
}()

// SUCCESSES
t.Run("Help", func(t *testing.T) {
// GIVEN
// WHEN
Expand All @@ -62,6 +63,20 @@ func TestCLI(t *testing.T) {
assert.NoError(t, err, "output:\n%s", o)
})

t.Run("Parsing profile with raw encoding works", func(t *testing.T) {
// GIVEN
// WHEN
configPath := "assets/config-raw-encoding.yml"
c := exec.Command(binPath, "local", "-c", configPath, "--display-config-only")
o, err := c.CombinedOutput()
// THEN
if err != nil {
fmt.Printf("output:\n%s", o)
}
assert.NoError(t, err, "output:\n%s", o)
})

// FAILURES
t.Run("Fails for absent config file", func(t *testing.T) {
// GIVEN
// WHEN
Expand Down

0 comments on commit 4d7b434

Please # to comment.