feature: add verbose flag, improve command output, improve error display
This commit is contained in:
+30
-10
@@ -1,6 +1,10 @@
|
||||
import subprocess
|
||||
from dataclasses import dataclass
|
||||
|
||||
from rich.markup import escape
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
|
||||
from git_flow import FLOWCONFIG_FILENAME
|
||||
from git_flow.io import *
|
||||
|
||||
@@ -180,10 +184,13 @@ class Git:
|
||||
self._run(**kwargs)
|
||||
|
||||
def _get(self, print: str | None = None, check: bool = True):
|
||||
process = subprocess.run(self.command, capture_output=True, text=True)
|
||||
process = subprocess.run(self.command, capture_output=True, text=True, check=False)
|
||||
|
||||
if print is not None:
|
||||
self._print_process(process.stdout, process.stderr, print)
|
||||
# Sin --verbose solo se muestra el paso; si falla, la salida la muestra main()
|
||||
if print is not None and settings.verbose:
|
||||
self._print_process(process, print)
|
||||
elif print is not None:
|
||||
console.print(Text.assemble(("› ", "cyan"), print))
|
||||
|
||||
if check:
|
||||
process.check_returncode()
|
||||
@@ -193,7 +200,7 @@ class Git:
|
||||
def _run(self, print: str | None = None, check: bool = True):
|
||||
return self._get(print, check)
|
||||
|
||||
def _print_process(self, stdout: str, stderr: str, title: str):
|
||||
def _print_process(self, process: subprocess.CompletedProcess, title: str | None):
|
||||
command = []
|
||||
|
||||
for arg in self.command:
|
||||
@@ -205,12 +212,25 @@ class Git:
|
||||
|
||||
command.append(arg)
|
||||
|
||||
text = f"[green]$ {' '.join(command)}[/green]"
|
||||
ok = process.returncode == 0
|
||||
# git escribe mensajes informativos en stderr, así que solo se resalta si falló
|
||||
stderr_style = "dim" if ok else "red"
|
||||
text = Text.assemble(("$ ", "dim"), (" ".join(command), "bold yellow"))
|
||||
|
||||
for line in stdout.splitlines():
|
||||
text += "\n" + line
|
||||
for line in process.stdout.splitlines():
|
||||
text.append("\n" + line)
|
||||
|
||||
for line in stderr.splitlines():
|
||||
text += "\n[red]" + line + "[/red]"
|
||||
for line in process.stderr.splitlines():
|
||||
text.append("\n" + line, stderr_style)
|
||||
|
||||
panel(title or "Ejecutando", text)
|
||||
console.print(
|
||||
Panel(
|
||||
text,
|
||||
title=f"[bold]{escape(title or 'Ejecutando')}[/bold]",
|
||||
title_align="left",
|
||||
subtitle=None if ok else f"código de salida {process.returncode}",
|
||||
subtitle_align="right",
|
||||
border_style="bright_black" if ok else "red",
|
||||
expand=False,
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user