Initial commit: mycli toolkit with all commands
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
"""Utility functions and decorators for mycli."""
|
||||
|
||||
import functools
|
||||
import subprocess
|
||||
|
||||
import click
|
||||
|
||||
|
||||
def handle_errors(func):
|
||||
"""Decorator to handle common errors gracefully."""
|
||||
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except FileNotFoundError as e:
|
||||
click.secho(f"Command not found: {e.filename}", fg="red", err=True)
|
||||
raise click.Exit(1)
|
||||
except subprocess.CalledProcessError as e:
|
||||
click.secho(f"Command failed with exit code {e.returncode}", fg="red", err=True)
|
||||
if e.stderr:
|
||||
click.echo(e.stderr, err=True)
|
||||
raise click.Exit(1)
|
||||
except KeyboardInterrupt:
|
||||
click.echo("\nAborted.")
|
||||
raise click.Exit(130)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def run_command(cmd: list[str], check: bool = True, **kwargs) -> subprocess.CompletedProcess:
|
||||
"""Run a subprocess command with consistent error handling.
|
||||
|
||||
Args:
|
||||
cmd: Command and arguments as list
|
||||
check: Whether to check return code
|
||||
**kwargs: Additional args for subprocess.run
|
||||
|
||||
Returns:
|
||||
CompletedProcess instance
|
||||
"""
|
||||
return subprocess.run(cmd, check=check, **kwargs)
|
||||
Reference in New Issue
Block a user