Skip to content

climate_ref.cli.doctor #

Check a deployment for problems that a solve would otherwise hide.

DoctorFormat #

Bases: StrEnum

Output format for ref doctor.

Source code in packages/climate-ref/src/climate_ref/cli/doctor.py
class DoctorFormat(StrEnum):
    """
    Output format for ``ref doctor``.
    """

    text = "text"
    markdown = "markdown"
    json = "json"

doctor(ctx, output_format=DoctorFormat.text, verbose=True, strict=False, environment=None, list_checks=False) #

Check this deployment for data and configuration problems.

Use --format markdown to produce a report, including a description of this deployment that can be pasted into a bug report.

Exits non-zero if any error is found, or any warning when --strict is used.

Source code in packages/climate-ref/src/climate_ref/cli/doctor.py
def doctor(  # noqa: PLR0913
    ctx: typer.Context,
    output_format: Annotated[
        DoctorFormat,
        typer.Option(
            "--format",
            help="Output format: 'text' (default), 'markdown' to paste into an issue, or 'json'.",
        ),
    ] = DoctorFormat.text,
    verbose: Annotated[
        bool,
        typer.Option("--verbose/--quiet", help="Include the explanation and remedy for each finding."),
    ] = True,
    strict: Annotated[
        bool,
        typer.Option(help="Exit non-zero for warnings as well as errors."),
    ] = False,
    environment: Annotated[
        bool | None,
        typer.Option(
            "--environment/--no-environment",
            help="Include a description of this deployment. On by default for the markdown and json formats.",
        ),
    ] = None,
    list_checks: Annotated[
        bool,
        typer.Option("--list", help="List the available checks and where they came from, then exit."),
    ] = False,
) -> None:
    """
    Check this deployment for data and configuration problems.

    Use `--format markdown` to produce a report,
    including a description of this deployment that can be pasted into a bug report.

    Exits non-zero if any error is found, or any warning when --strict is used.
    """
    console = ctx.obj.console

    if list_checks:
        _list_checks(console)
        return

    # The environment is the point of the machine-readable formats, and noise in the default one.
    if environment is None:
        environment = output_format != DoctorFormat.text

    context = DoctorContext(config=ctx.obj.config, database=ctx.obj.database)
    report = diagnose(context, environment=environment)

    if output_format == DoctorFormat.json:
        print(_render_json(report))
    elif output_format == DoctorFormat.markdown:
        print(_render_markdown(report))
    else:
        if report.findings:
            # Stated before the findings so the size of the problem does not need scrolling to.
            console.print(f"[bold]{_summary_line(report.findings, report.check_count)}[/bold]")
            _print_findings(console, report.findings, verbose)
        else:
            console.print(f"[green]No problems found[/green] ({pluralise(report.check_count, 'check')})")
        if report.environment is not None:
            _print_environment(console, report.environment)

    if report.worst_severity == Severity.ERROR or (strict and report.worst_severity == Severity.WARNING):
        raise typer.Exit(1)