From cfd4e08e4e4920edb7672bc779245776943e7b32 Mon Sep 17 00:00:00 2001 From: "dingfeng.wong" Date: Thu, 10 Jul 2025 00:13:53 +0800 Subject: [PATCH] factor out wrap --- src/elf/cli.py | 98 +----------------------------------------- src/elf/wrap_cli.py | 101 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 97 deletions(-) create mode 100644 src/elf/wrap_cli.py diff --git a/src/elf/cli.py b/src/elf/cli.py index 656e3c3..a30c6ed 100644 --- a/src/elf/cli.py +++ b/src/elf/cli.py @@ -1,106 +1,10 @@ import typer from rich.console import Console -import tarfile -import gzip -import pyperclip -import io -import base64 +from .wrap_cli import wrap_app app = typer.Typer() console = Console() -# Create a subcommand for wrap -wrap_app = typer.Typer() - -# Create a subcommand for encode -encode_app = typer.Typer() - -# Create a subcommand for decode -decode_app = typer.Typer() - -@encode_app.command("dir", short_help="Wrap files into a gzipped tar archive.") -def encode_dir( - files: list[str] = typer.Argument( - ..., help="List of files to wrap into a gzipped tar archive." - ) -): - """ - Creates a gzipped tar archive from the given files and copies it to the clipboard. - """ - console.print(f"Wrapping files: {files}") - - try: - # Create an in-memory buffer for the tar.gz file - buffer = io.BytesIO() - with gzip.open(buffer, "wb") as gz: - with tarfile.open(fileobj=gz, mode="w") as tar: - for file_path in files: - tar.add(file_path, arcname=file_path) - - # Get the gzipped tar content as bytes - gzipped_tar_content = buffer.getvalue() - - base64_encoded_content = base64.b64encode(gzipped_tar_content).decode('utf-8') - - pyperclip.copy(base64_encoded_content) - console.print("[green]Gzipped tar archive copied to clipboard (base64 encoded)![/green]") - - except FileNotFoundError as e: - console.print(f"[red]Error: One or more files not found: {e}[/red]") - raise typer.Exit(code=1) - except Exception as e: - console.print(f"[red]An unexpected error occurred: {e}[/red]") - raise typer.Exit(code=1) - -@decode_app.command("dir", short_help="Decode a base64 encoded, gzipped tar archive.") -def decode_dir( - destination_dir: str = typer.Option( - ".", "--destination-dir", "-d", help="Directory to extract the files to." - ) -): - """ - Decodes a base64 encoded, gzipped tar archive from the clipboard and extracts its contents. - """ - console.print("[blue]Attempting to decode and extract from clipboard...[/blue]") - - try: - encoded_content = pyperclip.paste() - if not encoded_content: - console.print("[red]Error: Clipboard is empty or contains no text.[/red]") - raise typer.Exit(code=1) - - # Decode base64 content - gzipped_tar_content = base64.b64decode(encoded_content) - - # Create an in-memory buffer from the gzipped content - buffer = io.BytesIO(gzipped_tar_content) - - # Open the gzipped tar file and extract - with gzip.open(buffer, "rb") as gz: - with tarfile.open(fileobj=gz, mode="r") as tar: - tar.extractall(path=destination_dir) - - console.print(f"[green]Successfully extracted files to {destination_dir}![/green]") - - except base64.binascii.Error: - console.print("[red]Error: Clipboard content is not valid base64 encoded data.[/red]") - raise typer.Exit(code=1) - except gzip.BadGzipFile: - console.print("[red]Error: Clipboard content is not a valid gzipped file.[/red]") - raise typer.Exit(code=1) - except tarfile.ReadError: - console.print("[red]Error: Clipboard content is not a valid tar archive.[/red]") - raise typer.Exit(code=1) - except Exception as e: - console.print(f"[red]An unexpected error occurred during decode: {e}[/red]") - raise typer.Exit(code=1) - -# Add the encode subcommand to the wrap app -wrap_app.add_typer(encode_app, name="encode") - -# Add the decode subcommand to the wrap app -wrap_app.add_typer(decode_app, name="decode") - # Add the wrap subcommand to the main app app.add_typer(wrap_app, name="wrap") diff --git a/src/elf/wrap_cli.py b/src/elf/wrap_cli.py new file mode 100644 index 0000000..e3e8cf2 --- /dev/null +++ b/src/elf/wrap_cli.py @@ -0,0 +1,101 @@ +import typer +from rich.console import Console +import tarfile +import gzip +import pyperclip +import io +import base64 + +console = Console() + +# Create a subcommand for wrap +wrap_app = typer.Typer() + +# Create a subcommand for encode +encode_app = typer.Typer() + +# Create a subcommand for decode +decode_app = typer.Typer() + +@encode_app.command("dir", short_help="Wrap files into a gzipped tar archive.") +def encode_dir( + files: list[str] = typer.Argument( + ..., help="List of files to wrap into a gzipped tar archive." + ) +): + """ + Creates a gzipped tar archive from the given files and copies it to the clipboard. + """ + console.print(f"Wrapping files: {files}") + + try: + # Create an in-memory buffer for the tar.gz file + buffer = io.BytesIO() + with gzip.open(buffer, "wb") as gz: + with tarfile.open(fileobj=gz, mode="w") as tar: + for file_path in files: + tar.add(file_path, arcname=file_path) + + # Get the gzipped tar content as bytes + gzipped_tar_content = buffer.getvalue() + + base64_encoded_content = base64.b64encode(gzipped_tar_content).decode('utf-8') + + pyperclip.copy(base64_encoded_content) + console.print("[green]Gzipped tar archive copied to clipboard (base64 encoded)![/green]") + + except FileNotFoundError as e: + console.print(f"[red]Error: One or more files not found: {e}[/red]") + raise typer.Exit(code=1) + except Exception as e: + console.print(f"[red]An unexpected error occurred: {e}[/red]") + raise typer.Exit(code=1) + +@decode_app.command("dir", short_help="Decode a base64 encoded, gzipped tar archive.") +def decode_dir( + destination_dir: str = typer.Option( + ".", "--destination-dir", "-d", help="Directory to extract the files to." + ) +): + """ + Decodes a base64 encoded, gzipped tar archive from the clipboard and extracts its contents. + """ + console.print("[blue]Attempting to decode and extract from clipboard...[/blue]") + + try: + encoded_content = pyperclip.paste() + if not encoded_content: + console.print("[red]Error: Clipboard is empty or contains no text.[/red]") + raise typer.Exit(code=1) + + # Decode base64 content + gzipped_tar_content = base64.b64decode(encoded_content) + + # Create an in-memory buffer from the gzipped content + buffer = io.BytesIO(gzipped_tar_content) + + # Open the gzipped tar file and extract + with gzip.open(buffer, "rb") as gz: + with tarfile.open(fileobj=gz, mode="r") as tar: + tar.extractall(path=destination_dir) + + console.print(f"[green]Successfully extracted files to {destination_dir}![/green]") + + except base64.binascii.Error: + console.print("[red]Error: Clipboard content is not valid base64 encoded data.[/red]") + raise typer.Exit(code=1) + except gzip.BadGzipFile: + console.print("[red]Error: Clipboard content is not a valid gzipped file.[/red]") + raise typer.Exit(code=1) + except tarfile.ReadError: + console.print("[red]Error: Clipboard content is not a valid tar archive.[/red]") + raise typer.Exit(code=1) + except Exception as e: + console.print(f"[red]An unexpected error occurred during decode: {e}[/red]") + raise typer.Exit(code=1) + +# Add the encode subcommand to the wrap app +wrap_app.add_typer(encode_app, name="encode") + +# Add the decode subcommand to the wrap app +wrap_app.add_typer(decode_app, name="decode") \ No newline at end of file