Skip to content

Commit

Permalink
Merge branch 'main' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fgeller committed Sep 9, 2023
2 parents 81a3ff6 + ba48c15 commit 21aba8e
Show file tree
Hide file tree
Showing 28 changed files with 748 additions and 458 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Continuous Integration

on:
workflow_dispatch:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:

test:
name: kt Test
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.21

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Bring up containers
run: docker compose -f test-dependencies.yml up -d

- name: Test
run: make test
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ release-darwin:
release: testing clean release-linux release-darwin

dep-up:
docker-compose -f ./test-dependencies.yml up -d --remove-orphan
sleep 4
docker compose -f ./test-dependencies.yml up -d

dep-down:
docker-compose -f ./test-dependencies.yml down
docker compose -f ./test-dependencies.yml down

testing: dep-up test dep-down

test: clean
go test -v -vet=all -failfast
go test -v -vet=all -failfast -race

.PHONY: test-secrets
test-secrets:
cd test-secrets ; /usr/bin/env bash create-certs.sh

clean:
rm -f kt
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# kt - a Kafka tool that likes JSON [![Build Status](https://travis-ci.org/fgeller/kt.svg?branch=master)](https://travis-ci.org/fgeller/kt)
# kt - a Kafka tool that likes JSON [![Continuous Integration](https://github.com/fgeller/kt/actions/workflows/go.yml/badge.svg)](https://github.com/fgeller/kt/actions/workflows/go.yml)

Some reasons why you might be interested:

Expand All @@ -7,14 +7,15 @@ Some reasons why you might be interested:
* Modify consumer group offsets (e.g., resetting or manually setting offsets per topic and per partition).
* JSON output for easy consumption with tools like [kp](https://github.com/echojc/kp) or [jq](https://stedolan.github.io/jq/).
* JSON input to facilitate automation via tools like [jsonify](https://github.com/fgeller/jsonify).
* Configure brokers, topic and authentication via environment variables `KT_BROKERS`, `KT_TOPIC` and `KT_AUTH`.
* Configure brokers, topic, Kafka version and authentication via environment variables `KT_BROKERS`, `KT_TOPIC`, `KT_KAFKA_VERSION` and `KT_AUTH`.
* Fast start up time.
* No buffering of output.
* Binary keys and payloads can be passed and presented in base64 or hex encoding.
* Support for TLS authentication.
* Basic cluster admin functions: Create & delete topics.

I'm not using kt actively myself anymore, so if you think it's lacking some feature - please let me know by creating an issue!
> [!NOTE]
> I'm not using kt actively myself anymore, so if you think it's lacking some feature - please let me know by creating an issue.
## Examples

Expand Down Expand Up @@ -213,7 +214,7 @@ You can download kt via the [Releases](https://github.com/fgeller/kt/releases) s

Alternatively, the usual way via the go tool, for example:

$ go get -u github.com/fgeller/kt
$ go install github.com/fgeller/kt/v14@latest

Or via Homebrew on OSX:

Expand Down Expand Up @@ -283,11 +284,16 @@ Required fields:

- `mode`: This needs to be set to `TLS-1way`

Optional fields:

- `ca-certificate`: Path to your CA certificate


Example:


{
"mode": "TLS-1way",
"mode": "TLS-1way"
}

### Other modes
Expand Down
44 changes: 34 additions & 10 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"strings"
"time"

"github.com/Shopify/sarama"
"github.com/IBM/sarama"
)

type adminCmd struct {
baseCmd

brokers []string
verbose bool
version sarama.KafkaVersion
timeout *time.Duration
auth authConfig
Expand Down Expand Up @@ -45,14 +46,25 @@ type adminArgs struct {
func (cmd *adminCmd) parseArgs(as []string) {
var (
args = cmd.parseFlags(as)
err error
)

cmd.verbose = args.verbose
cmd.version = kafkaVersion(args.version)
cmd.version, err = chooseKafkaVersion(args.version, os.Getenv(ENV_KAFKA_VERSION))
if err != nil {
failf("failed to read kafka version err=%v", err)
}

cmd.timeout, err = parseTimeout(os.Getenv(ENV_ADMIN_TIMEOUT))
if err != nil {
failf("failed to read timeout from env var err=%v", err)
}

cmd.timeout = parseTimeout(os.Getenv(ENV_ADMIN_TIMEOUT))
if args.timeout != "" {
cmd.timeout = parseTimeout(args.timeout)
cmd.timeout, err = parseTimeout(args.timeout)
if err != nil {
failf("failed to read timeout from args err=%v", err)
}
}

readAuthFile(args.auth, os.Getenv(ENV_AUTH), &cmd.auth)
Expand Down Expand Up @@ -90,6 +102,18 @@ func (cmd *adminCmd) parseArgs(as []string) {
}
}

func parseTimeout(s string) (*time.Duration, error) {
if s == "" {
return nil, nil
}

v, err := time.ParseDuration(s)
if err != nil {
return nil, err
}
return &v, nil
}

func (cmd *adminCmd) run(args []string) {
var err error

Expand All @@ -105,7 +129,6 @@ func (cmd *adminCmd) run(args []string) {

if cmd.createTopic != "" {
cmd.runCreateTopic()

} else if cmd.deleteTopic != "" {
cmd.runDeleteTopic()
} else {
Expand Down Expand Up @@ -136,9 +159,10 @@ func (cmd *adminCmd) saramaConfig() *sarama.Config {

cfg.Version = cmd.version
if usr, err = user.Current(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to read current user err=%v", err)
cmd.infof("Failed to read current user err=%v", err)
}
cfg.ClientID = "kt-admin-" + sanitizeUsername(usr.Username)
cmd.infof("sarama client configuration %#v\n", cfg)

if cmd.timeout != nil {
cfg.Admin.Timeout = *cmd.timeout
Expand Down Expand Up @@ -167,9 +191,9 @@ func (cmd *adminCmd) parseFlags(as []string) adminArgs {
flags.StringVar(&args.deleteTopic, "deletetopic", "", "Name of the topic that should be deleted.")

flags.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage of admin:")
warnf("Usage of admin:")
flags.PrintDefaults()
fmt.Fprintln(os.Stderr, adminDocString)
warnf(adminDocString + "\n")
}

err := flags.Parse(as)
Expand All @@ -189,7 +213,7 @@ The value supplied on the command line wins over the environment variable value.
If both -createtopic and deletetopic are supplied, -createtopic wins.
The topic details should be passed via a JSON file that represents a sarama.TopicDetail struct.
cf https://godoc.org/github.com/Shopify/sarama#TopicDetail
cf https://godoc.org/github.com/IBM/sarama#TopicDetail
A simple way to pass a JSON file is to use a tool like https://github.com/fgeller/jsonify and shell's process substition:
Expand Down
Loading

0 comments on commit 21aba8e

Please sign in to comment.