diff --git a/frg/cli.py b/frg/cli.py index cf3aa0e..27dad69 100644 --- a/frg/cli.py +++ b/frg/cli.py @@ -2,26 +2,45 @@ import click import pydantic import frg.forgejo.browser as forgejo_browser - from frg.context import get_git_context, GitContext from frg.configuration import get_configuration, Config +import logging + +logger = logging.getLogger(__name__) + class CliContext(pydantic.BaseModel): config: Config git: GitContext + verbose: bool @click.group() +@click.option( + "-v", + "--verbose", + default=False, + type=bool, + is_flag=True, + help="Exposes extra logging.", +) @click.pass_context -def cli(ctx): +def cli(ctx, verbose: bool): """ A command-line tool to interact with different kinds of code forges. """ + if verbose: + logging.basicConfig(level=logging.INFO) + config = get_configuration() + logger.info(f"Config: {config}") + git_context = get_git_context(domain_aliases=config.domain_aliases) - ctx.obj = CliContext(git=git_context, config=config) + logger.info(f"Context: {git_context}") + + ctx.obj = CliContext(git=git_context, config=config, verbose=verbose) @cli.group() @@ -42,7 +61,6 @@ def pr(ctx): @click.pass_obj def create_pr(ctx, web: bool): """Interacts with pull requests.""" - if web: forgejo_browser.create_pull_request_via_web( head=ctx.git.current_branch, @@ -50,6 +68,8 @@ def create_pr(ctx, web: bool): repo=ctx.git.repo_name, owner=ctx.git.repo_author, ) + else: + raise NotImplementedError("--web is the only mode supported.") @cli.group() @@ -79,7 +99,10 @@ def view_repo(ctx, web: bool): def main(): - cli() + try: + cli() + except Exception as e: + click.secho(f"Error: {e}", fg="red", bold=True) if __name__ == "__main__": diff --git a/frg/context.py b/frg/context.py index 03ba08f..27aaf9a 100644 --- a/frg/context.py +++ b/frg/context.py @@ -7,8 +7,11 @@ invocation environment (i.e. git context, ...) to make things work. import pydantic +import logging import subprocess +logger = logging.getLogger(__name__) + class GitContext(pydantic.BaseModel): """ @@ -49,10 +52,10 @@ def parse_remote_url(remote_url: str) -> tuple[str, str, str]: def get_git_context(*, domain_aliases: dict[str, str] | None = None) -> GitContext: + """Gathers contextual data from the environment.""" if not domain_aliases: domain_aliases = dict() - """Gathers contextual data from the environment.""" current_branch_cmd = subprocess.run( ["git", "branch", "--show-current"], capture_output=True ) @@ -66,7 +69,7 @@ def get_git_context(*, domain_aliases: dict[str, str] | None = None) -> GitConte host, owner, repo = parse_remote_url(remote_url) if host in domain_aliases: - print(f"Remapped domain: {host} -> {domain_aliases[host]}") + logger.info(f"Remapped domain: {host} -> {domain_aliases[host]}") host = domain_aliases[host] return GitContext( diff --git a/uv.lock b/uv.lock index 0777d22..9021aab 100644 --- a/uv.lock +++ b/uv.lock @@ -58,8 +58,8 @@ wheels = [ ] [[package]] -name = "frg" -version = "0.1.0" +name = "forge-tools" +version = "0.0.0" source = { virtual = "." } dependencies = [ { name = "click" },