From e61caf5c46c1d41c63de900bcf2f3ba47bef5874 Mon Sep 17 00:00:00 2001 From: Navidur Rahman Date: Tue, 12 Dec 2023 16:47:48 -0500 Subject: [PATCH] Show sgpt version command line option (#394) --- pyproject.toml | 2 +- sgpt/__init__.py | 2 -- sgpt/__version__.py | 1 + sgpt/app.py | 14 +++++++++++++- sgpt/utils.py | 10 ++++++++++ tests/test_integration.py | 17 +++++++++++++++++ 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 sgpt/__version__.py diff --git a/pyproject.toml b/pyproject.toml index 4da8829..fe2d584 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,7 @@ dev = [ ] [tool.hatch.version] -path = "sgpt/__init__.py" +path = "sgpt/__version__.py" [tool.hatch.build.targets.wheel] only-include = ["sgpt"] diff --git a/sgpt/__init__.py b/sgpt/__init__.py index a2202c0..49fc9a9 100644 --- a/sgpt/__init__.py +++ b/sgpt/__init__.py @@ -1,4 +1,2 @@ from .app import main as main from .app import entry_point as cli # noqa: F401 - -__version__ = "0.9.4" diff --git a/sgpt/__version__.py b/sgpt/__version__.py new file mode 100644 index 0000000..e94731c --- /dev/null +++ b/sgpt/__version__.py @@ -0,0 +1 @@ +__version__ = "0.9.4" diff --git a/sgpt/app.py b/sgpt/app.py index 0c97bda..21dfe75 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -11,7 +11,12 @@ from sgpt.handlers.chat_handler import ChatHandler from sgpt.handlers.default_handler import DefaultHandler from sgpt.handlers.repl_handler import ReplHandler from sgpt.role import DefaultRoles, SystemRole -from sgpt.utils import get_edited_prompt, install_shell_integration, run_command +from sgpt.utils import ( + get_edited_prompt, + get_sgpt_version, + install_shell_integration, + run_command, +) def main( @@ -63,6 +68,13 @@ def main( True, help="Cache completion results.", ), + version: bool = typer.Option( + False, + "--version", + "-v", + help="Show version.", + callback=get_sgpt_version, + ), chat: str = typer.Option( None, help="Follow conversation with id, " 'use "temp" for quick session.', diff --git a/sgpt/utils.py b/sgpt/utils.py index f2e92ee..6dbb0b2 100644 --- a/sgpt/utils.py +++ b/sgpt/utils.py @@ -7,6 +7,8 @@ from typing import Any, Callable import typer from click import BadParameter +from sgpt.__version__ import __version__ + def get_edited_prompt() -> str: """ @@ -74,3 +76,11 @@ def install_shell_integration(*_args: Any) -> None: else: url = "https://raw.githubusercontent.com/TheR1D/shell_gpt/shell-integrations/install.sh" os.system(f'sh -c "$(curl -fsSL {url})"') + + +@option_callback +def get_sgpt_version(*_args: Any) -> None: + """ + Displays the current installed version of ShellGPT + """ + typer.echo(f"ShellGPT {__version__}") diff --git a/tests/test_integration.py b/tests/test_integration.py index 37b8fc2..bfb05ca 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -19,6 +19,7 @@ from uuid import uuid4 import typer from typer.testing import CliRunner +from sgpt.__version__ import __version__ from sgpt.app import main from sgpt.config import cfg from sgpt.handlers.handler import Handler @@ -454,3 +455,19 @@ class TestShellGpt(TestCase): # Can't really test it since stdin in disable for --shell flag. # for word in ("prints", "hello", "console"): # assert word in result.stdout + + def test_version(self): + dict_arguments = { + "prompt": "", + "--version": True, + } + result = runner.invoke(app, self.get_arguments(**dict_arguments), input="d\n") + assert __version__ in result.stdout + + dict_arguments = { + "prompt": "", + "-v": True, + } + + result = runner.invoke(app, self.get_arguments(**dict_arguments), input="d\n") + assert __version__ in result.stdout