Skip to content

Commit

Permalink
cli: add install-completions subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 authored and doronz88 committed Dec 22, 2024
1 parent 2cb87a7 commit 89cbc4e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ cd pymobiledevice3
python3 -m pip install -U -e .
```

You can also install auto-completion for all available sub-commands by adding the following into your `~/.zshrc`:
You can also install auto-completion for all available sub-commands by running the following command:

```shell
# python-click<8.0
eval "$(_PYMOBILEDEVICE3_COMPLETE=source_zsh pymobiledevice3)"
# python-click>=8.0
eval "$(_PYMOBILEDEVICE3_COMPLETE=zsh_source pymobiledevice3)"
pymobiledevice3 install-completions
```

If you're not a macOS user:
Expand Down
1 change: 1 addition & 0 deletions pymobiledevice3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
'usbmux': 'usbmux',
'webinspector': 'webinspector',
'version': 'version',
'install-completions': 'completions',
}


Expand Down
52 changes: 52 additions & 0 deletions pymobiledevice3/cli/completions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import logging
from pathlib import Path

import click
from plumbum import CommandNotFound, local

logger = logging.getLogger(__name__)

COMPLETERS_PATH = [
('bash_source', Path('/opt/homebrew/share/bash-completion/completions')),
('bash_source', Path('/usr/local/share/bash-completion/completions')),
('bash_source', Path('/usr/share/bash-completion/completions'))
]

ZSH_COMPLETERS_PATH = []


@click.group()
def cli() -> None:
pass


@cli.command()
def install_completions() -> None:
""" Install shell completions for the pymobiledevice3 command """
try:
pymobiledevice3 = local['pymobiledevice3']
except CommandNotFound:
logger.error('pymobiledevice3 main binary could not be found in your path.')
return
paths_written = 0
for envvar_value, path in COMPLETERS_PATH:
if not path.exists():
continue
try:
with local.env(_PYMOBILEDEVICE3_COMPLETE=envvar_value):
logger.info(f'Installing shell completions in: {path}')
(path / 'pymobiledevice3').write_text(pymobiledevice3())
paths_written += 1
except PermissionError:
logger.warning(
f'Got "PermissionError" trying to write into: {path}. '
'Please retry as root or change the directory permissions'
)

if paths_written == 0:
logger.error(
'No shell completions path could be found. Please consider submitting a pull request or creating an issue '
'detailing your platform and the PATH that should be added.\n'
'\n'
'https://github.com/doronz88/pymobiledevice3/issues/new?assignees=&labels=&projects=&template=bug_report.md&title=' # noqa: E501
)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ aiofiles
prompt_toolkit
sslpsk-pmd3>=1.0.3;python_version<'3.13'
python-pcapng>=2.1.1
plumbum

0 comments on commit 89cbc4e

Please sign in to comment.