diff --git a/oks_cli/main.py b/oks_cli/main.py index 6cd72d7..c67f295 100755 --- a/oks_cli/main.py +++ b/oks_cli/main.py @@ -82,7 +82,7 @@ def version(): print(importlib.metadata.version(__package__)) @cli.command("install-completion", help="Install shell completion scripts.") -@click.option('--type', required=True, help="Shell, supported [bash,zsh]") +@click.option('--type', help="Shell, supported [bash,zsh]") def install_completion(type): """Install shell completion scripts for the CLI.""" install_completions(type) diff --git a/oks_cli/utils.py b/oks_cli/utils.py index a8fdebe..b7cb2f0 100644 --- a/oks_cli/utils.py +++ b/oks_cli/utils.py @@ -801,6 +801,17 @@ def find_shell_profile(home, shell_type): def install_completions(shell_type): """Install shell completion scripts for bash or zsh.""" home = os.path.expanduser('~') + + if shell_type is None: + try: + shell_pid = os.getppid() + result = subprocess.run(['ps', '-p', str(shell_pid), '-o', 'comm='], capture_output=True, text=True) + shell_name = result.stdout.strip() + + shell_type = os.path.basename(shell_name).lstrip('-') + except subprocess.SubProcessError: + click.echo("Failed to determine shell type, please specify it by --type") + completion_dir = os.path.join(home, ".oks_cli", "completions") os.makedirs(completion_dir, exist_ok=True) diff --git a/tests/test_install_completion.py b/tests/test_install_completion.py deleted file mode 100644 index 327e746..0000000 --- a/tests/test_install_completion.py +++ /dev/null @@ -1,24 +0,0 @@ -from click.testing import CliRunner -from oks_cli.main import cli -# from unittest.mock import patch, MagicMock - -def test_install_completion_command_type_required(): - """ Test --type option is required """ - runner = CliRunner() - result = runner.invoke(cli, ["install-completion"]) - assert result.exit_code == 2 - assert "Missing option '--type'" in result.output - -def test_install_completion_command_wrong_type(): - """ Test --type supported values """ - runner = CliRunner() - result = runner.invoke(cli, ["install-completion", "--type", "fake-shell"]) - assert result.exit_code == 2 - assert "Shell completions for fake-shell are not implemented" in result.output - -def test_install_completion_command(): - """ Test working shell """ - runner = CliRunner() - result = runner.invoke(cli, ["install-completion", "--type", "bash"]) - assert result.exit_code == 0 - assert "Autocompletion installed for bash" in result.output \ No newline at end of file