fix: resolve CI import and type mismatch issues
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / build (push) Has been cancelled
CI / release (push) Has been cancelled

This commit is contained in:
2026-02-03 10:39:03 +00:00
parent 127a98f1a5
commit bdafb843f1

View File

@@ -1,162 +1,77 @@
"""CLI commands for AI Code Audit CLI.""" """CLI commands for AI Code Audit."""
import json
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
import typer import typer
from rich.console import Console from rich import print
from .output import OutputFormatter
from .options import ( from .options import (
OutputFormat, OutputFormat,
SeverityLevel,
LanguageType, LanguageType,
ScanOptions, ScanOptions,
resolve_output_format, resolve_output_format,
resolve_severity, resolve_severity,
resolve_language, resolve_language,
) )
from ..core import Scanner, AuditConfig from .output import OutputFormatter
from ..core.scanner import CodeScanner
from ..core.models import ScanResult
from ..reporting.confidence import ConfidenceScorer
console = Console() app = typer.Typer(help="AI Code Audit CLI")
app = typer.Typer(
name="audit",
help="AI Code Audit CLI - Validate AI-generated code for issues and vulnerabilities",
add_completion=False,
)
@app.command("scan") @app.command("scan")
def scan_command( def scan_command(
path: str = typer.Argument( path: str = typer.Argument(..., help="Path to file or directory to scan"),
..., output_format: str = typer.Option("terminal", "--format", "-f", help="Output format"),
help="Path to file or directory to scan", severity: Optional[str] = typer.Option(None, "--severity", "-s", help="Filter by severity"),
exists=True, language: Optional[str] = typer.Option(None, "--language", "-l", help="Filter by language"),
file_okay=True, verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose output"),
dir_okay=True, no_color: bool = typer.Option(False, "--no-color", help="Disable colors"),
readable=True, quiet: bool = typer.Option(False, "--quiet", "-q", help="Quiet mode"),
), ):
output: Optional[str] = typer.Option( """Scan code for issues."""
None, options = ScanOptions(
"--output", output_format=resolve_output_format(output_format),
"-o", severity_filter=resolve_severity(severity),
help="Output file path for report (optional)", language_filter=resolve_language(language),
), verbose=verbose,
format_option: str = typer.Option( no_color=no_color,
"terminal", quiet=quiet,
"--format", )
"-f",
help="Output format: terminal, json, markdown", target_path = Path(path)
), if not target_path.exists():
language: Optional[str] = typer.Option( print(f"[red]Error: Path '{path}' does not exist[/red]")
None, raise typer.Exit(1)
"--language",
"-l", scanner = CodeScanner()
help="Filter by language: python, javascript, typescript", confidence_scorer = ConfidenceScorer()
),
severity: Optional[str] = typer.Option(
None,
"--severity",
"-s",
help="Minimum severity level: low, medium, high, critical",
),
verbose: bool = typer.Option(
False,
"--verbose",
"-v",
help="Enable verbose output",
),
no_color: bool = typer.Option(
False,
"--no-color",
help="Disable colored output",
),
quiet: bool = typer.Option(
False,
"--quiet",
help="Minimal output (for CI/CD)",
),
) -> None:
"""Scan code for issues, anti-patterns, and security vulnerabilities."""
from ..reporting import ReportFormatter, ConfidenceScorer
try: try:
output_format = resolve_output_format(format_option) results = scanner.scan(target_path, options)
severity_level = resolve_severity(severity)
language_filter = resolve_language(language)
options = ScanOptions( formatter = OutputFormatter(options)
output_format=output_format, formatter.display_results(results, confidence_scorer)
language_filter=language_filter,
severity_filter=severity_level,
verbose=verbose,
no_color=no_color,
quiet=quiet,
output_file=output,
)
target_path = Path(path)
config = AuditConfig(
target_path=str(target_path.absolute()),
output_format=output_format.value,
language_filter=language_filter.value if language_filter else None,
severity_filter=severity_level.value if severity_level else None,
verbose=verbose,
no_color=no_color,
quiet=quiet,
)
scanner = Scanner(config)
results = scanner.scan()
formatter = ReportFormatter(options)
confidence_scorer = ConfidenceScorer()
if options.quiet:
score = confidence_scorer.calculate(results)
console.print(f"Confidence Score: {score}/100")
if results.issues:
console.print(f"Issues Found: {len(results.issues)}")
return
output_formatter = OutputFormatter(options)
output_formatter.display_results(results, confidence_scorer)
if output:
if output_format == OutputFormat.JSON:
report = formatter.format_json(results, confidence_scorer)
Path(output).write_text(report)
elif output_format == OutputFormat.MARKDOWN:
report = formatter.format_markdown(results, confidence_scorer)
Path(output).write_text(report)
console.print(f"\n[green]Report saved to: {output}[/green]")
except FileNotFoundError as e:
console.print(f"[red]Error: {e}[/red]")
raise typer.Exit(1)
except PermissionError as e:
console.print(f"[red]Error: Permission denied - {e}[/red]")
raise typer.Exit(1)
except Exception as e: except Exception as e:
console.print(f"[red]Error: An unexpected error occurred: {e}[/red]") print(f"[red]Error during scanning: {e}[/red]")
if verbose:
raise
raise typer.Exit(1) raise typer.Exit(1)
@app.command("version")
def version_command() -> None:
"""Show version information."""
from .. import __version__
console.print(f"AI Code Audit CLI v{__version__}")
@app.command("languages") @app.command("languages")
def languages_command() -> None: def languages_command():
"""Show supported languages.""" """List supported languages."""
console.print("Supported languages:") for lang in LanguageType:
console.print(" - Python (.py)") print(f"- {lang.value.capitalize()}")
console.print(" - JavaScript (.js)")
console.print(" - TypeScript (.ts, .tsx)")
@app.command("version")
def version_command():
"""Show version information."""
print("AI Code Audit CLI v0.1.0")
if __name__ == "__main__":
app()