import os import json import yaml import click from rich import print as rprint from rich.panel import Panel from rich.table import Table from ..config import Config from ..database import Database from ..history import HistoryManager @click.group() def export(): """Export data.""" pass @export.command() @click.option("--output", "-o", default="commands.json", help="Output file") def commands(output): """Export commands to file.""" db = Database() history = HistoryManager(db=db) commands = db.get_commands(limit=10000) data = [c.to_dict() for c in commands] db.close() if output.endswith(".json"): with open(output, "w") as f: json.dump(data, f, indent=2, default=str) else: with open(output, "w") as f: yaml.dump(data, f) rprint(f"[green]Exported {len(commands)} commands to {output}[/green]") @export.command() @click.option("--output", "-o", default="workflows.yaml", help="Output file") def workflows(output): """Export workflows to file.""" db = Database() workflows = db.get_all_workflows() data = [w.to_dict() for w in workflows] db.close() if output.endswith(".json"): with open(output, "w") as f: json.dump(data, f, indent=2, default=str) else: with open(output, "w") as f: yaml.dump(data, f) rprint(f"[green]Exported {len(workflows)} workflows to {output}[/green]") @export.command() @click.option("--output", "-o", default="backup.json", help="Output file") def all(output): """Export all data.""" db = Database() history = HistoryManager(db=db) data_str = history.export_history(format="json" if output.endswith(".json") else "yaml") with open(output, "w") as f: f.write(data_str) db.close() rprint(f"[green]Exported all data to {output}[/green]") @export.command() def scripts(): """List generated scripts.""" from ..generator import ScriptGenerator generator = ScriptGenerator() scripts = generator.list_generated_scripts() if not scripts: rprint("[yellow]No generated scripts found[/yellow]") return table = Table(title="Generated Scripts") table.add_column("Name", style="cyan") table.add_column("Path", style="magenta") for s in scripts: table.add_row(s["name"], s["path"]) rprint(Panel(table, title=f"{len(scripts)} Scripts"))