Skip to content

feat: auto fill '--command-config' for commands (closes #6) #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ Clusters can be defined to run commands against. Commands automatically add flag

Each cluster should have a unique name (a key in the `clusters` root-level object). By default, the cluster `local` is added.

| key | type | description |
|-----------------------|--------------|---------------------------------------------------------------|
| `bootstrap_servers` | string | Comma-separated `host:port` Kafka brokers to connect to. |
| `zookeeper_connect` | string | Comma-separated `host:port` Zookeeper nodes to connect to. |
| `schema_registry_url` | string | Schema Registry URL used when working with avro schemas. |
| `ksql_server_url` | string | KSQL Server URL used when utilizing the `ksql` command. |
| `command_prefix` | string | Prefix all commands with another command, i.e. 'docker exec'. |
| `consumer_settings` | ToolSettings | Pass config and default property settings to consumer CLIs. |
| `producer_settings` | ToolSettings | Pass config and default property settings to producer CLIs. |
| key | type | description |
|-------------------------|--------------|---------------------------------------------------------------|
| `bootstrap_servers` | string | Comma-separated `host:port` Kafka brokers to connect to. |
| `zookeeper_connect` | string | Comma-separated `host:port` Zookeeper nodes to connect to. |
| `schema_registry_url` | string | Schema Registry URL used when working with avro schemas. |
| `ksql_server_url` | string | KSQL Server URL used when utilizing the `ksql` command. |
| `command_prefix` | string | Prefix all commands with another command, i.e. 'docker exec'. |
| `consumer_settings` | ToolSettings | Pass config and default property settings to consumer CLIs. |
| `producer_settings` | ToolSettings | Pass config and default property settings to producer CLIs. |
| `admin_client_settings` | ToolSettings | Pass config to admin clients through `--command-config`. |


#### Tool Settings
Expand Down Expand Up @@ -113,6 +114,8 @@ clusters:
config: producer.properties
properties:
key.separator: ","
admin_client_settings:
config: admin.properties
```

### Example Commands
Expand All @@ -131,6 +134,12 @@ For example, if you typed the command `kafka-avro-console-producer --topic test`
docker exec -it kafka-tools kafka-avro-console-producer --broker-list docker:9092 --property schema.registry.url=http://docker:8081 --producer.config producer.properties --property key.separtor=,
```

For example, if you typed the command `kafka-broker-api-versions`:

```bash
docker exec -it kafka-tools kafka-broker-api-versions --bootstrap-server docker:9092 --command-config admin.properties
```

As you can see, you can save a ton of typing time by utilizing `kafka-shell`!


Expand Down
1 change: 1 addition & 0 deletions kafkashell/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
FLAG_BOOTSTRAP_SERVER = "--bootstrap-server"
FLAG_BROKER_LIST = "--broker-list"
FLAG_SCHEMA_REGISTRY_URL = "--property schema.registry.url"
FLAG_COMMAND_CONFIG = "--command-config"
18 changes: 18 additions & 0 deletions kafkashell/data/shell-config.schema
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@
}
},
"additionalProperties": false
},
"admin_client_settings": {
"type": "object",
"title": "Admin Client Settings",
"description": "Settings for configuring admin client within the CLI tools.",
"properties": {
"config": {
"type": "string",
"title": "Admin Client Config",
"description": "Configuration path for properties file passed to --command-config.",
"default": "",
"examples": [
"/tmp/admin.properties"
],
"pattern": "^(.*).properties$"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
Expand Down
37 changes: 26 additions & 11 deletions kafkashell/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,58 +150,64 @@ def handle_kafka_topics_command(self, command):

def handle_kafka_configs_command(self, command):
command += self.handle_zookeeper_flag(command)
command += self.handle_admin_client_settings(command)
return command

def handle_kafka_console_consumer_command(self, command):
command += self.handle_bootstrap_server_flag(command)
command += self.handle_cli_settings("consumer")
command += self.handle_cli_settings(command, "consumer")
return command

def handle_kafka_console_producer_command(self, command):
command += self.handle_broker_list_flag(command)
command += self.handle_cli_settings("producer")
command += self.handle_cli_settings(command, "producer")
return command

def handle_kafka_avro_console_consumer_command(self, command):
command += self.handle_bootstrap_server_flag(command)
command += self.handle_schema_registry_url_property(command)
command += self.handle_cli_settings("consumer")
command += self.handle_cli_settings(command, "consumer")
return command

def handle_kafka_avro_console_producer_command(self, command):
command += self.handle_broker_list_flag(command)
command += self.handle_schema_registry_url_property(command)
command += self.handle_cli_settings("producer")
command += self.handle_cli_settings(command, "producer")
return command

def handle_kafka_verifiable_consumer(self, command):
command += self.handle_broker_list_flag(command)
command += self.handle_config("consumer.config", "consumer")
command += self.handle_config(command, "consumer.config", "consumer")
return command

def handle_kafka_verifiable_producer(self, command):
command += self.handle_broker_list_flag(command)
command += self.handle_config("producer.config", "producer")
command += self.handle_config(command, "producer.config", "producer")
return command

def handle_kafka_consumer_groups_command(self, command):
command += self.handle_bootstrap_server_flag(command)
command += self.handle_admin_client_settings(command)
return command

def handle_kafka_broker_api_versions_command(self, command):
command += self.handle_bootstrap_server_flag(command)
command += self.handle_admin_client_settings(command)
return command

def handle_kafka_delete_records_command(self, command):
command += self.handle_bootstrap_server_flag(command)
command += self.handle_admin_client_settings(command)
return command

def handle_kafka_log_dirs_command(self, command):
command += self.handle_bootstrap_server_flag(command)
command += self.handle_admin_client_settings(command)
return command

def handle_kafka_acls_command(self, command):
command += self.handle_bootstrap_server_flag(command)
command += self.handle_admin_client_settings(command)
return command

def handle_ksql_command(self, command):
Expand Down Expand Up @@ -248,18 +254,27 @@ def handle_ksql_input(self, command):
def handle_zookeeper_shell_input(self, command):
return " " + self.settings.get_cluster_details()["zookeeper_connect"] if len(command.split()) == 1 else ""

def handle_cli_settings(self, settings_type):
def handle_admin_client_settings(self, command):
if "admin_client_settings" in self.settings.get_cluster_details().keys():
return self.handle_config(command, "command-config", "admin_client")
else:
return ""

def handle_cli_settings(self, command, settings_type):
if "{0}_settings".format(settings_type) in self.settings.get_cluster_details().keys():
return "".join([
self.handle_config("{0}.config".format(settings_type), settings_type),
self.handle_config(command, "{0}.config".format(settings_type), settings_type),
self.handle_properties(settings_type)
])
else:
return ""

def handle_config(self, config_prefix, settings_type):
config = self.get_config_from_settings(settings_type)
return " --{0} {1}".format(config_prefix, config) if config is not None else ""
def handle_config(self, command, config_prefix, settings_type):
if "--{}".format(config_prefix) not in command:
config = self.get_config_from_settings(settings_type)
return " --{0} {1}".format(config_prefix, config) if config is not None else ""
else:
return ""

def handle_properties(self, settings_type):
properties = self.get_properties_from_settings(settings_type)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
name="kafka-shell",
version=meta["__version__"],
author="Shawn Seymour",
author_email="shawn@flunk.io",
author_email="shawn@devshawn.com",
description="A supercharged, interactive Kafka shell built on top of the existing Kafka CLI tools.",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
18 changes: 18 additions & 0 deletions tests/data/test-admin-client-settings-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 1
enable:
history: true
save_on_exit: true
auto_complete: true
auto_suggest: true
inline_help: true
fuzzy_search: true
cluster: local
clusters:
local:
bootstrap_servers: localhost:9092
zookeeper_connect: localhost:2181
schema_registry_url: http://localhost:8081
ksql_server_url: http://localhost:8088
command_prefix: ''
admin_client_settings:
config: admin.properties
Loading