-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use inheritance to construct plugin CLI (#936)
* refactor: Use inheritance to construct plugin CLI * Update descriptor to generalize plugin CLIs * Add docs * Rename file --------- Co-authored-by: Ken Payne <[email protected]>
- Loading branch information
1 parent
e46a5e0
commit 196f05a
Showing
10 changed files
with
392 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Custom CLIs | ||
|
||
## Overview | ||
|
||
By default, packages created with the Singer SDK will have a single command, e.g. `tap-my-source`, which will run the application in a Singer-compatible way. However, you may want to add additional commands to your package. For example, you may want to add a command to initialize the database or platform with certain attributes required by the application to run properly. | ||
|
||
## Adding a custom command | ||
|
||
To add a custom command, you will need to add a new method to your plugin class that returns an instance of [`click.Command`](https://click.palletsprojects.com/en/8.1.x/api/#commands) (or a subclass of it) and decorate it with the `singer_sdk.cli.plugin_cli` decorator. Then you will need to add the command to the `[tool.poetry.scripts]` section of your `pyproject.toml` file. | ||
|
||
```python | ||
# tap_shortcut/tap.py | ||
|
||
class ShortcutTap(Tap): | ||
"""Shortcut tap class.""" | ||
|
||
@plugin_cli | ||
def update_schema(cls) -> click.Command: | ||
"""Update the OpenAPI schema for this tap.""" | ||
@click.command() | ||
def update(): | ||
response = requests.get( | ||
"https://developer.shortcut.com/api/rest/v3/shortcut.swagger.json", | ||
timeout=5, | ||
) | ||
with Path("tap_shortcut/openapi.json").open("w") as f: | ||
f.write(response.text) | ||
|
||
return update | ||
``` | ||
|
||
```toml | ||
# pyproject.toml | ||
|
||
[tool.poetry.scripts] | ||
tap-shortcut = "tap_shortcut.tap:ShortcutTap.cli" | ||
tap-shortcut-update-schema = "tap_shortcut.tap:ShortcutTap.update_schema" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,35 @@ | ||
"""Helpers for the tap, target and mapper CLIs.""" | ||
|
||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
if t.TYPE_CHECKING: | ||
import click | ||
|
||
_T = t.TypeVar("_T") | ||
|
||
|
||
class plugin_cli: # noqa: N801 | ||
"""Decorator to create a plugin CLI.""" | ||
|
||
def __init__(self, method: t.Callable[..., click.Command]) -> None: | ||
"""Create a new plugin CLI. | ||
Args: | ||
method: The method to call to get the command. | ||
""" | ||
self.method = method | ||
self.name: str | None = None | ||
|
||
def __get__(self, instance: _T, owner: type[_T]) -> click.Command: | ||
"""Get the command. | ||
Args: | ||
instance: The instance of the plugin. | ||
owner: The plugin class. | ||
Returns: | ||
The CLI entrypoint. | ||
""" | ||
return self.method(owner) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.