Botica.Doctor (Botica v1.0.0)

Copy Markdown View Source

Environment diagnostics and auto-repair — returns structured results.

Define checks and use run/1 to diagnose or fix/1 to repair. All functions return data structures — the caller decides how to display.

Check definition

config = %{
  app_name: "myapp",
  checks: [
    %{
      id: :postgresql,
      name: "PostgreSQL",
      description: "Database server",
      priority: 1,
      check: fn -> {:ok, "v14"} end,
      fix: fn -> {:ok, "installed"} end,
      fix_command: "sudo apt install postgresql-14"
    }
  ]
}

Each check/0 returns {:ok, msg}, {:warning, msg} or {:error, msg}. Each fix/0 returns {:ok, msg}, {:error, msg} or :skipped.

Return values

run/1 returns {:ok, results} — all checks always return ok wrapper, the individual status is embedded in each result map.

fix/1 returns :ok — repairs are applied silently.

summary/1 returns a map with counts and a passed? boolean.

Summary

Functions

Runs fixes for all checks with errors.

Runs a quick health check and returns a simple pass/fail map.

Runs all checks in parallel and returns structured results.

Summarizes a list of check results into a single map.

Types

check_def()

@type check_def() :: %{
  id: check_id(),
  name: String.t(),
  description: String.t(),
  priority: non_neg_integer(),
  check: (-> check_result()),
  fix: (-> fix_result()),
  fix_command: String.t() | nil
}

check_id()

@type check_id() :: atom()

check_result()

@type check_result() ::
  {:ok, String.t()} | {:warning, String.t()} | {:error, String.t()}

config()

@type config() :: %{app_name: String.t(), checks: [check_def()]}

fix_result()

@type fix_result() :: {:ok, String.t()} | {:error, String.t()} | :skipped

result()

@type result() :: %{
  id: check_id(),
  name: String.t(),
  status: :ok | :warning | :error,
  message: String.t(),
  fix_command: String.t() | nil
}

summary()

@type summary() :: %{
  ok: non_neg_integer(),
  warning: non_neg_integer(),
  error: non_neg_integer(),
  total: non_neg_integer(),
  passed?: boolean()
}

Functions

fix(config)

@spec fix(config()) :: :ok

Runs fixes for all checks with errors.

health_check(config)

@spec health_check(config()) :: %{status: :ok | :degraded | :fail, summary: summary()}

Runs a quick health check and returns a simple pass/fail map.

Shorthand for run/1 + summary/1 — returns %{status: :ok | :degraded | :fail, summary: summary()}.

Examples

iex> Doctor.health_check(config)
%{status: :ok, summary: %{ok: 5, warning: 0, error: 0, total: 5, passed?: true}}

run(config)

@spec run(config()) :: {:ok, [result()]}

Runs all checks in parallel and returns structured results.

summary(results)

@spec summary([result()]) :: summary()

Summarizes a list of check results into a single map.