feat: add verbose flag, extra logging #2

Merged
marc merged 1 commit from feat/verbose-mode into main 2024-11-02 22:23:51 +00:00
3 changed files with 35 additions and 9 deletions

View file

@ -2,26 +2,45 @@ import click
import pydantic import pydantic
import frg.forgejo.browser as forgejo_browser import frg.forgejo.browser as forgejo_browser
from frg.context import get_git_context, GitContext from frg.context import get_git_context, GitContext
from frg.configuration import get_configuration, Config from frg.configuration import get_configuration, Config
import logging
logger = logging.getLogger(__name__)
class CliContext(pydantic.BaseModel): class CliContext(pydantic.BaseModel):
config: Config config: Config
git: GitContext git: GitContext
verbose: bool
@click.group() @click.group()
@click.option(
"-v",
"--verbose",
default=False,
type=bool,
is_flag=True,
help="Exposes extra logging.",
)
@click.pass_context @click.pass_context
def cli(ctx): def cli(ctx, verbose: bool):
""" """
A command-line tool to interact with different kinds of code forges. A command-line tool to interact with different kinds of code forges.
""" """
if verbose:
logging.basicConfig(level=logging.INFO)
config = get_configuration() config = get_configuration()
logger.info(f"Config: {config}")
git_context = get_git_context(domain_aliases=config.domain_aliases) 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() @cli.group()
@ -42,7 +61,6 @@ def pr(ctx):
@click.pass_obj @click.pass_obj
def create_pr(ctx, web: bool): def create_pr(ctx, web: bool):
"""Interacts with pull requests.""" """Interacts with pull requests."""
if web: if web:
forgejo_browser.create_pull_request_via_web( forgejo_browser.create_pull_request_via_web(
head=ctx.git.current_branch, head=ctx.git.current_branch,
@ -50,6 +68,8 @@ def create_pr(ctx, web: bool):
repo=ctx.git.repo_name, repo=ctx.git.repo_name,
owner=ctx.git.repo_author, owner=ctx.git.repo_author,
) )
else:
raise NotImplementedError("--web is the only mode supported.")
@cli.group() @cli.group()
@ -79,7 +99,10 @@ def view_repo(ctx, web: bool):
def main(): def main():
try:
cli() cli()
except Exception as e:
click.secho(f"Error: {e}", fg="red", bold=True)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -7,8 +7,11 @@ invocation environment (i.e. git context, ...) to make things work.
import pydantic import pydantic
import logging
import subprocess import subprocess
logger = logging.getLogger(__name__)
class GitContext(pydantic.BaseModel): 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: def get_git_context(*, domain_aliases: dict[str, str] | None = None) -> GitContext:
"""Gathers contextual data from the environment."""
if not domain_aliases: if not domain_aliases:
domain_aliases = dict() domain_aliases = dict()
"""Gathers contextual data from the environment."""
current_branch_cmd = subprocess.run( current_branch_cmd = subprocess.run(
["git", "branch", "--show-current"], capture_output=True ["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) host, owner, repo = parse_remote_url(remote_url)
if host in domain_aliases: 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] host = domain_aliases[host]
return GitContext( return GitContext(

View file

@ -58,8 +58,8 @@ wheels = [
] ]
[[package]] [[package]]
name = "frg" name = "forge-tools"
version = "0.1.0" version = "0.0.0"
source = { virtual = "." } source = { virtual = "." }
dependencies = [ dependencies = [
{ name = "click" }, { name = "click" },