Skip to content

Commit

Permalink
Merge pull request #703 from amal-thundiyil/fix
Browse files Browse the repository at this point in the history
fix(config): move `--install-tab-completion` to its own command
  • Loading branch information
craigds committed Aug 24, 2022
2 parents bde1deb + 6deab6e commit 2ca3b0a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _When adding new entries to the changelog, please include issue/PR numbers where

## 0.11.6 (UNRELEASED)

- Install tab completion using `kart install tab-completion` instead of `kart config --install-tab-completion`. [#701](https://github.com/koordinates/kart/issues/701)
- "Primary key conflicts" - conflicts caused by reusing a primary key which is already assigned to a feature outside the spatial filter - are renamed to "spatial filter conflicts" for future proofing with other dataset types. Consequently, commit option `--allow-pk-conflicts` is renamed to `--allow-spatial-filter-conflicts`.
- Bugfix: directing geojson diff output to the console didn't work in a multi-dataset repo, even if only one dataset had changed. [#702](https://github.com/koordinates/kart/issues/702)

Expand Down
9 changes: 1 addition & 8 deletions kart/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
)
from .context import Context
from .exec import run_and_wait
from kart.completion import Shells, install_callback

MODULE_COMMANDS = {
"annotations.cli": {"build-annotations"},
Expand Down Expand Up @@ -51,6 +50,7 @@
"upgrade": {"upgrade"},
"tabular.import_": {"import"},
"point_cloud.import_": {"point-cloud-import"},
"install": {"install"},
}

# These commands aren't valid Python symbols, even when we change dash to underscore.
Expand Down Expand Up @@ -258,13 +258,6 @@ def reflog(ctx, args):

@cli.command(context_settings=dict(ignore_unknown_options=True))
@click.pass_context
@click.option(
"--install-tab-completion",
type=click.Choice([s.value for s in Shells] + ["auto"]),
callback=install_callback,
expose_value=False,
help="Install tab completion for the specific or current shell",
)
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
def config(ctx, args):
"""Get and set repository or global options"""
Expand Down
14 changes: 1 addition & 13 deletions kart/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path
return path_obj


def install(
def install_tab_completion(
shell: Optional[str] = None,
prog_name: Optional[str] = None,
complete_var: Optional[str] = None,
Expand Down Expand Up @@ -213,18 +213,6 @@ def install(
raise click.exceptions.Exit(1)


def install_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any:
if not value or ctx.resilient_parsing:
return value
if value == "auto":
shell, path = install()
else:
shell, path = install(shell=value)
click.secho(f"{shell} completion installed in {path}", fg="green")
click.echo("Completion will take effect once you restart the terminal")
sys.exit(0)


@click.shell_completion.add_completion_class
class PowerShellComplete(click.shell_completion.ShellComplete):
name = Shells.powershell.value
Expand Down
29 changes: 29 additions & 0 deletions kart/install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import click

from kart.completion import Shells, install_tab_completion
from kart.cli_util import add_help_subcommand, KartGroup


@add_help_subcommand
@click.group(cls=KartGroup)
@click.pass_context
def install(ctx, **kwargs):
"""Install tools and add-ons"""


@install.command()
@click.option(
"--shell",
nargs=1,
type=click.Choice([s.value for s in Shells] + ["auto"]),
default="auto",
help="Select a shell to install tab completion for. Defaults to auto for auto-detecting shell.",
)
def tab_completion(shell: str):
"""Install tab completion for the specific or current shell"""
if shell == "auto":
shell, path = install_tab_completion()
else:
shell, path = install_tab_completion(shell=shell)
click.secho(f"{shell} completion installed in {path}", fg="green")
click.echo("Completion will take effect once you restart the terminal")
13 changes: 4 additions & 9 deletions tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
SHOW_OUTPUT_FORMATS = DIFF_OUTPUT_FORMATS


def test_completion_install_no_shell(cli_runner):
r = cli_runner.invoke(["config", "--install-tab-completion"])
assert "Error: Option '--install-tab-completion' requires an argument" in r.stderr


def test_completion_install_bash(cli_runner):
bash_completion_path = Path.home() / ".bashrc"
bash_profile = Path.home() / ".bash_profile"
Expand All @@ -27,7 +22,7 @@ def test_completion_install_bash(cli_runner):
text = ""
if bash_completion_path.is_file():
text = bash_completion_path.read_text()
r = cli_runner.invoke(["config", "--install-tab-completion", "bash"])
r = cli_runner.invoke(["install", "tab-completion", "--shell", "bash"])
new_text = bash_completion_path.read_text()
bash_completion_path.write_text(text)
install_source = os.path.join(".bash_completions", "cli.sh")
Expand All @@ -49,7 +44,7 @@ def test_completion_install_zsh(cli_runner):
completion_path.write_text('echo "custom .zshrc"')
if completion_path.is_file():
text = completion_path.read_text()
r = cli_runner.invoke(["config", "--install-tab-completion", "zsh"])
r = cli_runner.invoke(["install", "tab-completion", "--shell", "zsh"])
new_text = completion_path.read_text()
completion_path.write_text(text)
zfunc_fragment = "fpath+=~/.zfunc"
Expand All @@ -67,7 +62,7 @@ def test_completion_install_fish(cli_runner):
completion_path: Path = Path.home() / os.path.join(
".config", "fish", "completions", "cli.fish"
)
r = cli_runner.invoke(["config", "--install-tab-completion", "fish"])
r = cli_runner.invoke(["install", "tab-completion", "--shell", "fish"])
new_text = completion_path.read_text()
completion_path.unlink()
assert "complete --no-files --command cli" in new_text
Expand Down Expand Up @@ -154,7 +149,7 @@ def test_completion_install_powershell(cli_runner, mocker):
["pwsh"], returncode=0, stdout=str(completion_path)
),
)
result = cli_runner.invoke(["config", "--install-tab-completion", "auto"])
result = cli_runner.invoke(["install", "tab-completion"])
install_script = "Register-ArgumentCompleter -Native -CommandName mocked-typer-testing-app -ScriptBlock $scriptblock"
parent: Path = completion_path.parent
parent.mkdir(parents=True, exist_ok=True)
Expand Down

0 comments on commit 2ca3b0a

Please sign in to comment.