Skip to content
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

feat: add pretty custom version message #25

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/armonik_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
import rich_click as click

from armonik_cli import commands, __version__
from armonik import __version__ as api_version
from rich.text import Text
from rich.style import Style

from armonik_cli import commands, __version__ as cli_version
from armonik_cli.core import console


ascii_cli_name = """
░░░░░╗ ░░░░░░╗ ░░░╗ ░░░╗ ░░░░░░╗ ░░░╗ ░░╗░░╗░░╗ ░░╗
░░╔══░░╗░░╔══░░╗░░░░╗ ░░░░║░░╔═══░░╗░░░░╗ ░░║░░║░░║ ░░╔╝
▒▒▒▒▒▒▒║▒▒▒▒▒▒╔╝▒▒╔▒▒▒▒╔▒▒║▒▒║ ▒▒║▒▒╔▒▒╗ ▒▒║▒▒║▒▒▒▒▒╔╝
▓▓╔══▓▓║▓▓╔══▓▓╗▓▓║╚▓▓╔╝▓▓║▓▓║ ▓▓║▓▓║╚▓▓╗▓▓║▓▓║▓▓╔═▓▓╗
██║ ██║██║ ██║██║ ╚═╝ ██║╚██████╔╝██║ ╚████║██║██║ ██╗
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝
"""


def version_callback(ctx: click.Context, param: click.Parameter, value: bool) -> None:
"""Callback function for the `--version` option.
Displays the ASCII logo of the CLI, along with the current CLI and API versions,
and exits the program.

Args:
ctx: The current Click context.
param: The parameter associated with the callback.
value: The flag value indicating if the `--version` option was set.

Returns:
None: This function does not return any value, but it exits the program after printing the version information.
"""
if not value or ctx.resilient_parsing:
return

color = "#FE5100"
console.print(Text(ascii_cli_name, style=f"bold {color}"))
console.print(Text("-" * (console.width // 2), style=Style(color=color)))
console.print(
Text("CLI version: ", style="bold white").append(cli_version, style=f"bold {color}")
)
console.print(
Text("API version: ", style="bold white").append(api_version, style=f"bold {color}")
)
ctx.exit()


@click.group(name="armonik")
@click.version_option(version=__version__, prog_name="armonik")
@click.option(
"--version",
"-V",
is_flag=True,
expose_value=False,
is_eager=True,
help="Show the version and exit.",
callback=version_callback,
)
def cli() -> None:
"""
ArmoniK CLI is a tool to monitor and manage ArmoniK clusters.
Expand Down
11 changes: 8 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import pytest

from conftest import run_cmd_and_assert_exit_code


def test_armonik_version():
result = run_cmd_and_assert_exit_code("--version")
assert result.output.startswith("armonik, version ")
@pytest.mark.parametrize("flag", ["-V", "--version"])
def test_armonik_version(flag):
result = run_cmd_and_assert_exit_code(flag)
output = result.output.split("\n")[:-1]
assert output[-1].startswith("API version: ")
assert output[-2].startswith("CLI version: ")


def test_armonik_help():
Expand Down