Skip to content

feat: add support for command file extensions #19

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
Dec 11, 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
49 changes: 39 additions & 10 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ 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. |
| `admin_client_settings` | ToolSettings | Pass config to admin clients through `--command-config`. |
| 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'. |
| `command_file_extension` | string | Add a file extension such as `sh` to commands. |
| `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 @@ -142,6 +143,34 @@ docker exec -it kafka-tools kafka-broker-api-versions --bootstrap-server docker:

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

### Command File Extension

The file extension for commands such as `kafka-topics` is `null` by default. Depending on how you installed the kafka command-line tools, they may have the extension `sh` or `bat`. They may also be set this way in pre-built docker images.

You can change this, per cluster, by setting the `command_file_extension` property in the cluster config. For example:

```yaml
...
clusters:
local:
bootstrap_servers: localhost:9092
zookeeper_connect: localhost:2181
schema_registry_url: http://localhost:8081
ksql_server_url: http://localhost:8088
command_file_extension: sh
```

If you run `kafka-topics --list` with the above command, the following would be run:

```bash
kafka-topics.sh --list --zookeeper localhost:2181
```

Without the file extension config set, the following would be run:

```bash
kafka-topics --list --zookeeper localhost:2181
```

## Support
If you have a question on how to configure `kafka-shell`, feel free to [open a support issue][support].
Expand Down
11 changes: 11 additions & 0 deletions kafkashell/data/shell-config.schema
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@
],
"pattern": "^(.*)$"
},
"command_file_extension": {
"type": "string",
"title": "Command File Extension",
"description": "Add a file extension such as '.sh' or .bat' to the commands.",
"default": "",
"examples": [
"sh",
"bat"
],
"pattern": "^sh$|^bat$"
},
"consumer_settings": {
"type": "object",
"title": "Consumer Settings",
Expand Down
18 changes: 17 additions & 1 deletion kafkashell/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
from __future__ import print_function
from __future__ import unicode_literals

import os
import sys

import os
from prompt_toolkit import shortcuts

from kafkashell import constants
Expand Down Expand Up @@ -117,12 +117,22 @@ def execute_valid_command(self, command):
else:
final_command = command

final_command = self.handle_file_extension(final_command)

if self.check_for_valid_command_prefix():
command_prefix = self.settings.get_cluster_details()["command_prefix"].strip()
final_command = "{0} {1}".format(command_prefix, final_command)

os.system(final_command)

def handle_file_extension(self, command):
file_extension = self.get_file_extension()
if file_extension is not None:
split_command = command.split(" ")
split_command[0] = "{}.{}".format(split_command[0], file_extension)
return " ".join(split_command)
return command

def execute_cluster_command(self, command):
split_text = command.split(" ")
if len(split_text) > 1:
Expand Down Expand Up @@ -328,6 +338,12 @@ def check_for_valid_command_prefix(self):
and \
len(self.settings.get_cluster_details()["command_prefix"]) > 0

def get_file_extension(self):
try:
return self.settings.get_cluster_details()["command_file_extension"]
except KeyError:
return None

@staticmethod
def wrap_with_spaces(string):
return " {0} ".format(string)
17 changes: 17 additions & 0 deletions tests/data/test-file-extension-bat-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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: ''
command_file_extension: bat
17 changes: 17 additions & 0 deletions tests/data/test-file-extension-sh-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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: ''
command_file_extension: sh
17 changes: 17 additions & 0 deletions tests/data/test-invalid-file-extension-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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: ''
command_file_extension: asdf
42 changes: 42 additions & 0 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,24 @@
(" ksql ", "ksql -- http://localhost:8088"),
]

command_file_extension_sh_test_data = [
("kafka-topics --list", "kafka-topics.sh --list --zookeeper localhost:2181"),
("kafka-console-consumer", "kafka-console-consumer.sh --bootstrap-server localhost:9092"),
(
"kafka-consumer-groups --to-offset 10 --list",
"kafka-consumer-groups.sh --to-offset 10 --list --bootstrap-server localhost:9092"
)
]

command_file_extension_bat_test_data = [
("kafka-topics --list", "kafka-topics.bat --list --zookeeper localhost:2181"),
("kafka-console-consumer", "kafka-console-consumer.bat --bootstrap-server localhost:9092"),
(
"kafka-consumer-groups --to-offset 10 --list",
"kafka-consumer-groups.bat --to-offset 10 --list --bootstrap-server localhost:9092"
)
]

command_prefix_test_data = [
({}, False),
({"command_prefix": ""}, False),
Expand Down Expand Up @@ -704,6 +722,30 @@ def test_executor_command_prefix_none(mock_config_path, mock_os_system, test_inp
mock_os_system.assert_called_once_with(expected)


@mock.patch('os.system')
@mock.patch('kafkashell.config.get_user_config_path')
@pytest.mark.parametrize("test_input,expected", command_file_extension_sh_test_data)
def test_executor_command_file_extension_sh(mock_config_path, mock_os_system, test_input, expected):
mock_config_path.return_value = setup_config_path_for_test("test-file-extension-sh")

executor = kafkashell.executor.Executor(kafkashell.settings.Settings())
executor.execute(test_input)

mock_os_system.assert_called_once_with(expected)


@mock.patch('os.system')
@mock.patch('kafkashell.config.get_user_config_path')
@pytest.mark.parametrize("test_input,expected", command_file_extension_bat_test_data)
def test_executor_command_file_extension_bat(mock_config_path, mock_os_system, test_input, expected):
mock_config_path.return_value = setup_config_path_for_test("test-file-extension-bat")

executor = kafkashell.executor.Executor(kafkashell.settings.Settings())
executor.execute(test_input)

mock_os_system.assert_called_once_with(expected)


@mock.patch('sys.exit')
@mock.patch('kafkashell.settings.Settings.save_settings')
@mock.patch('kafkashell.config.get_user_config_path')
Expand Down
6 changes: 6 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ def test_invalid_configs():
json_value = get_test_config("test-invalid-schema-registry")
message = validate_invalid_schema(json_value, "shell-config")
assert message is not None


def test_invalid_file_extension():
json_value = get_test_config("test-invalid-file-extension")
message = validate_invalid_schema(json_value, "shell-config")
assert message is not None