Run a command and handle errors with rich output.
(cmd: list[str], cwd: Optional[Path] = None, check: bool = True)
| 42 | |
| 43 | |
| 44 | def run_command(cmd: list[str], cwd: Optional[Path] = None, check: bool = True) -> subprocess.CompletedProcess: |
| 45 | """Run a command and handle errors with rich output.""" |
| 46 | console.print(f"[dim]Running:[/dim] [cyan]{' '.join(cmd)}[/cyan]") |
| 47 | |
| 48 | try: |
| 49 | result = subprocess.run(cmd, cwd=cwd, check=check, capture_output=True, text=True) |
| 50 | if result.stdout.strip(): |
| 51 | console.print(f"[dim]{result.stdout.strip()}[/dim]") |
| 52 | return result |
| 53 | except subprocess.CalledProcessError as e: |
| 54 | console.print(f"[red]Error running command:[/red] {e}") |
| 55 | if e.stderr: |
| 56 | console.print(f"[red]Error output:[/red] {e.stderr}") |
| 57 | if e.stdout: |
| 58 | console.print(f"[yellow]Output:[/yellow] {e.stdout}") |
| 59 | sys.exit(1) |
| 60 | |
| 61 | |
| 62 |