feature: add verbose flag, improve command output, improve error display
This commit is contained in:
+50
-6
@@ -1,12 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import traceback
|
||||
from subprocess import SubprocessError
|
||||
from subprocess import CalledProcessError, SubprocessError
|
||||
from typing import Annotated
|
||||
|
||||
import click
|
||||
import typer
|
||||
from rich.markup import escape
|
||||
from rich.traceback import Traceback
|
||||
|
||||
from git_flow import GitFlowError
|
||||
from git_flow.command import base, branch, commit, init, merge, new, release, tag
|
||||
from git_flow.io import console, settings
|
||||
|
||||
REPOSITORY_TOKEN_PATH = ".repository-token"
|
||||
LOCALE = "es_AR.UTF-8"
|
||||
@@ -22,15 +26,55 @@ app.add_typer(release.app)
|
||||
app.add_typer(branch.app)
|
||||
|
||||
|
||||
@app.callback()
|
||||
def options(
|
||||
verbose: Annotated[
|
||||
bool,
|
||||
typer.Option(
|
||||
"--verbose",
|
||||
"-v",
|
||||
help="Muestra la salida de los comandos git y el traceback de los errores",
|
||||
),
|
||||
] = False,
|
||||
):
|
||||
settings.verbose = verbose
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
app()
|
||||
print()
|
||||
except GitFlowError as e:
|
||||
lines = [str(e), ""]
|
||||
lines += traceback.format_exception(e)
|
||||
base.io_error("".join(lines), title="Git Flow")
|
||||
_print_error(escape(str(e)), "Git Flow", e)
|
||||
except CalledProcessError as e:
|
||||
_print_error(_format_process_error(e), "Comando", e)
|
||||
except SubprocessError as e:
|
||||
base.io_error(str(e), title="Comando")
|
||||
_print_error(escape(str(e)), "Comando", e)
|
||||
except KeyboardInterrupt:
|
||||
base.io_error("Ejecución abortada")
|
||||
|
||||
|
||||
def _format_process_error(e: CalledProcessError) -> str:
|
||||
command = " ".join(e.cmd) if isinstance(e.cmd, list) else str(e.cmd)
|
||||
message = f"[bold]$ {escape(command)}[/bold]\nTerminó con código de salida {e.returncode}"
|
||||
|
||||
for output in (e.stdout, e.stderr):
|
||||
if isinstance(output, str) and output.strip():
|
||||
message += "\n\n" + escape(output.strip())
|
||||
|
||||
return message
|
||||
|
||||
|
||||
def _print_error(message: str, title: str, e: BaseException):
|
||||
if settings.verbose:
|
||||
base.io_error(message, title=title)
|
||||
console.print(
|
||||
Traceback.from_exception(
|
||||
type(e), e, e.__traceback__, suppress=[click, typer]
|
||||
)
|
||||
)
|
||||
else:
|
||||
base.io_error(
|
||||
message + "\n\n[dim]Ejecutar con `git-flow --verbose <comando>` para ver el detalle[/dim]",
|
||||
title=title,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user