Skip to content

Commit

Permalink
Merge pull request #1318 from doronz88/feature/shell-completions
Browse files Browse the repository at this point in the history
cli: add `install-completions` subcommand
  • Loading branch information
doronz88 authored Dec 23, 2024
2 parents 03fa143 + d22845b commit c1581ab
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Installation](#installation)
- [OpenSSL libraries](#openssl-libraries)
- [libusb dependency](#libusb-dependency)
- [Autocompletions](#autocompletions)
- [Usage](#usage)
- [Working with developer tools (iOS \>= 17.0)](#working-with-developer-tools-ios--170)
- [Commonly used actions](#commonly-used-actions)
Expand Down Expand Up @@ -70,15 +71,6 @@ 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`:

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

If you're not a macOS user:

- If you're using a Windows workstation, you'll need to install and run
Expand Down Expand Up @@ -147,6 +139,21 @@ Following libusb website to download latest release binaries:

<https://libusb.info/>

### Autocompletions

You can also install auto-completion for all available sub-commands by running the following command:

```shell
# Install bash completions
pymobiledevice3 install-completions
```

Supported shells include:

- Fish
- Bash
- Zsh

## Usage

The CLI subcommands are divided roughly by the protocol layer used for interacting in the device. For example, all
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
48 changes: 48 additions & 0 deletions pymobiledevice3/cli/completions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import logging
from collections import namedtuple
from pathlib import Path

import click
from plumbum import CommandNotFound, local

logger = logging.getLogger(__name__)

ShellCompletion = namedtuple('ShellCompletion', ['source', 'rc', 'path'])

COMPLETIONS = [
ShellCompletion('zsh_source', Path('~/.zshrc').expanduser(), Path('~/.pymobiledevice3.zsh').expanduser()),
ShellCompletion('bash_source', Path('~/.bashrc').expanduser(), Path('~/.pymobiledevice3.bash').expanduser()),
ShellCompletion('fish_source', None, Path('~/.config/fish/completions/pymobiledevice3.fish').expanduser()),
]


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


@cli.command()
def install_completions() -> None:
"""
Install shell completions for the pymobiledevice3 command
If supplying an explicit shell script to write, install it there, otherwise install globally.
"""
try:
pymobiledevice3 = local['pymobiledevice3']
except CommandNotFound:
logger.error('pymobiledevice3 main binary could not be found in your path.')
return

for completion in COMPLETIONS:
with local.env(_PYMOBILEDEVICE3_COMPLETE=completion.source):
if not completion.path.parent.exists():
# fish is not installed, skip
continue
logger.info(f'Writing shell completions to: {completion.path}')
completion.path.write_text(pymobiledevice3())
line = f'source {completion.path}'
if line in completion.rc.read_text():
continue
logger.info(f'Adding source line to {completion.rc}')
completion.rc.write_text(f'{completion.rc.read_text()}\n{line}')
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 c1581ab

Please sign in to comment.