Skip to content

Commit 39212c0

Browse files
committed
chore: scaffold CLI with base options
0 parents  commit 39212c0

5 files changed

Lines changed: 409 additions & 0 deletions

File tree

.gitignore

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Compiled class files
2+
*.class
3+
4+
# Log files
5+
*.log
6+
logs/
7+
8+
# BlueJ files
9+
*.ctxt
10+
11+
# Mobile Tools for Java (J2ME)
12+
.mtj.tmp/
13+
14+
# Package Files
15+
*.jar
16+
*.war
17+
*.nar
18+
*.ear
19+
*.zip
20+
*.tar.gz
21+
*.rar
22+
23+
# Maven
24+
target/
25+
pom.xml.tag
26+
pom.xml.releaseBackup
27+
pom.xml.versionsBackup
28+
pom.xml.next
29+
release.properties
30+
dependency-reduced-pom.xml
31+
buildNumber.properties
32+
.mvn/timing.properties
33+
.mvn/wrapper/maven-wrapper.jar
34+
35+
# IDE - IntelliJ IDEA
36+
.idea/
37+
*.iws
38+
*.iml
39+
*.ipr
40+
out/
41+
42+
# IDE - Eclipse
43+
.settings/
44+
.classpath
45+
.project
46+
.metadata
47+
bin/
48+
tmp/
49+
*.tmp
50+
*.bak
51+
*.swp
52+
*~.nib
53+
local.properties
54+
.loadpath
55+
.recommenders
56+
57+
# IDE - VS Code
58+
.vscode/
59+
*.code-workspace
60+
61+
# OS specific
62+
.DS_Store
63+
.DS_Store?
64+
._*
65+
.Spotlight-V100
66+
.Trashes
67+
ehthumbs.db
68+
Thumbs.db
69+
70+
# Application specific
71+
# Uncomment if you want to ignore application-specific files
72+
# application.properties
73+
# application.yml
74+
# application-*.yml

Makefile

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Kafka CLI Makefile
2+
3+
# Variables
4+
JAR_FILE = target/kafka-cli-1.0-SNAPSHOT-jar-with-dependencies.jar
5+
BOOTSTRAP_SERVERS ?= localhost:9092
6+
TOPIC ?= my-topic
7+
GROUP_ID ?= kafka-cli-group
8+
9+
# Default target
10+
.PHONY: all
11+
all: build
12+
13+
# Build the project
14+
.PHONY: build
15+
build:
16+
mvn clean package
17+
18+
# Clean the project
19+
.PHONY: clean
20+
clean:
21+
mvn clean
22+
23+
# Run the help command
24+
.PHONY: help
25+
help:
26+
java -jar $(JAR_FILE) --help
27+
28+
# Produce a message to a topic
29+
.PHONY: produce
30+
produce:
31+
@if [ -z "$(MESSAGE)" ]; then \
32+
echo "Error: MESSAGE is required. Usage: make produce MESSAGE=\"your message\" TOPIC=your-topic"; \
33+
exit 1; \
34+
fi
35+
java -jar $(JAR_FILE) \
36+
--bootstrap-servers $(BOOTSTRAP_SERVERS) \
37+
--topic $(TOPIC) \
38+
--produce \
39+
--message "$(MESSAGE)"
40+
41+
# Consume messages from a topic
42+
.PHONY: consume
43+
consume:
44+
java -jar $(JAR_FILE) \
45+
--bootstrap-servers $(BOOTSTRAP_SERVERS) \
46+
--topic $(TOPIC) \
47+
--consume \
48+
--group-id $(GROUP_ID)
49+
50+
# Run with custom bootstrap servers
51+
.PHONY: run
52+
run:
53+
java -jar $(JAR_FILE) $(ARGS)
54+
55+
# Show version information
56+
.PHONY: version
57+
version:
58+
java -jar $(JAR_FILE) --version
59+
60+
# Example usage instructions
61+
.PHONY: examples
62+
examples:
63+
@echo "Kafka CLI Examples:"
64+
@echo " make build # Build the project"
65+
@echo " make produce MESSAGE=\"Hello, Kafka!\" # Produce a message"
66+
@echo " make consume TOPIC=my-topic # Consume messages"
67+
@echo " make run ARGS=\"--bootstrap-servers kafka:9092 --topic test --consume\" # Run with custom args"
68+
@echo " make help # Show help"
69+
@echo " make version # Show version"

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Kafka CLI
2+
3+
A simple command-line interface for interacting with Apache Kafka.
4+
5+
## Building
6+
7+
To build the project, run:
8+
9+
```bash
10+
mvn clean package
11+
```
12+
13+
This will create a JAR file in the `target` directory named `kafka-cli-1.0-SNAPSHOT-jar-with-dependencies.jar`.
14+
15+
## Using the Makefile
16+
17+
For convenience, a Makefile is provided to simplify common operations:
18+
19+
```bash
20+
# Build the project
21+
make build
22+
23+
# Clean the project
24+
make clean
25+
26+
# Show help
27+
make help
28+
29+
# Produce a message
30+
make produce MESSAGE="Hello, Kafka!" TOPIC=my-topic
31+
32+
# Consume messages
33+
make consume TOPIC=my-topic GROUP_ID=my-consumer-group
34+
35+
# Run with custom arguments
36+
make run ARGS="--bootstrap-servers kafka:9092 --topic test --consume"
37+
38+
# Show version
39+
make version
40+
41+
# Show examples
42+
make examples
43+
```
44+
45+
You can customize the default values by overriding the variables:
46+
47+
```bash
48+
make consume BOOTSTRAP_SERVERS=kafka:9092 TOPIC=my-topic GROUP_ID=my-group
49+
```
50+
51+
## Direct Usage
52+
53+
The CLI supports both producing and consuming messages from Kafka topics. Here are some examples:
54+
55+
### Producing Messages
56+
57+
```bash
58+
java -jar target/kafka-cli-1.0-SNAPSHOT-jar-with-dependencies.jar \
59+
--bootstrap-servers localhost:9092 \
60+
--topic my-topic \
61+
--produce \
62+
--message "Hello, Kafka!"
63+
```
64+
65+
### Consuming Messages
66+
67+
```bash
68+
java -jar target/kafka-cli-1.0-SNAPSHOT-jar-with-dependencies.jar \
69+
--bootstrap-servers localhost:9092 \
70+
--topic my-topic \
71+
--consume \
72+
--group-id my-consumer-group
73+
```
74+
75+
## Options
76+
77+
- `-b, --bootstrap-servers`: Kafka bootstrap servers (required)
78+
- `-t, --topic`: Kafka topic name (required)
79+
- `-m, --message`: Message to produce
80+
- `-c, --consume`: Consume messages from topic
81+
- `-g, --group-id`: Consumer group ID (default: kafka-cli-group)
82+
- `-p, --produce`: Produce message to topic
83+
- `-h, --help`: Show help message
84+
- `-V, --version`: Show version information
85+
86+
## Requirements
87+
88+
- Java 11 or higher
89+
- Maven 3.6 or higher
90+
- Apache Kafka 3.6.1 or compatible version

pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.kafka</groupId>
8+
<artifactId>kafka-cli</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<kafka.version>3.6.1</kafka.version>
15+
<picocli.version>4.7.5</picocli.version>
16+
</properties>
17+
18+
<dependencies>
19+
<!-- Kafka Client -->
20+
<dependency>
21+
<groupId>org.apache.kafka</groupId>
22+
<artifactId>kafka-clients</artifactId>
23+
<version>${kafka.version}</version>
24+
</dependency>
25+
26+
<!-- CLI Framework -->
27+
<dependency>
28+
<groupId>info.picocli</groupId>
29+
<artifactId>picocli</artifactId>
30+
<version>${picocli.version}</version>
31+
</dependency>
32+
33+
<!-- Logging -->
34+
<dependency>
35+
<groupId>org.slf4j</groupId>
36+
<artifactId>slf4j-api</artifactId>
37+
<version>2.0.9</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>ch.qos.logback</groupId>
41+
<artifactId>logback-classic</artifactId>
42+
<version>1.4.14</version>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-assembly-plugin</artifactId>
51+
<version>3.6.0</version>
52+
<configuration>
53+
<archive>
54+
<manifest>
55+
<mainClass>com.kafka.KafkaCli</mainClass>
56+
</manifest>
57+
</archive>
58+
<descriptorRefs>
59+
<descriptorRef>jar-with-dependencies</descriptorRef>
60+
</descriptorRefs>
61+
</configuration>
62+
<executions>
63+
<execution>
64+
<id>make-assembly</id>
65+
<phase>package</phase>
66+
<goals>
67+
<goal>single</goal>
68+
</goals>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>

0 commit comments

Comments
 (0)