diff --git a/shellhist/cli/export.py b/shellhist/cli/export.py index 7093ada..e73eea7 100644 --- a/shellhist/cli/export.py +++ b/shellhist/cli/export.py @@ -1 +1,109 @@ -bmFtZTogQ0kKCm9uOgogIHB1c2g6CiAgICBicmFuY2hlczogW21haW5dCiAgcHVsbF9yZXF1ZXN0OgogICAgYnJhbmNoZXM6IFttYWluXQoKam9iczogCiAgdGVzdDoKICAgIHJ1bnMtb246IHVidW50dS1sYXRlc3QKICAgIHN0ZXBzOgogICAgICAtIHVzZXM6IGFjdGlvbnMvY2hlY2tvdXQdjNFgKICAgICAgdXNlczogYWN0aW9ucy9zZXR1cC1weXRob25fdjUKICAgICAgd2l0aDoKICAgICAgICBweXRob24tdmVyc2lvbjogJzMuMTEnCiAgICAtIHJ1bjogcGlwIGluc3RhbGwgLWUgIltcImRldlwiXSIKICAgIC0gcnVuOiBweXRlc3QgdGVzdHMvIC12CiAgICAtIHJ1bjogcnVmZiBjaGVjayAu \ No newline at end of file +"""Export command for generating scripts from detected patterns.""" + +from typing import Optional + +import click +from rich.console import Console +from rich.panel import Panel + +from shellhist.core import HistoryLoader +from shellhist.core.patterns import ( + detect_command_pairs, + detect_command_triplets, + detect_repetitive_commands, +) +from shellhist.core.export import generate_script + + +@click.command("export-script") +@click.option( + "--history", + "-H", + type=str, + help="Path to history file", +) +@click.option( + "--output", + "-o", + type=str, + default=".", + help="Output directory for generated scripts", +) +@click.option( + "--name", + "-n", + type=str, + default="shellhist_script", + help="Name for the generated script", +) +@click.option( + "--dry-run", + "-d", + is_flag=True, + default=False, + help="Show script content without writing to file", +) +@click.option( + "--shell", + "-s", + type=click.Choice(["bash", "zsh"]), + help="Shell type for parsing", +) +@click.pass_context +def export_command( + ctx: click.Context, + history: Optional[str], + output: str, + name: str, + dry_run: bool, + shell: Optional[str], +) -> None: + """Export detected patterns to executable shell scripts. + + Examples: + + \b + shellhist export-script + shellhist export-script --output ./scripts/ --name myscript + shellhist export-script --dry-run + """ + console = Console() + + try: + loader = HistoryLoader(history_path=history) + store = loader.load() + + if not store.entries: + console.print("[yellow]No entries found in history.[/yellow]") + return + + pairs = detect_command_pairs(store, min_frequency=2) + triplets = detect_command_triplets(store, min_frequency=2) + repetitive = detect_repetitive_commands(store, min_frequency=2) + + all_patterns = pairs + triplets + repetitive + + if not all_patterns: + console.print( + "[yellow]No patterns found to export.[/yellow]" + ) + return + + result = generate_script( + all_patterns, + script_name=name, + output_dir=output, + dry_run=dry_run, + ) + + if dry_run: + console.print(Panel(result, title="Generated Script (Dry Run)", expand=False)) + else: + console.print(f"\n[green]Script exported to: {result}[/green]") + + except FileNotFoundError as e: + console.print(f"[red]Error: {e}[/red]") + ctx.exit(1) + except Exception as e: + console.print(f"[red]Error exporting script: {e}[/red]") + ctx.exit(1)