import sys from datetime import datetime from pathlib import Path from typing import Optional import click from rich import print as rprint from git_insights import GitInsights from git_insights.formatters import DashboardFormatter, JSONFormatter, MarkdownFormatter, HTMLFormatter @click.group() def main(): """Git Insights CLI - Analyze git repositories for productivity insights.""" pass @main.command() @click.argument("path", type=click.Path(exists=True), required=False, default=".") @click.option("--days", type=int, default=30, help="Number of days to analyze") @click.option("--format", type=click.Choice(["json", "markdown", "html"]), default="console", help="Output format") @click.option("-v", "--verbose", is_flag=True, help="Enable verbose output") def analyze(path: str, days: int, format: str, verbose: bool): """Analyze a git repository for commit patterns and productivity metrics.""" try: insights = GitInsights(path, days=days) result = insights.analyze() if format == "json": click.echo(JSONFormatter.format(result)) elif format == "markdown": click.echo(MarkdownFormatter.format(result)) elif format == "html": click.echo(HTMLFormatter.format(result)) else: DashboardFormatter.display(result) if verbose: click.echo(f"\nAnalysis period: {days} days") click.echo(f"Repository: {Path(path).resolve()}") click.echo(f"Timestamp: {datetime.now().isoformat()}") except Exception as e: rprint(f"[red]Error: {e}[/red]") sys.exit(1) @main.command() @click.argument("path", type=click.Path(exists=True), required=False, default=".") @click.option("--days", type=int, default=30, help="Number of days to analyze") def dashboard(path: str, days: int): """Display productivity metrics in an interactive dashboard.""" try: insights = GitInsights(path, days=days) result = insights.analyze() DashboardFormatter.display(result) except Exception as e: rprint(f"[red]Error: {e}[/red]") sys.exit(1) @main.command() @click.argument("path", type=click.Path(exists=True), required=False, default=".") @click.option("--days", type=int, default=30, help="Number of days to analyze") @click.option("--format", type=click.Choice(["json", "markdown", "html"]), default="json", help="Output format") @click.option("--output", type=click.Path(), help="Output file path") def export(path: str, days: int, format: str, output: Optional[str]): """Export analysis results to a file.""" try: insights = GitInsights(path, days=days) result = insights.analyze() if format == "json": content = JSONFormatter.format(result) elif format == "markdown": content = MarkdownFormatter.format(result) else: content = HTMLFormatter.format(result) if output: Path(output).write_text(content) click.echo(f"Exported to {output}") else: click.echo(content) except Exception as e: rprint(f"[red]Error: {e}[/red]") sys.exit(1) @main.command() @click.argument("path", type=click.Path(exists=True), required=False, default=".") @click.option("--days", type=int, default=30, help="Number of days to analyze") @click.option("--output", type=click.Path(), help="Output file path") def report(path: str, days: int, output: Optional[str]): """Generate a comprehensive productivity report.""" try: insights = GitInsights(path, days=days) report = insights.get_productivity_report() content = HTMLFormatter.format(report) if output: Path(output).write_text(content) click.echo(f"Report generated: {output}") else: click.echo(content) except Exception as e: rprint(f"[red]Error: {e}[/red]") sys.exit(1)