Repository navigation
chore: add interface-first plugin cookiecutter template #2862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,21 @@ | ||
| # Copyright (c) 2024 Snowflake Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # This cookiecutter template is not part of the snowflake-cli test suite. Its | ||
| # files contain Jinja placeholders (`{{ cookiecutter.* }}`) and are not valid | ||
| # Python, so the repo-wide pytest runs that collect from the project root | ||
| # (e.g. `pytest -m performance`, `pytest -m e2e`) would fail trying to import | ||
| # them. Ignore everything under this directory during collection; the generated | ||
| # project ships its own valid tests. | ||
| collect_ignore_glob = ["*"] |
This file contains hidden or 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,13 @@ | ||
| { | ||
| "plugin_name": "my-snow-plugin", | ||
| "plugin_module": "{{ cookiecutter.plugin_name | replace('-', '_') }}", | ||
| "package_namespace": "snowflakecli_plugins", | ||
| "cli_command_name": "{{ cookiecutter.plugin_name }}", | ||
| "cli_parent_path": "", | ||
| "plugin_description": "A Snowflake CLI plugin.", | ||
| "author_name": "Your Name", | ||
| "author_email": "you@example.com", | ||
| "requires_connection": true, | ||
| "command_type": ["group", "single"], | ||
| "python_requires": ">=3.10" | ||
| } | ||
This file contains hidden or 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,53 @@ | ||
| <!-- | ||
| Copyright (c) 2024 Snowflake Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
|
|
||
| # {{ cookiecutter.plugin_name }} | ||
|
|
||
| {{ cookiecutter.plugin_description }} | ||
|
|
||
| ## Development | ||
|
|
||
| ### Install in development mode | ||
|
|
||
| ```bash | ||
| pip install -e . | ||
| ``` | ||
|
|
||
| ### Enable the plugin | ||
|
|
||
| ```bash | ||
| snow plugin enable {{ cookiecutter.plugin_module }} | ||
| ``` | ||
|
|
||
| ### Run tests | ||
|
|
||
| ```bash | ||
| pytest | ||
| ``` | ||
|
|
||
| ## Plugin Structure | ||
|
|
||
| | File | Purpose | | ||
| |------|---------| | ||
| | `interface.py` | Command surface definition (spec + handler ABC) -- reviewed in Phase 1 | | ||
| | `handler.py` | Business logic implementation -- reviewed in Phase 2 | | ||
| | `plugin_spec.py` | Wires interface + handler into Snowflake CLI | | ||
|
|
||
| ## Contributing | ||
|
|
||
| 1. Modify `interface.py` to define your command surface (Phase 1 PR) | ||
| 2. After approval, implement handlers in `handler.py` (Phase 2 PR) | ||
| 3. Run `pytest` to validate the interface-handler contract |
38 changes: 38 additions & 0 deletions
38
plugin-template/{{cookiecutter.plugin_name}}/pyproject.toml
This file contains hidden or 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 @@ | ||
| # Copyright (c) 2024 Snowflake Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [project] | ||
| name = "{{ cookiecutter.plugin_name }}" | ||
| version = "0.1.0" | ||
| description = "{{ cookiecutter.plugin_description }}" | ||
| requires-python = "{{ cookiecutter.python_requires }}" | ||
| authors = [ | ||
| { name = "{{ cookiecutter.author_name }}", email = "{{ cookiecutter.author_email }}" }, | ||
| ] | ||
| dependencies = [ | ||
| "snowflake-cli", | ||
| ] | ||
|
|
||
| [project.entry-points."snowflake.cli.plugin.command"] | ||
| {{ cookiecutter.plugin_module }} = "{{ cookiecutter.package_namespace }}.{{ cookiecutter.plugin_module }}.plugin_spec" | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["src/{{ cookiecutter.package_namespace }}"] | ||
|
|
||
| [tool.pytest.ini_options] | ||
| testpaths = ["tests"] |
13 changes: 13 additions & 0 deletions
13
...-template/{{cookiecutter.plugin_name}}/src/{{cookiecutter.package_namespace}}/__init__.py
This file contains hidden or 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,13 @@ | ||
| # Copyright (c) 2024 Snowflake Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
13 changes: 13 additions & 0 deletions
13
..._name}}/src/{{cookiecutter.package_namespace}}/{{cookiecutter.plugin_module}}/__init__.py
This file contains hidden or 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,13 @@ | ||
| # Copyright (c) 2024 Snowflake Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
43 changes: 43 additions & 0 deletions
43
...n_name}}/src/{{cookiecutter.package_namespace}}/{{cookiecutter.plugin_module}}/handler.py
This file contains hidden or 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,43 @@ | ||
| # Copyright (c) 2024 Snowflake Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """{{ cookiecutter.plugin_description }} -- Implementation. | ||
|
|
||
| This file contains the concrete handler implementing the interface | ||
| defined in ``interface.py``. Submit this for review (Phase 2) after | ||
| the interface has been approved. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from snowflake.cli.api.output.types import CommandResult, MessageResult | ||
|
|
||
| from {{ cookiecutter.package_namespace }}.{{ cookiecutter.plugin_module }}.interface import ( | ||
| {{ cookiecutter.plugin_module | replace('_', ' ') | title | replace(' ', '') }}Handler, | ||
| ) | ||
|
|
||
|
|
||
| class {{ cookiecutter.plugin_module | replace('_', ' ') | title | replace(' ', '') }}HandlerImpl( | ||
| {{ cookiecutter.plugin_module | replace('_', ' ') | title | replace(' ', '') }}Handler, | ||
| ): | ||
| {% if cookiecutter.command_type == "group" %} | ||
| def hello(self, name: str) -> CommandResult: | ||
| return MessageResult(f"Hello, {name}!") | ||
|
|
||
| def status(self) -> CommandResult: | ||
| return MessageResult("Plugin is running.") | ||
| {%- else %} | ||
| def run(self, name: str) -> CommandResult: | ||
| return MessageResult(f"Hello, {name}!") | ||
| {%- endif %} |
130 changes: 130 additions & 0 deletions
130
...name}}/src/{{cookiecutter.package_namespace}}/{{cookiecutter.plugin_module}}/interface.py
This file contains hidden or 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,130 @@ | ||
| # Copyright (c) 2024 Snowflake Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """{{ cookiecutter.plugin_description }} | ||
|
|
||
| This file defines the command surface for ``snow {{ cookiecutter.cli_command_name }}``. | ||
| Submit this file for review (Phase 1) before writing the implementation. | ||
|
|
||
| Commands | ||
| -------- | ||
| {%- if cookiecutter.command_type == "group" %} | ||
| - ``snow {{ cookiecutter.cli_command_name }} hello <name>`` -- Say hello. | ||
| - ``snow {{ cookiecutter.cli_command_name }} status`` -- Show status. | ||
| {%- else %} | ||
| - ``snow {{ cookiecutter.cli_command_name }} <name>`` -- Run the command. | ||
| {%- endif %} | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import abstractmethod | ||
|
|
||
| from snowflake.cli.api.output.types import CommandResult | ||
| from snowflake.cli.api.plugins.command.interface import ( | ||
| CommandDef, | ||
| {%- if cookiecutter.command_type == "group" %} | ||
| CommandGroupSpec, | ||
| {%- else %} | ||
| SingleCommandSpec, | ||
| {%- endif %} | ||
| CommandHandler, | ||
| ParamDef, | ||
| ParamKind, | ||
| REQUIRED, | ||
|
sfc-gh-olorek marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Command surface (reviewable spec) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| {% if cookiecutter.command_type == "group" -%} | ||
| PLUGIN_SPEC = CommandGroupSpec( | ||
| name="{{ cookiecutter.cli_command_name }}", | ||
| help="{{ cookiecutter.plugin_description }}", | ||
| parent_path=({{ cookiecutter.cli_parent_path | default("", true) }}), | ||
|
sfc-gh-olorek marked this conversation as resolved.
Outdated
|
||
| commands=( | ||
| CommandDef( | ||
| name="hello", | ||
| help="Say hello to someone.", | ||
| handler_method="hello", | ||
| requires_connection={{ cookiecutter.requires_connection }}, | ||
| params=( | ||
| ParamDef( | ||
| name="name", | ||
| type=str, | ||
| kind=ParamKind.ARGUMENT, | ||
| help="Name to greet.", | ||
| ), | ||
| ), | ||
| output_type="MessageResult", | ||
| ), | ||
| CommandDef( | ||
| name="status", | ||
| help="Show plugin status.", | ||
| handler_method="status", | ||
| requires_connection={{ cookiecutter.requires_connection }}, | ||
| output_type="MessageResult", | ||
| ), | ||
| ), | ||
| ) | ||
| {%- else -%} | ||
| PLUGIN_SPEC = SingleCommandSpec( | ||
| parent_path=({{ cookiecutter.cli_parent_path | default("", true) }}), | ||
|
sfc-gh-olorek marked this conversation as resolved.
Outdated
|
||
| command=CommandDef( | ||
| name="{{ cookiecutter.cli_command_name }}", | ||
| help="{{ cookiecutter.plugin_description }}", | ||
| handler_method="run", | ||
| requires_connection={{ cookiecutter.requires_connection }}, | ||
| params=( | ||
| ParamDef( | ||
| name="name", | ||
| type=str, | ||
| kind=ParamKind.ARGUMENT, | ||
| help="Name argument.", | ||
| ), | ||
| ), | ||
| output_type="MessageResult", | ||
| ), | ||
| ) | ||
| {%- endif %} | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Handler contract (ABC) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class {{ cookiecutter.plugin_module | replace('_', ' ') | title | replace(' ', '') }}Handler(CommandHandler): | ||
| """Handler contract for {{ cookiecutter.cli_command_name }} commands. | ||
|
|
||
| Each abstract method corresponds to a ``CommandDef`` above via | ||
| ``handler_method``. | ||
| """ | ||
| {% if cookiecutter.command_type == "group" %} | ||
| @abstractmethod | ||
| def hello(self, name: str) -> CommandResult: | ||
| """Say hello.""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def status(self) -> CommandResult: | ||
| """Show status.""" | ||
| ... | ||
| {% else %} | ||
| @abstractmethod | ||
| def run(self, name: str) -> CommandResult: | ||
| """Run the command.""" | ||
| ... | ||
| {% endif %} | ||
26 changes: 26 additions & 0 deletions
26
...me}}/src/{{cookiecutter.package_namespace}}/{{cookiecutter.plugin_module}}/plugin_spec.py
This file contains hidden or 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,26 @@ | ||
| # Copyright (c) 2024 Snowflake Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from snowflake.cli.api.plugins.command import plugin_hook_impl | ||
| from snowflake.cli.api.plugins.command.bridge import build_command_spec | ||
|
|
||
| from {{ cookiecutter.package_namespace }}.{{ cookiecutter.plugin_module }}.handler import ( | ||
| {{ cookiecutter.plugin_module | replace('_', ' ') | title | replace(' ', '') }}HandlerImpl, | ||
| ) | ||
| from {{ cookiecutter.package_namespace }}.{{ cookiecutter.plugin_module }}.interface import PLUGIN_SPEC | ||
|
|
||
|
|
||
| @plugin_hook_impl | ||
| def command_spec(): | ||
| return build_command_spec(PLUGIN_SPEC, {{ cookiecutter.plugin_module | replace('_', ' ') | title | replace(' ', '') }}HandlerImpl()) |
13 changes: 13 additions & 0 deletions
13
plugin-template/{{cookiecutter.plugin_name}}/tests/__init__.py
This file contains hidden or 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,13 @@ | ||
| # Copyright (c) 2024 Snowflake Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.