Skip to content

Commit

Permalink
feat(cli): migrate to cyclopts
Browse files Browse the repository at this point in the history
  • Loading branch information
pythoninja committed Mar 16, 2024
1 parent 7eb275d commit cca5053
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 67 deletions.
176 changes: 136 additions & 40 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sshgen = "sshgen.cli:app"
[tool.poetry.dependencies]
python = "^3.12"
ruamel-yaml = ">=0.17.31,<0.19.0"
typer = "^0.9.0"
cyclopts = "^2.4.2"

[tool.poetry.group.dev.dependencies]
ruff = "*"
Expand Down
43 changes: 17 additions & 26 deletions sshgen/cli.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#!/usr/bin/env python
from typing import Annotated, Optional
from typing import Annotated

import typer
from cyclopts import App, Group
from cyclopts import Parameter

from sshgen import __app_name__, __version__
from sshgen.logger import init_logger
from sshgen.models.loglevel import LogLevel
from sshgen.utils.app import AppUtils
from sshgen.utils.file import FileUtils

app = typer.Typer(no_args_is_help=True)
app = App(version=f"{__app_name__} v{__version__}", version_flags=["--version"], help_flags=["--help"])
app.meta.group_parameters = Group("Debug Output")


@app.command("generate")
@app.command(name="generate")
def generate_hosts_file(
hosts_file: Annotated[str, typer.Option("--hosts-file", "-h")] = "./hosts.yml",
output: Annotated[str, typer.Option("--output", "-o")] = "./config",
hosts_file: Annotated[str, Parameter(name=["--hosts-file", "-h"], allow_leading_hyphen=True)] = "./hosts.yml",
output: Annotated[str, Parameter(["--output", "-o"])] = "./config",
) -> None:
"""
Command to generate SSH configuration file.
By default, it uses file hosts.yml placed in your working directory and outputs to the file named "config".
Example usage: sshgen generate -o my_ssh_config
"""

Expand All @@ -30,37 +34,24 @@ def generate_hosts_file(
sshgen.generate_ssh_config()


def _version_callback(value: bool) -> None:
if value:
typer.echo(f"{__app_name__} v{__version__}")
raise typer.Exit()


# noinspection PyUnusedLocal
@app.callback()
@app.meta.default()
def main(
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)],
verbose: Annotated[
Optional[bool],
typer.Option(
"--verbose",
is_eager=True,
envvar=["SSHGEN_VERBOSE", "SSHGEN_DEBUG"],
help="Switch log level to DEBUG, default is INFO.",
),
bool, Parameter(name="--verbose", show_default=False, negative="", env_var=["SSHGEN_VERBOSE", "SSHGEN_DEBUG"])
] = False,
version: Annotated[
Optional[bool],
typer.Option("-v", "--version", is_eager=True, callback=_version_callback),
] = None,
) -> None:
"""
sshgen generates SSH configuration file based on an Ansible hosts file.
"""

if verbose:
init_logger(level=LogLevel.DEBUG)
else:
init_logger(level=LogLevel.INFO)

app(tokens)


if __name__ == "__main__":
app()
app.meta()

0 comments on commit cca5053

Please sign in to comment.